I have a JXTable that is created like this:
JScrollPane scrollPane = new JScrollPane();
contentPanel.add(scrollPane, BorderLayout.CENTER);
tbl = new JXTable();
tbl.setColumnSelectionAllowed(true);
tbl.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tbl.setEditable(false);
scrollPane.setViewportView(tbl);
The problem is that when it gets filled, all the columns are squashed to fit into the table. I expected the columns to be adjusted according to the width of the column header or column value (which ever is broader) and then for a horizontal scroll bar to be displayed that will enable me to scroll left or right.
How do I bridge this problem?
You should use the packAll method: this resizes columns to fit the viewport; if horizontal scrolling is enabled, columns will get their preferred width.
Related
I have a jTable with the tree which automatically selects the corresponding row from previous action. But changeSelection scroll and select row as last visible on bottom. For me is uncomfortable because is tree and is much better to be scrolled and selected row as first visible on top of the table.
I am try table.scrollRectToVisible(table.getCellRect(row,column, true)); same result and tried custom set scrollBar value but I don't know right value because panel can change size.
I expect that automatic scroll to row and that row will be marked on top of the table. Now automatic scroll to row and is marked on bottom of the table.
The scrollRectToVisible(...) method only scrolls the JViewport of the JScrollPane to make sure the Rectangle is visible. So when you scroll down. It will display at the bottom. If you are scrolling up it will display at the top.
An easier way to control the scrolling is to set the JViewport position yourself:
JViewport viewport = (JViewport)scrollPane.getViewport();
Rectangle rectangle = table.getCellRect(row, column, true);
Point point = new Point(rectangle.x, rectangle.y);
viewport.setViewPosition( point );
I have a Panel inside which I add a scroll table. I want this scroll table's header to be resizable (all columns should be able to resize and if table width exceeds panel width then a horizontal scroll bar should be displayed). Also if I resize the panel, scroll table should also be resized to fit the with of panel.
If I use AUTO_RESIZE_OFF for the table, I am able to resize header (columns) and a horizontal scroll bar is displayed but with this scroll table is not fit to panel's size when I resize the panel.
Adding a simple code base:
JPanel panel = new JPanel();
JTable table = new JTable();
table.getTableHeader().setResizingAllowed(true);
table.setAutoResizeMode(Auto_Resize_Off) // this is done to show horizontal scroll bar when table's columns are resized
panel.add(table);
This allows me to resize my table's columns a display a horizontal scroll bar if table width is more than panel's width.
But when I resize the panel, it is not resizing the table also which I need.
I have a JTable inside a JScrollPane, inside a JTabbedPane. The table header scrolls fine until I set a custom default renderer for it that dynamically changes the width and height of the header cells, i.e.:
table.getTableHeader().setDefaultRenderer(new MyHeaderRenderer());
... where MyHeaderRenderer extends a JLabel and its getTableCellRendererComponent() method sets the preferred size like this:
table.getTableHeader().setPreferredSize(new Dimension(width, height));
After settings the custom renderer the table body continues scrolling correctly, but the header stays fixed to the upper left corner of the JScrollPane. I believe the renderer is repainting the header continuously in that position.
How could I fix this?
Thanks.
My problem was that I had lots of columns with long header texts, so I created a header renderer to set the height and width of the header cells according to the length of the cell text.
As it turns out, you can't resize the header cells by setting the preferred size of the table header itself or you'll end up with a header always fixed to the upper left corner of the table.
Regarding this matter, I found no solution.
I tried setting preferredSize outside the renderer: it didn't work, which means the problem is not the continuous rendering.
I tried using a JTextArea instead of a JLabel: it made no difference.
I tried overriding the JTable doLayout() method: it didn't work.
I fixed the issue by removing:
table.getTableHeader().setPreferredSize(new Dimension(width, height));
... and adding these code lines to the getTableCellRendererComponent() method:
table.getColumnModel().getColumn(column).setPreferredWidth(width); //sets the width
setPreferredSize(new Dimension(width, height)); //sets the height of the JLabel returned by the renderer
This was the best I could think of. If anyone finds a better solution, please share it :-)
I faced the same problem and found a workaround.
JTable header is a separate component, that stored inside parent JScrollPane ColumnHeader. So, instead of overriding header preferred size we can set preferred size of JScrollPane ColumnHeader.
JScrollPane scrollPane = new JScrollPane(table);
scrollPane.getColumnHeader().setPreferredSize(new Dimension(0, height));
Fortunately, JScrollPane ignores column header width, so you can safely set zero here.
I have a JTable that is filled dynamically.
It has only two columns, the first is a boolean value and the second is a string.
The problem is that when I select a row in the string column, the table autoscrolls to the right hiding the first column.
How do I change this behaviour?
If I disable autoscroll, the user is not able to scroll to the bottom through the arrow keys.
Any ideas?
Thanks in advance.
Call:
jscrollpane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
which will make sure no horizontal JScrollBar is created for the JScrollPane.
Perhaps also need to ensure the JScrollPane fits the JTable horizontally via:
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane jscrollpane = new JScrollPane(table);
I am trying to resize the widths of the columns in a JTable.
I would need to resize them when the program is running to see the full column header names.
I want to resize table of database showing in swing frame.
I have tried
table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS)
But I didn't get the desired output because when I use this I have a horizontal scrollbar to scroll to the columns that are off screen. But I want the size of the whole table to become approximately equal to the size of frame.
How can I make the columns as wide as they need to be to display all the text?
But I want the size of the whole table to become approximately equal to the size of frame.
JTable table = ...;
JFrame frame = ...;
JPanel contentPane = new JPanel( new BorderLayout() );
contentPane.add( new JScrollPane( table ), BorderLayout.CENTER );
frame.setContentPane( contentPane );
How can I make the columns as wide as they need to be to display all the text?
Unfortunately, the maximum width of the JTable will be limited to the size of the JFrame, and there is no mechanism to push from your JTable the desired width to the JFrame when your JTable is contained in a JScrollPane.
What you could do is try to give your JScrollPane the correct width. I haven't tried this, but I suspect that giving your scrollpane the same width as the preferred width of your table (when autoresize is on) should solve this (+ perhaps some extra width for the vertical scrollbar). Note however that this will only work if you allow your JFrame in which the JScrollPane is contained to grow in width as well.