JGoodies - binding - sample
Samstag 20 März 2010 at 7:05 pm. Stichwörter: java, jgoodies
Ich bin dran in einer Swing GUI mit JGoodies das Binding zu verwenden.
Hier möchte ich ein kleines Beispiel zeigen.
1) Es wird ein Text eines Model in zwei JTextFields verwendet.
2) Ein JTextField wird abhängig von einer JCheckbox enabled bzw. disabeld.
EDIT:
3) Es wird eine JListe angezeigt. Die aktuelle Auswahl der JList wird in einem JTextField angezeigt.
Zunächst das verwendete Model.
public class MyModel extends Model {
/** serialVersionUID */
private static final long serialVersionUID = 3496603843953848123L;
public static final String PROPERTY_theText = "theText";
public static final String PROPERTY_theBool = "theBool";
private String theText = "Hallo Text";
private boolean theBool = false;
private List<String> strings = new ArrayList<String>();
public MyModel(){
strings.add("Swing");
strings.add("SWT");
strings.add("HTML");
strings.add("Flash");
strings.add("QT");
}
/**
* @return the strings
*/
public List<String> getStrings() {
return strings;
}
/**
* @param strings the strings to set
*/
public void setStrings(List<String> strings) {
this.strings = strings;
}
/**
* @param theText the theText to set
*/
public void setTheText(String theText) {
String old = this.theText;
this.theText = theText;
firePropertyChange(MyModel.PROPERTY_theText, old, theText );
}
/**
* @return the theText
*/
public String getTheText() {
return theText;
}
/**
* @param theBool the theBool to set
*/
public void setTheBool(boolean theBool) {
boolean old = this.theBool;
this.theBool = theBool;
firePropertyChange(MyModel.PROPERTY_theBool, old, theBool);
}
/**
* @return the theBool
*/
public boolean isTheBool() {
return theBool;
}
}Und hier die GUI dazu.
public class Main extends JFrame {
/**
* @param args
*/
public static void main(String[] args) {
Main min = new Main();
min.setSize(400, 400);
min.setVisible(true);
}
private MyModel model;
private JTextField tfString;
private JTextField tfString2;
private JCheckBox chkCheck;
private JList list;
private JTextField tfListChoice;
public Main(){
setLayout(new FlowLayout());
model = new MyModel();
BeanAdapter<MyModel> adapter = new BeanAdapter<MyModel>(model, true);
ValueModel vmText = adapter.getValueModel(MyModel.PROPERTY_theText);
ValueModel vmBool = adapter.getValueModel(MyModel.PROPERTY_theBool);
SelectionInList<String> selectionInList = new SelectionInList<String>(model.getStrings());
tfString = BasicComponentFactory.createTextField(vmText, true);
tfString.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, Color.black));
tfString2 = BasicComponentFactory.createTextField(vmText, true);
tfString2.setBorder(BorderFactory.createMatteBorder(0, 0, 5, 0, Color.black));
chkCheck = BasicComponentFactory.createCheckBox(vmBool, "Check");
list = BasicComponentFactory.createList(selectionInList);
tfListChoice = BasicComponentFactory.createTextField(selectionInList.getSelectionHolder());
tfListChoice.setColumns(30);
Bindings.bind(tfString2, vmText);
Bindings.bind(tfString, vmText);
Bindings.bind(tfString, "enabled", vmBool);
Bindings.bind(list, selectionInList);
Bindings.bind(tfListChoice, selectionInList);
add(tfString);
add(chkCheck);
add(tfString2);
add(list);
add(tfListChoice);
// Window Close
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addWindowListener(new WindowCloseListsner());
}
class WindowCloseListsner extends WindowAdapter
{
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
}
Nachtrag: 19 Juni 2010
Sollen die Daten z.B. einer JCombobox upgedated werden, muss dies auf dem Model geschehen.
Aus dem Grund bietet es sich an die SelectionInList als Class Member festzulegen.
SelectionInList<String> vmListCbo
Nun lässt sich durch dessen Commands das Model anpassen.
Die JCombobox wird nun automatisch upgedated.
vmListCbo.clearSelection();
vmListCbo.setList(UtilFileHandling.getSheets(filename));
vmListCbo.setSelectionIndex(0);
Kein Kommentar