Customized comboboxes :: Riverlayout : JGoodies binding , valueholder
package samples;
/* * Copyright (c) 2008 - 2008 willems piet pwProTech All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList;
import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.EmptyBorder;
import se.datadosen.component.RiverLayout;
import com.jgoodies.binding.adapter.BasicComponentFactory; import com.jgoodies.binding.adapter.ComboBoxAdapter; import com.jgoodies.binding.value.ValueHolder; import com.jgoodies.binding.value.ValueModel;
public class CustomComboAdapter extends JPanel { private String CMD_OK = "cmd.ok"/* NOI18N */;
private JButton okButton;
public ComboValueHolder ch, cl; // static
public CustomComboAdapter() { JPanel panel = new JPanel(new RiverLayout()); panel.setBorder(new EmptyBorder(6, 6, 6, 6)); riverFormBuilder(panel); add(panel); }
private void riverFormBuilder(JPanel panel) { ArrayList<String> countries = new ArrayList<String>(); countries.add("England"); countries.add("Scotland"); countries.add("Iereland"); countries.add("Nedtherlands"); countries.add("Flanders");
ArrayList<String> languages = new ArrayList<String>(); languages.add("Dutch"); languages.add("English"); languages.add("Spanish"); languages.add("German"); languages.add("Russian"); Dimension d = new Dimension(150, 20); Font userfont = new Font("Arial",1,12); ch = new ComboValueHolder(); ch.setStartselection("Make selection"); JComboBox countriesComboBoxes = ch.customComboBox(countries, d,userfont,Color.WHITE);
cl = new ComboValueHolder(); cl.setStartselection("Spanish"); JComboBox combolanguageBox = cl.customComboBox(languages, d,userfont,Color.WHITE); okButton = new JButton(); okButton.setText("ok"); okButton.setActionCommand(CMD_OK); okButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent event) { // windowAction(event); System.out.println("Combo selection country" + ch.getSelectionHolder().getValue()); System.out.println("Combo selection language" + cl.getSelectionHolder().getValue()); System.out.println(okButton.getActionCommand());
} });
panel.add("p", new JLabel("ComboBox Countries:")); panel.add("tab hfill", countriesComboBoxes); panel.add("tab tab", new JLabel("Selection:")); panel.add("tab tab tab hfill", BasicComponentFactory.createTextField(ch .getSelectionHolder()));
panel.add("p", new JLabel("ComboBox Languages:")); panel.add("tab hfill", combolanguageBox); panel.add("tab tab", new JLabel("Selection:")); panel.add("tab tab tab hfill", BasicComponentFactory.createTextField(cl .getSelectionHolder())); panel.add("p hfill", okButton); }
public static void main(String[] a) { JFrame f = new JFrame("Custom ComboBox"); f.setDefaultCloseOperation(2); f.add(new CustomComboAdapter()); f.pack(); f.setVisible(true); }
public class ComboValueHolder { String startselection;
ValueModel selectionHolder;
public ValueModel ComboHolder() { ValueModel selectionHolder = new ValueHolder(this.startselection); setSelectionHolder(selectionHolder); return selectionHolder; }
public JComboBox customComboBox(ArrayList countries, Dimension d, Font font, Color color) {
ComboBoxAdapter comboBoxAdapter = new ComboBoxAdapter(countries,ComboHolder()); JComboBox comboBox = new JComboBox(); comboBox.setPreferredSize(d); comboBox.setFont(font); comboBox.setBackground(color); comboBox.setModel(comboBoxAdapter); return comboBox; }
public String getStartselection() { return startselection; }
public void setStartselection(String startselection) { this.startselection = startselection; }
public ValueModel getSelectionHolder() { return selectionHolder; }
public void setSelectionHolder(ValueModel selectionHolder) { this.selectionHolder = selectionHolder; }
} }
Tags :: RiverLayout , JGoodies , Valueholder , bindings, formmodel , model , sample, samples, java
|