1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.HeadlessException;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.table.TableCellEditor;
/**
* @author bebe
*/
public class EnableDisableJTable extends JFrame implements ActionListener {
JTable myTable = null;
JButton myButton = null;
public EnableDisableJTable(String s) throws HeadlessException {
super(s);
/* layout components on the ContenPane */
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 5, 5, 5);
gbc.weightx = gbc.weighty = 1.;
gbc.fill = GridBagConstraints.BOTH;
/* some usual codes... */
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationByPlatform(true);
setSize(400, 300);
/* add the table to the ContentPane */
myTable = new JTable(new MyTableModel());
add(new JScrollPane(myTable), gbc);
/* add a button to enable/disable the table */
myButton = new JButton("Click me to disable JTable");
myButton.setEnabled(false);
myButton.addActionListener(this);
/* add the button to the ContentPane */
gbc.weightx = gbc.weighty = gbc.gridx = gbc.insets.top = 0;
gbc.fill = GridBagConstraints.NONE;
gbc.anchor = GridBagConstraints.EAST;
add(myButton, gbc);
}
/* */
public void actionPerformed(ActionEvent e) {
boolean isEnabled = myTable.isEnabled();
if (isEnabled) {
myButton.setText("Click me to enable JTable");
} else {
myButton.setText("Click me to disable JTable");
}
// first stop editing. But what if editing doesn't want to stop?
TableCellEditor tce = myTable.getCellEditor();
if (tce != null) {
tce.stopCellEditing();
}
// disable the table. from now, use !isEnabled i.o. isEnabled
myTable.setEnabled(!isEnabled);
/* clear the selection, so you won't see anymore which row was previously selected.
or, (perhaps better) save it and restore it when you enable/disable your table.
*/
myTable.clearSelection();
/* disable/enable reordering... */
myTable.getTableHeader().setReorderingAllowed(!isEnabled);
/* you can also enable/disable autoResizeMode, but you'll have to save the mode and restore it */
/* now, set the background color depending on the JTable's state. */
if (!isEnabled) {
myTable.setBackground((Color) UIManager.get("TextField.background"));
/* Although brighter and darker are inverse operations, the results of a series of invocations of these
two methods might be inconsistent because of rounding errors. */
/* change also the tableHeader's background? here we "restore" it */
myTable.getTableHeader().setBackground(myTable.getTableHeader().getBackground().brighter());
} else {
myTable.setBackground((Color) UIManager.get("TextField.inactiveBackground"));
/* change also the tableHeader's background to make it darker */
myTable.getTableHeader().setBackground(myTable.getTableHeader().getBackground().darker());
}
}
/** the main method */
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run() {
new EnableDisableJTable("1").setVisible(true);
}
});
}
} |
Partager