Creating a Text Field That Mirrors the Value in the Anchor Cell in a JTable Component

In some spreadsheet applications, the value of the current cell (anchor) is mirrored in a separate and conveniently larger text field. Any changes to the current cell are immediately reflected in the text field and vice versa. This example demonstrates how to set up a mirror text field for the anchor cell in a table component.

In order to detect changes to the anchor cell, a selection-changed listener and a cell-value-changed listener must be added to the table.

See Getting the Anchor Cell in a JTable Component for more information about the anchor cell.

JTable table = new JTable(); // Add data // Create text field and add action JTextField textField = new JTextField(); textField.setAction(new UpdateAnchorAction(table)); // Add selection listener to table SelectionListener listener = new SelectionListener(table, textField); table.getSelectionModel().addListSelectionListener(listener); table.getColumnModel().getSelectionModel() .addListSelectionListener(listener); // Add value changed listener to table table.getModel().addTableModelListener(new MyTableModelListener(table, textField)); // Create a frame and add both components to the frame JFrame frame = new JFrame(); frame.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER); frame.getContentPane().add(textField, BorderLayout.NORTH); frame.pack(); frame.setVisible(true); public class SelectionListener implements ListSelectionListener { JTable table; JTextField textField; // It is necessary to keep the table since it is not possible // to determine the table from the event's source SelectionListener(JTable table, JTextField textField) { this.table = table; this.textField = textField; } // Update the text field whenever the anchor cell changes public void valueChanged(ListSelectionEvent e) { int rowIndex = table.getSelectionModel().getAnchorSelectionIndex(); int vColIndex = table.getColumnModel().getSelectionModel() .getAnchorSelectionIndex(); // Get the value and set the text field textField.setText((String)table.getValueAt(rowIndex, vColIndex)); } } public class MyTableModelListener implements TableModelListener { JTable table; JTextField textField; // It is necessary to keep the table since it is not possible // to determine the table from the event's source MyTableModelListener(JTable table, JTextField textField) { this.table = table; this.textField = textField; } // Update the text field whenever the value in the anchor cell changes public void tableChanged(TableModelEvent e) { // Get anchor cell location int rAnchor = table.getSelectionModel().getAnchorSelectionIndex(); int vcAnchor = table.getColumnModel().getSelectionModel() .getAnchorSelectionIndex(); // This method is defined in // Converting a Column Index Between the View and Model in a JTable Component int mcAnchor = toModel(table, vcAnchor); // Get affected rows and columns int firstRow = e.getFirstRow(); int lastRow = e.getLastRow(); int mColIndex = e.getColumn(); if (firstRow != TableModelEvent.HEADER_ROW && rAnchor >= firstRow && rAnchor <= lastRow && (mColIndex == TableModelEvent.ALL_COLUMNS || mColIndex == mcAnchor)) { // Set the text field with the new value textField.setText((String)table.getValueAt(rAnchor, vcAnchor)); } } } public class UpdateAnchorAction extends AbstractAction { JTable table; UpdateAnchorAction(JTable table) { super("Set Anchor"); this.table = table; } // Update the value in the anchor cell whenever the text field changes public void actionPerformed(ActionEvent evt) { JTextField textField = (JTextField)evt.getSource(); // Get anchor cell location int rAnchor = table.getSelectionModel().getAnchorSelectionIndex(); int vcAnchor = table.getColumnModel().getSelectionModel() .getAnchorSelectionIndex(); table.setValueAt(textField.getText(), rAnchor, vcAnchor); } }

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.