TableDialogEditDemopublic class TableDialogEditDemo extends JFrame This is like TableEditDemo, except that it substitutes a
Favorite Color column for the Last Name column and specifies
a custom cell renderer and editor for the color data. |
Fields Summary |
---|
private boolean | DEBUG |
Constructors Summary |
---|
public TableDialogEditDemo()
super("TableDialogEditDemo");
MyTableModel myModel = new MyTableModel();
JTable table = new JTable(myModel);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Set up renderer and editor for the Favorite Color column.
setUpColorRenderer(table);
setUpColorEditor(table);
//Set up real input validation for integer data.
setUpIntegerEditor(table);
//Add the scroll pane to this window.
getContentPane().add(scrollPane, BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
|
Methods Summary |
---|
public static void | main(java.lang.String[] args)
TableDialogEditDemo frame = new TableDialogEditDemo();
frame.pack();
frame.setVisible(true);
| private void | setUpColorEditor(javax.swing.JTable table)
//First, set up the button that brings up the dialog.
final JButton button = new JButton("") {
public void setText(String s) {
//Button never shows text -- only color.
}
};
button.setBackground(Color.white);
button.setBorderPainted(false);
button.setMargin(new Insets(0,0,0,0));
//Now create an editor to encapsulate the button, and
//set it up as the editor for all Color cells.
final ColorEditor colorEditor = new ColorEditor(button);
table.setDefaultEditor(Color.class, colorEditor);
//Set up the dialog that the button brings up.
final JColorChooser colorChooser = new JColorChooser();
ActionListener okListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
colorEditor.currentColor = colorChooser.getColor();
}
};
final JDialog dialog = JColorChooser.createDialog(button,
"Pick a Color",
true,
colorChooser,
okListener,
null); //XXXDoublecheck this is OK
//Here's the code that brings up the dialog.
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
button.setBackground(colorEditor.currentColor);
colorChooser.setColor(colorEditor.currentColor);
//Without the following line, the dialog comes up
//in the middle of the screen.
//dialog.setLocationRelativeTo(button);
dialog.show();
}
});
| private void | setUpColorRenderer(javax.swing.JTable table)
table.setDefaultRenderer(Color.class,
new ColorRenderer(true));
| private void | setUpIntegerEditor(javax.swing.JTable table)
//Set up the editor for the integer cells.
final WholeNumberField integerField = new WholeNumberField(0, 5);
integerField.setHorizontalAlignment(WholeNumberField.RIGHT);
DefaultCellEditor integerEditor =
new DefaultCellEditor(integerField) {
//Override DefaultCellEditor's getCellEditorValue method
//to return an Integer, not a String:
public Object getCellEditorValue() {
return new Integer(integerField.getValue());
}
};
table.setDefaultEditor(Integer.class, integerEditor);
|
|