woensdag 9 januari 2008

custom input dialogs

JOptionPane is very easy to use. You can even pass your own JPanel to display on the dialog. This way you can easily retrieve user input. An example:


TerminologyVersionPanel panel = new TerminologyVersionPanel();
int answer = JOptionPane.showConfirmDialog(
parentComponent, panel, title, JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE
);
if (answer == JOptionPane.YES_OPTION){
// do stuff with the panel
System.out.println("your input: "+panel.getVersion()+" "+panel.getDate());
}
//otherwise input has been canceled


and an example for the panel


public class TerminologyVersionPanel extends JPanel {

/**
* txtfield for the version name
*/
private JTextField txtVersion;

/**
* datepicker for releasedate
*/
private JXDatePicker datePicker;

/**
* def ctor
*/
public TerminologyVersionPanel() {
super();
init();
}

private void init() {
// set layout here
setLayout(new GridLayout(0, 2));

// string title
add(new JLabel("version name"));
txtVersion = new JTextField();
add(txtVersion);

// datepicker
add(new JLabel("release date"));
datePicker = new JXDatePicker();
datePicker.setVisible(true);
add(datePicker);
}

// getters to retrieve input

public String getTxtVersion() {
return txtVersion.getText();
}

public Date getDate() {
return datePicker.getDate();
}

}




sources:
http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=2&t=016466
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JOptionPane.html
http://forum.java.sun.com/thread.jspa?threadID=680092&messageID=3966133

Geen opmerkingen:

Een reactie posten