Java JTable expand column width while editing. No keylogging - java

I want JTable column to expand in width if a user enters too many characters into a cell so that content should remain visible. I do not want to use a keylogger as this is not clean, there could be other means of editing a cell.
It seems I can only react in the listeners either at the beginning of an edit or at its end. Can I also react to the editing in between? Or is there maybe something built in? I would have guessed this is a standard task.

I managed to listen to any editing event by using a DocumentListener for each cell.
It indeed works to resize the table during these events - however, if I do so and the column changed in size, the cell I am editing is suddenly colored in blue, as if I have just selected the column with the mouse. The cell however remains in editing mode.
I try now to get rid of the blue color by firing some more events. I hope the whole thing does not crash at some point.
Anyway, if I indeed follow this approach, I need to calcuate the required column size based on the characters typed into my cell - a difficult task if I use non-monospaced fonts. Any ideas on that?

Related

Why are there two different editing modes with DefaultCellEditors?

When using a DefaultCellEditor in a JTable, there seem to be two different editing modes:
Single clicking on a cell does not display the editor component, but (for String/Object classes) one can still type in the selected cell and thereby edit its value.
Double clicking on a cell displays the editor component and one can edit the selected cell's value as expected.
Why is this? One would think that editing a JTable cell's value would always involve the same UI behavior.
Technically, double clicking a cell and typing in a cell (which supports text editing) essentially boils down to the same thing. You can actually double click a text editable cell to start the editing process
Cell editability comes to down to two factors, the result of TableModel#isCellEditable and TableCellEditor#isCellEditable, when these two methods return true a cell can be placed into edit mode.
A text editable cell is, generally, a special case, where the cell can be edited by typing in the cell while it has focus and double clicking (and pressing F2 in most cases).
The case for providing more than one mechanism for initializing the editing process will come down to decisions made over usability and existing conventions across multiple platforms. It's likely that some attempt was made to mix expectations where possible so users of different platforms could feel more comfortable with the process, but this is simply speculation.
The problem is, even amongst users of a single platform, there are different expectations about how something like this works, so rather the supply a single trigger, the system has been designed to allow for multiple triggers, where it would be applicable.
For example, it wouldn't make sense for a cell containing an image to allow the user to edit the cell simply by typing in it.
Updated
If you take a look at the isCellEdtiable method for DefaultCellEditor...
public boolean isCellEditable(EventObject anEvent) {
if (anEvent instanceof MouseEvent) {
return ((MouseEvent)anEvent).getClickCount() >= clickCountToStart;
}
return true;
}
You will note that the only event that actually stops a cell from entering edit mode is a MouseEvent, but only when the number of mouse clicks is less than clickCountToStart, which is set to 2 when using a JTextField as the editor, otherwise it's 1

Java: break focus cycle in jtable

In my JTable, I want to edit all relevant cells like in Excel.
I implemented that ENTER stops editing the cell and transfers focus to the next cell. However, when I hit ENTER in the last cell, the focus cycle makes me jump to the fist cell. But I want to continue outside the table and set focus to a JButton.
How can I break this cycle?
JTable is not a spreadsheet, but setCellSelectionEnabled() should allow you to proceed. Use setDefaultButton() as required, for example.
Addendum: In addition to setDefaultButton(), you can evoke any desired action in your custom Action, as shown here.

How to: JTable look and feel like Windows 7 Details View or Vuze table

In Windows 7 if we set the content of a Folder in Details view, then it turns into a table like structure, in which if we hover a row it renders a rectangular shape with light blue color and slightly curvy corner on that row and if we select a row a similar shape with blue color set on that row. This similar effect is shown by Vuze's table.
Is there any way to achieve this table rendering for JTable? If so what is the way to get it?
Any information will be helpful to me.
Thanks in advance.
The highlighted portions in the following image shows what I intended to achieve. The first highlight is the selected row and the second one is hovered.
you can do that by implement Substance Custom Look and Feel for JTreeTable,
plugin for SwingX
TreeTable by aepryh (best and open code for TreeTable)
notice you have to change XxxRenderer to SubstanceXxxRenderer (works on Xp / Win7 / 2008R2)
There's nothing built-in to achieve this. You can achieve the hover effect by using custom cell renderers and mouseover listeners. This answer gives you an overall picture of what to do.
As for the rectangular effect, again - custom cell renderers only, with either images, or drawRoundRect
I just noticed this post. JIDE has a component that does what you need. It is in paid JIDE Grids product. You can find a screenshot at http://www.jidesoft.com/images/navigation-components.png. Of course you can do this by yourself by overriding the paintComponent of a regular JTable. Using cell renderer approach won't work as the rollover effect needs to span the whole row.

JTable custom cell renderer focus problem

I have a table like this. The second column uses a JTextField renderer and the third column uses a JPasswordField based renderer and editor.
Looks good. But the problem is We have to type the values and must hit "ENTER". In that image, I have typed my password but didn't hit Enter. So If I click the 'Save & Close' button, it'll show me an error that password field is empty.
Previously I have used only JTextFields and JPasswordFields under JTabbedPane and it worked well. When I had to add more and more stuff, I changed it to a table like this.
For now I have put a label to let people know that they should hit the ENTER.. This is not nice. Another big issue. Atleast in Nimbus Look and feel, we get an idea that that field is still in focus. In Windows system look, there's not much visible difference whether the field is focused or not.
I need the Username field or password field to set it's value when I click 'Save & Close' button. Please help me.
So your problem is, that you are still editing the cell. So you have to stop the editing and then the cell will be changed.
At your button you can get the cell who is being edited with
TableCellEditor cellEditor = table.getCellEditor();
then you can stop the editing with
if(cellEditor!=null){
cellEditor.stopCellEditing();
}
and then you can save the value
Tell the table to automatically commit when losing focus:
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

How to Mirror Edits across a Table Cell and Text Field

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.

Categories