How to Mirror Edits across a Table Cell and Text Field - java

The desired behavior is akin to the mirrored text editing field provided in Excel when a given cell is selected, allowing more space to view the contents of the cell. I have a JTable with 5 columns and n rows. Column 2 holds expressions that can be arbitrarily long, thus I'd like to provide a separate JTextField to work with for editing the contents of the expression cell per row. The other fields are directly editable in the table. When the user clicks on a field in column 2, however, I want to send them to the text field. Any contents preexisting in the cell should be appear in the text field and additional edits in the text field should be mirrored in the table cell. Likewise, if someone double-clicks on the cell and edits it directly, I want those changes reflected in the text field. Thus, the user can choose to edit in either space and both are updated. Ideally, they are updated per keystroke, but update upon hitting return is acceptable.
So, far I've got the JTable, TableModel, TableModelListener, JTextField, ListSelectionListener, and AbstractAction, working together to provide most of the functionality described above. I'm missing the reflection of direct table cell edits to the text field and per-keystoke updates.
Are their ideas on how best to construct this behavior?

Well, if you want to get data from the table to the cell then you add the code to your TableModel's setValueAt() function, which should run when the user changes the content in an editable cell. I don't think that will update per-keystroke though.
If you want to move data from the textbox to the table cell use code like this
myJTextField.getDocument().addDocumentListener(new MyDocumentListener());
Where MyDocumentListener is an implementation of the javax.swing.event.DocumentListener interface
That will get you per-keystroke updates from the box to the table. But for the other way around it's a bit trickier.
There are two ways you might be able to go about doing it
1) Add a key listener to the table, and when the user starts typing check to see what table element is active, and intercept keystrokes as they type. That's kind of messy, though.
2) Another option might be to try to grab or replace the component that the table is using to actually let the user make the changes. I think that JTable actually allows you to change the editor component if you dig around.

Related

How to hide the Jtable column data?

I want to hide data of a column of Jtable, but not hide the column view,just its data.
The column contain data about profit and the customer doesn't want to show the profit but in my code I use the values of this column and get it when the user select specified row.
How do I achieve the customer need and still be able to get the values of this column when selecting it after hiding its data(displaying column as empty but still has its values)?
You need to remove the TableColumn from the TableColumnModel of the JTable. For example:
table.removeColumn( table.getColumn(...) );
Now the column will not display in the table, but the data is still available in the TableModel. to access the data you use:
table.getModel().getValueAt(...);
You could try a subclassing DefaultTableCellRenderer, and call setCellRenderer. When the relevant column number is passed to getTableCellRendererComponent, return a blank JLabel, otherwise call the default super.getTableCellRendererComponent.
This should mean the column is still visible, but each cell will be blank.
If you want to display the data when a row is selected, you will need add a listener to the selection model (from getSelectionModel) to store the selected row in a variable, and call a repaint. You can then use this value in your CellRenderer.
Why don't you just let the dataModel control what is shown? I'm assuming you are using a tableModel.
All you have to do is change the getValueAt(..) method so that it does not return a value for the column you do not want to show. Make sure that getColumnCount() method is reduced by one as well.

Select table values to text field using SWT?

I have Table (TableViewer) containing TableItems with values. I have another class TextField mapped according to table column name.
Now my question is: When I select any row in table, the values in that row should get reflected in Text field. In Text field I can edit these vales and save to to table. Please let me know how I can achieve this?
What ever "Baz" has said is the right thing to do. If editing is table data is requirement, then make the table columns as editable. But if you want to specifically edit table row separately in a control (like Text field), then get IStructuredSelection from TableViewer and get the data in control to edit. Then after you edit, get the data back from control and set it back to table. Hope it helps.

Use of mouselisteners in a jTable

I have a jTable with columns 'Job_no' and 'Status'with values such as:
Job_no Status
1 Active
2 Pending
3 Pending
I would like it so that if a user clicks on a Status say in this case the first 'Pending'(where Job_no = 2) an inputDialog pops up allowing the user to change the status of the cell clicked-how can I do this? Bear in mind you will also have to retrieve the Job_no(that corresponds to that status) somehow, and that, though I'm OK with JOptionPane's, I'm new to JTables. I'm using JDBC(mySQL) and have a table 'Jobs' which amongst other things, has column Job_no and status.
Thanks for your help.
You don't do that by using a mouse listener and a popup, you just make the cells editable, and perhaps set a custom TableCellEditor. Take a look at the Java Tutorial for more details.
1) add a MouseListener to the JTable
2) Read the JTable API for the methods that will convert a mouse point to a row/column
3) Now that you know the row/column you can use the getValueAt(...) method to query the data in the Job_no column
4) Then you can change the status of the selected cell using the setValueAt(...) method.
So you break the problem down one step at a time.

JFace TableViewer - Format cells depending on other cells

I have a JFace TableViewer. The values in one column should normally be unique, but there are cases where it makes sense that they are not (e.g. when a row was copied and not yet changed). However I want to alert the user of the duplicate values by highlighting the rows which contain duplicate values in that column. What's the best way to do this? The LabelProvider seems to only give me access to the current cell or at most the current row.
Thanks,
Thomas
To detect duplicate across the whole table, I guess you got to have some kind of map or set containing all the cell's data. The way I did was to put such map in the view (TableViewer container) and then have label provider holding a link to that view (hence the map). So from within label provider, it is able to detect duplicate and respond accordingly.

How to change data in JTable cells?

I can set data in JTable constructor, and then user can change this data when program is running manually(typing from keyboard).
But what method should I use in case I want to change data in some column? To change column header I use TableColumn method setHeaderValue. What should I use to set value in JTable cell?
If you want to allow users to edit the data, then you need to set a TableCellEditor on the cells that you want people to edit. You probably also want to start using a TableModel instead of hard coding the data in the JTable itself.
See http://java.sun.com/docs/books/tutorial/uiswing/components/table.html
While creating the JTable you first need to specify that the values of particular column are editable. You can obviously also provide the row basis edit functionality. but all these things you should define while ccreating the table itself. Please reply if you need any help on this.

Categories