Allowing users to resize columns of JTable - java

Im creating a table which is dynamically changing its width. And I add columns by time as well. And I have bounded this table to a scrollpane. Here I have set the auto resizing to false in the JTable (else it will fill the whole area from the beginning).
And I'm adding images to cells using cellrenderer as well. Now I need to allow users to resize this table columns.
Any idea?

I don't understand the question, by default the user is allowed to resize the columns by dragging the header of the column.
However if you want this to happen automatically then you can try using the Table Column Adjuster, which can automatically adjust the columns to fit the size of the data.

Well first of all - just change TableModel. You can leave column resizing on and for example set maximum width of cell by using setMaxWidth
Or just pick correct mode that fits your requirements. You can find them here.

Related

JavaFx TableView Columns don't fill the TableView Width

I have a TableView generated with SceneBuilder and all the columns are FXML imports from other views, until there no problem, but the columns don't fill the width.
I tried to fix this with scene builder and FXML, but no luck, all the sizes are computed.
I tried to code it with a change listener that checks every time the window changes size to adapt the size of the columns.
This works and the columns resize to the proper width (basically I am getting the table view width and divide it by the number of columns), but for some reason the point where the column starts doesn't change and they overlap with each other.
Any suggestions?
Select the TableView in Scenebuilder an go to the Properties an change "Colum Resize P..." to the value > "constrained-resize":
Normally thats all you need.
If that doesn't work, you can try to reset the columns values:
Select all columns you have at the same time
Go to Layout and change all setting to the settings in the picture
This should work.

Changing Content of JTable Cells Individually

So Suppose I create a JTable with a specific number of columns and rows. Is it possible to add a JPanel to a specific cell after the JTable is already initialized? The JTable would be empty at first and then I would change a cell at row X and column Y to instead contain a JPanel which would contain an amount of ImageIcons. I don't have any code to put here really, I just need to know what I would have to use to accomplish this and how. Thank you.
To change the data in the table you use:
table.setValueAt(...);
This will update the data in the TableModel.
However, adding a JPanel to the table is not the way a table is designed to be used. Typically you add data to the model. Then you use a renderer to display the data in a cell.
Start by reading the section from the Swing tutorial on Using Custom Renderers for more information and examples.

Resizing the column of jxtable automatically

I am using JXtable in my project.When I double click some column,it will be expanded or contracted to the highest character cell in that column. Is there any way of creating JXtable so that all its columns shall be resized to the size of its highest character cell.
I'm not sure I understand correctly, but to programmatically resize all columns according to their contents you can use JXTable.packAll();.
packAll() Javadoc:
public void packAll()
Resizes all columns to fit their content.
By default this method is bound to the pack all columns Action and registered in the table's ActionMap.
This will make the column with the "broadest" content the biggest, the column with the second "broadest" content the second biggest and so on. I am using it in my projects and it works quite well.
Resizing can also be achieved by clicking on the according menu item from the table controls.
You can use the Table Column Adjuster which will automatically adjust the width of a column any time the data is changed.

How to scroll horizontally within a single column of a resizeable JTable?

I am making a dialog for the purpose of selecting multiple file paths. My dialog consists of two panels. One for buttons such as "Add" and "Remove", and a second panel containing a JTable wrapped in a scrollPane. The table has only one column. The cells of the table are not editable directly. When a user selects a file using a JFileChooser, the full path of that file will be added to the table. Although my dialog is resizeable, I still need a horizontal scroll behavior in the event that the file path is longer than the user's screen is wide.
I have researched the combination of resizeable table and horizontal scroll bar. That is similar, but not my issue. The typical scroll behavior is that the columns are scrolled, not the contents of the columns. I need the contents of a single column to scroll horizontally.
doesn't matter whether you scroll a multiple or only a single column: the basic issue is to get the horizontal scrollBar to start with :-)
There are two screws to tweak:
- enable horizontal scrolling by setting the table's resizeMode: default is to always fit the table's size to the size of the scrollPane, that is no scrolling
- resize the column width to fit its content
In a core JTable that maps into pseudo-code like
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
// on receiving a TableModelEvent which might increase the column width
// calculate new width by measuring pref of the renderer
int newWidth = ...
// set it as pref of the column
table.getColumnModel().getColumn(0).setPreferredWidth(newWidth);
The catch is that without resizeMode, you are always responsible to sizing the column: it its width is less than the scrollPane, there's an empty region at its trailing side.
JXTable (part of SwingX project), supports an addition sizing mode which fills the available horizontal space as long as the table's prefWidts is less than parent width and shows a horizontal scrollBar if needed
table.setHorizontalScrollEnabled(true);
// on receiving a TableModelEvent which might increase the column width
// tell the table to re-evaluate
table.packColumn(0);
I selected kleopatra's answer as correct because it addresses my specific question regarding table manipulation. I am adding this answer because I ended up solving my root problem in a different manner.
I chose to use a JList to represent my file paths instead of a single column table. The only real reason that I had wanted to use the JTable was because of the appearance of the lined rows that a table has, and because of my unfamiliarity with JList. I discovered how to edit the appearance of the JList by extending the DefaultListCellRenderer. Because I now knew about editing the appearance, the JList's natural resizing and scroll behavior made it a much more natural fit to my needs.

How do I control column width of the Vaadin Table?

I have a large table to display in one of our Vaadin application. The table has 110 columns with large text header for each column.
On display the table looks awful as all the column header goes off the screen and the header separator loses the alignment with the data record column separator.
I have set the table width to 100% to display the table scrollbar. I tried to set columnExpandRatio as well as column width but no success.
Vaadin table by default sets the text header width as the column width. Is there any way i can set the header/column width to fit my data rather the header?
Finally as a solution I assigned no width value to initial columns (like first 30 columns will span naturally) and later columns with fixed width size with setColumnWidth() and it worked.
Please use table column name hide which s very useful for the viewer can choose what are the column he is able to view ...
http://demo.vaadin.com/sampler/#TableRowHeaders

Categories