java 达人,
最近在开发一个 java 模块,用到了 JTable。现在对 JTable 里的单元格的操作中,在双击进入单元格后,单元格的内容不是全选中状态。
请问有啥办法使得双击进入单元格后,单元格的内容处于全选中状态?如下面的图片所示
十分感谢!
下面是已经写好的代码:
- Java code
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(); }}