/
MainPanel.java
173 lines (148 loc) · 4.99 KB
/
MainPanel.java
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// -*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
// @homepage@
package example;
import java.awt.*;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
public final class MainPanel extends JPanel {
private MainPanel() {
super(new BorderLayout());
RowDataModel model = new RowDataModel();
JTable table = new JTable(model) {
private final Color evenColor = new Color(0xFA_FA_FA);
@Override public Component prepareRenderer(TableCellRenderer tcr, int row, int column) {
Component c = super.prepareRenderer(tcr, row, column);
if (isRowSelected(row)) {
c.setForeground(getSelectionForeground());
c.setBackground(getSelectionBackground());
} else {
c.setForeground(getForeground());
c.setBackground(row % 2 == 0 ? evenColor : getBackground());
}
return c;
}
};
TableColumn col = table.getColumnModel().getColumn(0);
col.setMinWidth(60);
col.setMaxWidth(60);
col.setResizable(false);
model.addRowData(new RowData("Name 1", "comment..."));
model.addRowData(new RowData("Name 2", "Test"));
model.addRowData(new RowData("Name d", "ee"));
model.addRowData(new RowData("Name c", "Test cc"));
model.addRowData(new RowData("Name b", "Test bb"));
model.addRowData(new RowData("Name a", "ff"));
model.addRowData(new RowData("Name 0", "Test aa"));
table.setAutoCreateRowSorter(true);
table.setFillsViewportHeight(true);
table.setComponentPopupMenu(new TablePopupMenu());
add(new JScrollPane(table));
setPreferredSize(new Dimension(320, 240));
}
public static void main(String[] args) {
EventQueue.invokeLater(MainPanel::createAndShowGui);
}
private static void createAndShowGui() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException ignored) {
Toolkit.getDefaultToolkit().beep();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException ex) {
ex.printStackTrace();
return;
}
JFrame frame = new JFrame("@title@");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.getContentPane().add(new MainPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
class RowDataModel extends DefaultTableModel {
private static final ColumnContext[] COLUMN_ARRAY = {
new ColumnContext("No.", Integer.class, false),
new ColumnContext("Name", String.class, true),
new ColumnContext("Comment", String.class, true)
};
private int number;
public void addRowData(RowData t) {
Object[] obj = {number, t.getName(), t.getComment()};
super.addRow(obj);
number++;
}
@Override public boolean isCellEditable(int row, int col) {
return COLUMN_ARRAY[col].isEditable;
}
@Override public Class<?> getColumnClass(int column) {
return COLUMN_ARRAY[column].columnClass;
}
@Override public int getColumnCount() {
return COLUMN_ARRAY.length;
}
@Override public String getColumnName(int column) {
return COLUMN_ARRAY[column].columnName;
}
private static class ColumnContext {
public final String columnName;
public final Class<?> columnClass;
public final boolean isEditable;
protected ColumnContext(String columnName, Class<?> columnClass, boolean isEditable) {
this.columnName = columnName;
this.columnClass = columnClass;
this.isEditable = isEditable;
}
}
}
class RowData {
private final String name;
private final String comment;
protected RowData(String name, String comment) {
this.name = name;
this.comment = comment;
}
// public void setName(String str) {
// name = str;
// }
// public void setComment(String str) {
// comment = str;
// }
public String getName() {
return name;
}
public String getComment() {
return comment;
}
}
final class TablePopupMenu extends JPopupMenu {
private final JMenuItem delete;
/* default */ TablePopupMenu() {
super();
add("add").addActionListener(e -> {
JTable table = (JTable) getInvoker();
RowDataModel model = (RowDataModel) table.getModel();
model.addRowData(new RowData("New row", ""));
Rectangle r = table.getCellRect(model.getRowCount() - 1, 0, true);
table.scrollRectToVisible(r);
});
addSeparator();
delete = add("delete");
delete.addActionListener(e -> {
JTable table = (JTable) getInvoker();
DefaultTableModel model = (DefaultTableModel) table.getModel();
int[] selection = table.getSelectedRows();
for (int i = selection.length - 1; i >= 0; i--) {
model.removeRow(table.convertRowIndexToModel(selection[i]));
}
});
}
@Override public void show(Component c, int x, int y) {
if (c instanceof JTable) {
delete.setEnabled(((JTable) c).getSelectedRowCount() > 0);
super.show(c, x, y);
}
}
}