-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTableColumnAdjusterDemo.java
More file actions
140 lines (113 loc) · 4.02 KB
/
TableColumnAdjusterDemo.java
File metadata and controls
140 lines (113 loc) · 4.02 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import java.awt.*;
import java.awt.event.*;
//import java.beans.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class TableColumnAdjusterDemo extends JPanel
implements ItemListener
{
private TableColumnAdjuster tca;
TableColumnAdjusterDemo()
{
setLayout( new BorderLayout(10, 10) );
setBorder( new EmptyBorder(10, 10, 10, 10) );
JComponent center = createCenterPanel();
JComponent south = createSouthPanel();
add(center, BorderLayout.CENTER);
add(south, BorderLayout.SOUTH);
}
public JComponent createCenterPanel()
{
String[] columnNames = {"First Name", "Last Name", "Age", "Address"};
DefaultTableModel model = new DefaultTableModel(columnNames, 0)
{
@Override
public Class getColumnClass(int column)
{
return (column == 4) ? Boolean.class : String.class;
}
};
String[] row1 = {"Homer", "Simpson", "54", "Somewhere in Hollywoodasfsdf"};
String[] row2 = {"Herman", "Schwarzenegger", "50", "California"};
for (int i = 0; i < 1; i++)
{
model.addRow(row1);
model.addRow(row2);
}
model.setRowCount(5);
JTable table = new JTable( model );
// table.setColumnSelectionAllowed(true);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
TableColumnModel tcm = table.getColumnModel();
tcm.getColumn(0).setPreferredWidth(25);
tcm.getColumn(1).setPreferredWidth(25);
tcm.getColumn(2).setPreferredWidth(25);
tcm.getColumn(3).setPreferredWidth(25);
// JTableHeader header = table.getTableHeader();
// header.setPreferredSize(new Dimension(0, 0));
tca = new TableColumnAdjuster( table, 6 );
tca.setColumnDataIncluded(false);
tca.adjustColumns();
// table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
// TableColumn tc = tcm.getColumn(1);
// tc.setWidth(tc.getWidth() + 25);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane= new JScrollPane( table );
table.setAutoCreateRowSorter(true);
return scrollPane;
}
private JComponent createSouthPanel()
{
JPanel panel = new JPanel( new GridLayout(0, 2) );
JCheckBox columnHeaderIncluded = new JCheckBox("Column Header Included");
columnHeaderIncluded.setSelected(true);
columnHeaderIncluded.addItemListener( this );
panel.add( columnHeaderIncluded );
JCheckBox onlyAdjustLarger = new JCheckBox( "Only Adjust Larger" );
onlyAdjustLarger.setSelected(true);
onlyAdjustLarger.addItemListener( this );
panel.add( onlyAdjustLarger );
JCheckBox columnDataIncluded = new JCheckBox( "Column Data Included" );
columnDataIncluded.setSelected(true);
columnDataIncluded.addItemListener( this );
panel.add( columnDataIncluded );
JCheckBox dynamicAdjustment = new JCheckBox( "Dynamic Adjustment" );
dynamicAdjustment.addItemListener( this );
panel.add( dynamicAdjustment );
return panel;
}
public void itemStateChanged(ItemEvent e)
{
JCheckBox checkBox = (JCheckBox)e.getSource();
String command = checkBox.getText();
if ("Column Header Included".equals(command))
tca.setColumnHeaderIncluded( checkBox.isSelected() );
if ("Column Data Included".equals(command))
tca.setColumnDataIncluded( checkBox.isSelected() );
if ("Only Adjust Larger".equals(command))
tca.setOnlyAdjustLarger( checkBox.isSelected() );
if ("Dynamic Adjustment".equals(command))
tca.setDynamicAdjustment( checkBox.isSelected() );
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI()
{
JFrame frame = new JFrame("Table Column Adjuster");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new TableColumnAdjusterDemo() );
frame.setSize(500, 300);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}