http://bbs.csdn.net/topics/390195204
————————————————————————————————————————
java 达人,
最近在开发一个 java 模块,用到了 JTable。现在对 JTable 里的单元格的操作中,在双击进入单元格后,单元格的内容不是全选中状态。
请问有啥办法使得双击进入单元格后,单元格的内容处于全选中状态?如下面的图片所示
十分感谢!
下面是已经写好的代码:
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Vector; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.ListSelectionModel; import javax.swing.ScrollPaneLayout; import javax.swing.SwingConstants; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableCellRenderer; import javax.swing.table.TableColumn; public class GlobalVari_6 extends JFrame { private static final long serialVersionUID = 1L; private static double select = 0 ; private static int rowIndex = 0 ; public JTable table = null ; // 表格方法使用 public GlobalVari_6() { init(); this .setTitle( "Global Variables" ); this .setSize( new Dimension( 650 , 400 )); this .setLocationRelativeTo( null ); this .setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); this .setVisible( true ); // this.setResizable(false); } private void init() { Vector<String> colHeader = new Vector<String>(); Vector<Vector<String>> dataVec = new Vector<Vector<String>>(); colHeader.add( " Variable" ); colHeader.add( "Value " ); colHeader.add( "Time " ); table = new JTable(dataVec, colHeader) { private static final long serialVersionUID = 1L; public boolean isCellEditable( int row, int column) { if (column > 1 ) { return false ; } else { return true ; } } }; // 2设置表头行高 table.getTableHeader().setPreferredSize( new Dimension( 0 , 20 )); // 3设置表内容行高 table.setRowHeight( 20 ); // 4设置单选模式 table.getSelectionModel().setSelectionMode( ListSelectionModel.SINGLE_SELECTION); // 5设置单元格不可拖动 table.getTableHeader().setReorderingAllowed( false ); // 6设置不可改变列宽 table.getTableHeader().setResizingAllowed( false ); // 7设置列宽 table.getColumnModel().getColumn( 0 ).setPreferredWidth( 95 ); table.getColumnModel().getColumn( 1 ).setPreferredWidth( 55 ); table.getColumnModel().getColumn( 2 ).setPreferredWidth( 30 ); // 注意索引越界 DefaultTableCellRenderer right = new DefaultTableCellRenderer(); // right.setHorizontalAlignment(JLabel.RIGHT); right.setHorizontalAlignment(SwingConstants.RIGHT); table.getColumnModel().getColumn( 1 ).setCellRenderer(right); table.getColumnModel().getColumn( 2 ).setCellRenderer(right); // table.getColumn(table.getColumnName(1)).setHeaderRenderer(right); // table.getTableHeader().setDefaultRenderer(new HeaderRenderer(table)); ((DefaultTableCellRenderer) table.getTableHeader().getDefaultRenderer()) .setHorizontalAlignment(JLabel.RIGHT); TableColumn column = table.getColumnModel().getColumn( 0 ); MultiLineHeaderRenderer headerRenderer = new MultiLineHeaderRenderer( SwingConstants.LEFT, SwingConstants.BOTTOM); column.setHeaderRenderer(headerRenderer); /* * column = table.getColumnModel().getColumn(1); headerRenderer = new * MultiLineHeaderRenderer( SwingConstants.LEFT, SwingConstants.BOTTOM); * column.setHeaderRenderer(headerRenderer); */ final JButton buttonAdd = new JButton( "Add" ); final JButton buttonDel = new JButton( "Delete" ); final JButton buttonCls = new JButton( "Close" ); rowIndex = table.getSelectedRow(); System.out.println( "index:" + rowIndex); // 监听事件 table.getSelectionModel().addListSelectionListener( new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { if (e.getValueIsAdjusting()) { // 连续操作 rowIndex = table.getSelectedRow(); if (rowIndex != - 1 ) { System.out.println( "表格行被选中" + rowIndex); buttonDel.setEnabled( true ); select = 1 ; } } } }); JScrollPane scrollPane = new JScrollPane(); scrollPane.setLayout( new ScrollPaneLayout()); scrollPane.setViewportView(table); scrollPane.setBounds( 5 , 5 , 515 , 350 ); this .getContentPane().add(scrollPane); buttonAdd.setEnabled( true ); buttonDel.setEnabled( false ); buttonCls.setEnabled( true ); JPanel panel = new JPanel(); panel.setBounds( 520 , 5 , 10 , 350 ); this .getContentPane().add(panel); panel.setLayout( null ); panel.add(buttonAdd); buttonAdd.setSize( 100 , 22 ); buttonAdd.setLocation( 530 , 6 ); panel.add(buttonDel); buttonDel.setSize( 100 , 22 ); buttonDel.setLocation( 530 , 40 ); panel.add(buttonCls); buttonCls.setSize( 100 , 22 ); buttonCls.setLocation( 530 , 322 ); buttonAdd.addMouseListener( new MouseListener() { public void mouseClicked(MouseEvent e) { DefaultTableModel model = (DefaultTableModel) table.getModel(); int rownum = table.getRowCount() + 1 ; boolean exist = true ; for ( int i = 0 ; i < model.getRowCount(); i++) { if (model.getValueAt(i, 0 ).equals( "gvar" + rownum)) { exist = false ; break ; } } if (exist == true ) model.addRow( new Object[] { "gvar" + rownum, "" , new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ) .format( new Date()) }); } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } }); buttonDel.addMouseListener( new MouseListener() { public void mouseClicked(MouseEvent e) { if (select == 1 ) { DefaultTableModel model = (DefaultTableModel) table .getModel(); model.removeRow(rowIndex); select = 0 ; buttonDel.setEnabled( false ); } } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } }); buttonCls.addMouseListener( new MouseListener() { public void mouseClicked(MouseEvent e) { setVisible( false ); } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } }); this .pack(); } public static void main(String[] args) { new GlobalVari_6(); } } |
在你的init较后面的地方加。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
TableCellEditor tce = table.getDefaultEditor(String. class ); if (tce instanceof DefaultCellEditor) { DefaultCellEditor dce = (DefaultCellEditor) tce; final Component c = dce.getComponent(); c.addFocusListener( new FocusListener() { @Override public void focusGained(FocusEvent arg0) { System.out.println( "focusGained" ); if (c instanceof JTextComponent) { ((JTextComponent) c).selectAll(); } } @Override public void focusLost(FocusEvent arg0) { } }); } |
但是如果你的JTable里某列自己加过JTextField等。
例如:
写过类似这样的
1
|
column.setCellEditor( new DefaultCellEditor(textField)); |
那么下面那段也许也要加上。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
int count = table.getColumnModel().getColumnCount(); for ( int i = 0 ; i < count; i++) { TableColumn tableColumn = table.getColumnModel().getColumn(i); if (tableColumn.getCellEditor() == null ) { continue ; } if (tableColumn.getCellEditor() instanceof DefaultCellEditor) { DefaultCellEditor dce = (DefaultCellEditor) tableColumn .getCellEditor(); final Component c = dce.getComponent(); c.addFocusListener( new FocusListener() { @Override public void focusGained(FocusEvent arg0) { if (c instanceof JTextComponent) { ((JTextComponent) c).selectAll(); } } @Override public void focusLost(FocusEvent arg0) { } }); } } |
——————————————————————————————————
傲轩游戏网
傲轩游戏网