• R/O
  • SSH
  • HTTPS

Commit

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

作業部屋の使い方を試しています。


Commit MetaInfo

Revision98 (tree)
Time2014-11-24 23:02:22
Authortuna_p

Log Message

(empty log message)

Change Summary

Incremental Difference

--- branches/b1-swing/src/sample01/MainPanel.java (nonexistent)
+++ branches/b1-swing/src/sample01/MainPanel.java (revision 98)
@@ -0,0 +1,127 @@
1+// http://ateraimemo.com/Swing/CheckedRowColor.html
2+// JTableのセルにJCheckBoxを表示して行背景色を変更
3+
4+package sample01;
5+//-*- mode:java; encoding:utf-8 -*-
6+// vim:set fileencoding=utf-8:
7+//http://ateraimemo.com/Swing/CheckedRowColor.html
8+import java.awt.*;
9+import javax.swing.*;
10+import javax.swing.event.*;
11+import javax.swing.table.*;
12+
13+public final class MainPanel extends JPanel {
14+ private static final int BOOLEAN_COLUMN = 2;
15+ private static String[] columnNames = {"String", "Number", "Boolean"};
16+ private static Object[][] data = {
17+ {"aaa", 1, false}, {"bbb", 20, false},
18+ {"ccc", 2, false}, {"ddd", 3, false},
19+ {"aaa", 1, false}, {"bbb", 20, false},
20+ {"ccc", 2, false}, {"ddd", 3, false},
21+ };
22+
23+ private final DefaultTableModel model = new DefaultTableModel(data, columnNames) {
24+ @Override
25+ public Class<?> getColumnClass(int column) {
26+ return getValueAt(0, column).getClass();
27+ }
28+ @Override
29+ public boolean isCellEditable(int row, int col) {
30+ return col == BOOLEAN_COLUMN;
31+ }
32+ };
33+
34+ public MainPanel() {
35+ super(new BorderLayout());
36+ final JTable table = makeTable(model);
37+ //TEST: final JTable table = makeTable2(model);
38+ model.addTableModelListener(new TableModelListener() {
39+ @Override
40+ public void tableChanged(TableModelEvent e) {
41+ if (e.getType() == TableModelEvent.UPDATE) {
42+ //System.out.println("TableModel: tableChanged");
43+ rowRepaint(table, table.convertRowIndexToView(e.getFirstRow()));
44+ }
45+ }
46+ });
47+ table.setAutoCreateRowSorter(true);
48+ table.setFillsViewportHeight(true);
49+ table.setShowGrid(false);
50+ table.setIntercellSpacing(new Dimension());
51+ table.setRowSelectionAllowed(true);
52+ //table.setSurrendersFocusOnKeystroke(true);
53+ //table.putClientProperty("JTable.autoStartsEdit", false);
54+ add(new JScrollPane(table));
55+ setPreferredSize(new Dimension(320, 240));
56+ }
57+ public static JTable makeTable(final DefaultTableModel model) {
58+ return new JTable(model) {
59+ @Override
60+ public Component prepareEditor(TableCellEditor editor, int row, int column) {
61+ Component cmp = super.prepareEditor(editor, row, column);
62+ if (convertColumnIndexToModel(column) == BOOLEAN_COLUMN) {
63+ //System.out.println("JTable: prepareEditor");
64+ JCheckBox c = (JCheckBox) cmp;
65+ c.setBackground(c.isSelected() ? Color.ORANGE : getBackground());
66+ }
67+ return cmp;
68+ }
69+ @Override
70+ public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
71+ Component c = super.prepareRenderer(renderer, row, column);
72+ Boolean isChecked = (Boolean) model.getValueAt(convertRowIndexToModel(row), BOOLEAN_COLUMN);
73+ c.setForeground(getForeground());
74+ c.setBackground(isChecked ? Color.ORANGE : getBackground());
75+ return c;
76+ }
77+ };
78+ }
79+ public static JTable makeTable2(final DefaultTableModel model) {
80+ final JTable table = new JTable(model);
81+ TableColumnModel columns = table.getColumnModel();
82+ TableCellRenderer r = new RowColorTableRenderer();
83+ for (int i = 0; i < columns.getColumnCount(); i++) {
84+ columns.getColumn(i).setCellRenderer(r);
85+ }
86+ return table;
87+ }
88+ static class RowColorTableRenderer extends DefaultTableCellRenderer {
89+ @Override
90+ public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
91+ Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
92+ TableModel model = table.getModel();
93+ Boolean isChecked = (Boolean) model.getValueAt(table.convertRowIndexToModel(row), BOOLEAN_COLUMN);
94+ c.setForeground(table.getForeground());
95+ c.setBackground(isChecked ? Color.ORANGE : table.getBackground());
96+ return c;
97+ }
98+ }
99+ private static void rowRepaint(JTable table, int row) {
100+ Rectangle r = table.getCellRect(row, 0, true);
101+ //r.height = table.getRowHeight();
102+ r.width = table.getWidth();
103+ table.repaint(r);
104+ }
105+
106+ public static void main(String... args) {
107+ EventQueue.invokeLater(new Runnable() {
108+ @Override
109+ public void run() {
110+ createAndShowGUI();
111+ }
112+ });
113+ }
114+ public static void createAndShowGUI() {
115+ try {
116+ UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
117+ } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
118+ ex.printStackTrace();
119+ }
120+ JFrame frame = new JFrame("CheckedRowColor");
121+ frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
122+ frame.getContentPane().add(new MainPanel());
123+ frame.pack();
124+ frame.setLocationRelativeTo(null);
125+ frame.setVisible(true);
126+ }
127+}