|
| 1 | +import java.awt.*; |
| 2 | +import java.awt.event.*; |
| 3 | +//import java.beans.*; |
| 4 | +import javax.swing.*; |
| 5 | +import javax.swing.border.*; |
| 6 | +import javax.swing.event.*; |
| 7 | +import javax.swing.table.*; |
| 8 | + |
| 9 | +public class TableColumnAdjusterDemo extends JPanel |
| 10 | + implements ItemListener |
| 11 | +{ |
| 12 | + private TableColumnAdjuster tca; |
| 13 | + |
| 14 | + TableColumnAdjusterDemo() |
| 15 | + { |
| 16 | + setLayout( new BorderLayout(10, 10) ); |
| 17 | + setBorder( new EmptyBorder(10, 10, 10, 10) ); |
| 18 | + |
| 19 | + JComponent center = createCenterPanel(); |
| 20 | + JComponent south = createSouthPanel(); |
| 21 | + |
| 22 | + add(center, BorderLayout.CENTER); |
| 23 | + add(south, BorderLayout.SOUTH); |
| 24 | + } |
| 25 | + |
| 26 | + public JComponent createCenterPanel() |
| 27 | + { |
| 28 | + String[] columnNames = {"First Name", "Last Name", "Age", "Address"}; |
| 29 | + DefaultTableModel model = new DefaultTableModel(columnNames, 0) |
| 30 | + { |
| 31 | + @Override |
| 32 | + public Class getColumnClass(int column) |
| 33 | + { |
| 34 | + return (column == 4) ? Boolean.class : String.class; |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + String[] row1 = {"Homer", "Simpson", "54", "Somewhere in Hollywoodasfsdf"}; |
| 39 | + String[] row2 = {"Herman", "Schwarzenegger", "50", "California"}; |
| 40 | + |
| 41 | + for (int i = 0; i < 1; i++) |
| 42 | + { |
| 43 | + model.addRow(row1); |
| 44 | + model.addRow(row2); |
| 45 | + } |
| 46 | + |
| 47 | + model.setRowCount(5); |
| 48 | + |
| 49 | + JTable table = new JTable( model ); |
| 50 | +// table.setColumnSelectionAllowed(true); |
| 51 | + table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); |
| 52 | +// table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); |
| 53 | + TableColumnModel tcm = table.getColumnModel(); |
| 54 | + tcm.getColumn(0).setPreferredWidth(25); |
| 55 | + tcm.getColumn(1).setPreferredWidth(25); |
| 56 | + tcm.getColumn(2).setPreferredWidth(25); |
| 57 | + tcm.getColumn(3).setPreferredWidth(25); |
| 58 | + |
| 59 | +// JTableHeader header = table.getTableHeader(); |
| 60 | +// header.setPreferredSize(new Dimension(0, 0)); |
| 61 | + tca = new TableColumnAdjuster( table, 6 ); |
| 62 | + tca.setColumnDataIncluded(false); |
| 63 | + tca.adjustColumns(); |
| 64 | +// table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); |
| 65 | +// TableColumn tc = tcm.getColumn(1); |
| 66 | +// tc.setWidth(tc.getWidth() + 25); |
| 67 | + |
| 68 | + table.setPreferredScrollableViewportSize(table.getPreferredSize()); |
| 69 | + JScrollPane scrollPane= new JScrollPane( table ); |
| 70 | + |
| 71 | + table.setAutoCreateRowSorter(true); |
| 72 | + |
| 73 | + return scrollPane; |
| 74 | + } |
| 75 | + |
| 76 | + private JComponent createSouthPanel() |
| 77 | + { |
| 78 | + JPanel panel = new JPanel( new GridLayout(0, 2) ); |
| 79 | + |
| 80 | + JCheckBox columnHeaderIncluded = new JCheckBox("Column Header Included"); |
| 81 | + columnHeaderIncluded.setSelected(true); |
| 82 | + columnHeaderIncluded.addItemListener( this ); |
| 83 | + panel.add( columnHeaderIncluded ); |
| 84 | + |
| 85 | + JCheckBox onlyAdjustLarger = new JCheckBox( "Only Adjust Larger" ); |
| 86 | + onlyAdjustLarger.setSelected(true); |
| 87 | + onlyAdjustLarger.addItemListener( this ); |
| 88 | + panel.add( onlyAdjustLarger ); |
| 89 | + |
| 90 | + JCheckBox columnDataIncluded = new JCheckBox( "Column Data Included" ); |
| 91 | + columnDataIncluded.setSelected(true); |
| 92 | + columnDataIncluded.addItemListener( this ); |
| 93 | + panel.add( columnDataIncluded ); |
| 94 | + |
| 95 | + JCheckBox dynamicAdjustment = new JCheckBox( "Dynamic Adjustment" ); |
| 96 | + dynamicAdjustment.addItemListener( this ); |
| 97 | + panel.add( dynamicAdjustment ); |
| 98 | + |
| 99 | + return panel; |
| 100 | + } |
| 101 | + |
| 102 | + public void itemStateChanged(ItemEvent e) |
| 103 | + { |
| 104 | + JCheckBox checkBox = (JCheckBox)e.getSource(); |
| 105 | + String command = checkBox.getText(); |
| 106 | + |
| 107 | + if ("Column Header Included".equals(command)) |
| 108 | + tca.setColumnHeaderIncluded( checkBox.isSelected() ); |
| 109 | + |
| 110 | + if ("Column Data Included".equals(command)) |
| 111 | + tca.setColumnDataIncluded( checkBox.isSelected() ); |
| 112 | + |
| 113 | + if ("Only Adjust Larger".equals(command)) |
| 114 | + tca.setOnlyAdjustLarger( checkBox.isSelected() ); |
| 115 | + |
| 116 | + if ("Dynamic Adjustment".equals(command)) |
| 117 | + tca.setDynamicAdjustment( checkBox.isSelected() ); |
| 118 | + } |
| 119 | + |
| 120 | + |
| 121 | + public static void main(String[] args) |
| 122 | + { |
| 123 | + SwingUtilities.invokeLater(new Runnable() { |
| 124 | + public void run() { |
| 125 | + createAndShowGUI(); |
| 126 | + } |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + public static void createAndShowGUI() |
| 131 | + { |
| 132 | + JFrame frame = new JFrame("Table Column Adjuster"); |
| 133 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 134 | + frame.add( new TableColumnAdjusterDemo() ); |
| 135 | + frame.setSize(500, 300); |
| 136 | + frame.pack(); |
| 137 | + frame.setLocationRelativeTo(null); |
| 138 | + frame.setVisible(true); |
| 139 | + } |
| 140 | +} |
0 commit comments