I want to have multiline feature for JTable cells. Wherein the cell automatically expands and shrinks itself when user writes or deletes text in/from cell. I have set columns width, but it doesnt automatically adjust itself according to user's input, rather text beyond the width are not seen at all, but i have a requirement to fix on the width. Can anyone help me with this, to have self adjusting and multiline feature for jtable cell. Would be very thankful.
Thanks in advance.
Several steps:
implement a custom renderer using a JTextArea as rendering component
implement a custom editor using a JTextArea inside a JScrollPane as editing component
add logic (aka: TableModelListener) which updates JTable's individual rowHeight based on the renderer's preferred size on receiving a change notification
Related
I'm writing an application that has a JTable, and an edit button that sets the current selected row to be editable. Then once the user is done altering the data, they can click the edit button again (with text that now says "Save") to save the data.
The problem is though, when I set a row to be editable, there isn't a visible difference. I could add some code to the renderer to draw the editable cells a little differently, but I don't know what the proper way to make a cell look editable is. Change the color? Make it look like a JTextField? What's the standard method?
Thanks!
Really this is a user interface design question, not a programming one.
To do what you want you need to supply an appropriate cell renderer with the changes you desire but you will need to decide on your own settings. One option might just be to look at the difference between an editable and non-editable text area and apply those to all the cells on the table. This may be as simple as setting the renderers to disabled for any read-only rows.
I'm trying to imitate some Excel spreadsheet functionality on my JTable, specifically what I've been calling cell spillover or cell overflow. This occurs when you fill a cell with text with a wider width than the cell is set to. If there is no text in the cell to its right, the words simply keep going into the next cell and beyond.
I feel like this would be accomplished somehow with TableCellRenderer, but I do not know how.
Any help would be much appreciated
I dont know because you did not give alot of detail but a simple use case would be:
When text is entered check for the amount of characters
Check for the width of the cell and look if the text fits
If the text doesnt fit check if the cell next to this one has text
If it has no text, resize the cell to fit the text and make the one next to it smaller.
Or something like that.
But i dont know if JTable allows resizing of individual cells, you should check the JTable API.
http://docs.oracle.com/javase/6/docs/api/javax/swing/JTable.html
And i dont know if there is not a better way, so you should consult API documentation and examples.
For example: http://users.csc.calpoly.edu/~jdalbey/305/Lectures/TableCellRendererExamples.html
I hope it helps you.
No, a TableCellRenderer cannot span cells. As alternatives, consider these:
TablePopupEditor, a custom TableCellEditor, can be adapted to serve as a renderer.
A ListSelectionListener, shown here, can be added to an adjacent component in order to display details of the selected row, perhaps using a JScrollPane.
JTable and JXTables have a header cell that is generated above the scrollbar. It's a small cell but I would like to use it to render an icon and popup menu. How might I access this header cell?
Thanks
In JXTable, the property to set is the columnControl: basically, it can be any component you like. To keep the functionality of the default, you can subclass ColumnControlButton
I'm new to swing and I faced my first serious problem, here goes:
I have a JPanel with JTable and lots of checkboxes below the table. I'm trying to align the checkboxes below the table with checkbox located in the first table column. The problem is - this has to be done when model data changes.
Immediatly after the fireTableDataChanged() gets triggered in JTable there is no way to get the screen location of its custom cell renderer component (which in my case is JCheckBox).
As far as I understand this happens because JTable gets redrawn asynchronously. Whenever I try to get the location the IllegalComponentStateException gets thrown.
Any ideas on this are highly appreciated.
You shouldn't do it manually. You should use the appropriate layoutManager. Check this link for the different Layouts:
http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
This might help you to get screen location of row and column.
Rectangle rect= cartTable.getCellRect(row,column,true);
//cartTable-Jtable object name
int x=Double.valueOf(rect.getX()).intValue();
int y=Double.valueOf(rect.getY()).intValue();
Hai
I am developing a standalone application using Java in which I am using a JTable.The problem is when I enter a multiline text,the entire text is not displayed in the cell while I am typing.I get a scroll when I type a multiline text.How can I get my whole text to be visible while I type.i.e How can I increase my Cell width in JTable while I am Typing. But the entire text will displayed only when I click out of that Cell.Can someone help me how to solve this problem
Thank You Chaithu
You should try a custom TableCellRenderer with JTextArea for example. For typing you'll probably need similar TableCellEditor.
An example can be seen at Java Specialists' Newsletter : Multi-line cells in JTable in JDK 1.4+.