JavaFx TableView Columns don't fill the TableView Width - java

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.

Related

JavaFX Add Node to ScrollPane with SceneBuilder

The problem
In code it works perfectly if i do it like:
TilePane tilePane = new TilePane();
ScrollPane scrollPane = new ScrollPane(tilePane);
I mean that every time the TilePane has more items that can be shown then a scroll appears vertically or horizontally without adding any extra code or setting minimum or preferred size of TilePane.
Using SceneBuilder
Instead of using pure code i want to have the same result as the above using SceneBuilder but every time i am trying to do it something like this: happens
I have to set the preffered size to the TilePane.Why thought? And it doesn't work as expected.I want it to automatically work as described in the first lines of the question.A good description is needed...
See the prefWidth and prefHeight to USE_COMPUTED_SIZE. These are the default values for a TilePane, but SceneBuilder seems to set them to default numeric values when you drop the TilePane into a ScrollPane.
You can select these values directly by clicking on the drop-down boxes next to the text field for the prefWidth and prefHeight text fields.

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 to adjust components and JTable in a JFrame dynamically?

When i try to hide the rows in a jTable, the jTable ScrollPanel need to adjust its size to remaining rows and, other components in the Frame also adjust to the change. i.e., there shouldn't be any space between the components and the jTable after hiding the table-rows..
ex: in microsoft excel, when we delete a row, the remaining rows will move upwards to fill the deleted row..
Please help me.. Thank you..
Edited:
Used a model that extends AbstractTableModel. Used RowFilter to hide the rows.
Problem is even when the rows disapper, the JScrollpane(where the JTable resides) wont get adjusted to the remaining rows..
You can use setPreferredScrollableViewportSize() to resize the scroll pane to the desired multiple of getRowHeight(). You can use validate() "to cause a Container to lay out its subcomponents again" or pack() to cause the Window "to be sized to fit the preferred size and layouts of its subcomponents."
Are you trying to hide rows or delete rows? You mention hiding rows in JTable but deleting them in Excel.

Allowing users to resize columns of JTable

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.

Categories