Change JFrame size dynamically to JTable contents - java

I've got myself stuck again.
This time I have a JTable inside a JFrame.
I basically just want there to be no "white-space" below the table generated.
When resizing, the rows don't change height, but the columns change width for some reason. Is it at all possible to not have the white space below?
I'd prefer not to have a scrollbar if it at all possible, and just show the entire table with the white space removed, so even when resized, it doesn't show up.
Quick Update: i used the gridlayout layout and it kind of worked, but my header has a bigger font than the table, resulting in the cells to be shown properly, but the headers being cutoff and displayed as "Hea..."

Is it at all possible to not have the whitespace below?
JTable table = new JTable(model);
table.setPreferredScrollableViewportSize(table.getPreferredSize());
JScrollPane scrollPane = new JScrollPane( table );
frame.add(scrollPane);
frame.pack();
frame.setVisible( true );

Related

Horizontal scroll bar wont show on a dynamic table

I am trying to make a dynamically growing JTable, specifically, growing columns. However, I've stumbled upon a problem where If my JTable grows too big, all the columns are compressed and the cell wont be visible any more. I initially based code from the best answer on this. Additionally, I tried to altered the code and placed it on the viewport of the scrollpane and it did not work. This was it.
JScrollPane scrollPane = new JScrollPane();
scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
tableModel = new DefaultTableModel(new Object[] { "Initial Column" }, 5);
JTable table = new JTable(tableModel);
scrollPane.setViewportView(table);
JFrame frame = new JFrame("Table Column Add");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 600, 300);
frame.add(scrollPane);
frame.setVisible(true);
Afterwards, I saw another solution on dynamic JTables. I tried running the best answer on this link and it's the same thing, when then columns get too big, it will be compressed.
However, if you keep adding rows, the scroll bar would appear and the row height is maintained and not compressed.

JTable and JScrollPane

I have a really huge JTable and I want to add scroll bars to the JFrame so that I can scroll to see the rest of the table instead of resizing the frame.
Problem is, when I add the table and scroll-bars, the width of the columns is shrunk to fit the size of the window. Is there any way to keep the width of the columns a constant and use a scroll-bar to view the rest of the table?
"Is there any way to keep the width of the columns a constant and use a scroll-bar to view the rest of the table?"
In this case a better solution than set a fixed (constant) width for columns is set JTable's autoResizeMode property to AUTO_RESIZE_OFF in order to avoid columns be auto-resized to fit the table's visible area:
JTable table = new JTable(15, 25);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
JScrollPane scrollPane = new JScrollPane(table);
This snippet will produce a 15 rows and 25 columns table that will look like this (note the horizontal scroll bar):
However this approach won't be helpful if the sum of the columns width in your table is less than the table's width. In that case it should be better to let the default AUTO_RESIZE_SUBSEQUENT_COLUMNS policy.

How to Set Bounds of JTable while Set Layout is Null?

In my
public UI(){
I have JTextField,JButtons and labels
I have also setLayout(null);
}
Now I'm Trying to create a JTable
JTable table = new JTable(data, headers);
But it wont display until I take out setLayout
For all the buttons,text boxes and labels I have setBounds();
How can I display the JTable while the setLayout is Null?
Don't use null Layout, instead use LayoutManagers. Java created for you amazing layouts, you just have to use it, and let the layout do your job instead of hard working of calculating the position and size.
That's my answer, but if you insist of using it, just call setBounds(x,y,width,height) method for the JTable.
But again, don't use absolute positioning(null Layout).
//....
JScrollPane scrol = new JScrollPane(table);
scrol.setBounds(table.getBounds());
//....

NetBeans JTable without Scroll Pane, Keep Header

I'm attempting to add a JTable with NetBeans GUI builder. The table is inside a panel which already has a scroll bar. Netbeans automatically creates all JTables inside of a JScrollPane.
However, I want the table to scroll as part of a larger page. I do not need two scroll bars.
My problem is: if I get rid of the scroll pane, I lose the header.
Is there a way to have a table with a header inside the Netbeans GUI builder?
My problem is: if I get rid of the scroll pane, I lose the header.
JTableHeader is (automatically) visible in the case that JTable is inside JScrollPane
you have to get JTableHeader from JTable and place this Object programatically by using LayoutManager to the container, I'm strongly recommend to use BorderLayout or GridBagLayout for this container
If you add JTabel directly to container(not to JScrollPane) you need to add JTableHeader by yourself(programatically ), try next example:
public static void main(String[] args) {
JTable t = new JTable(new Object[][]{{1,2,3}},new Object[]{"1","2","3"});
JFrame frame = new JFrame();
frame.add(t.getTableHeader(),BorderLayout.NORTH);
frame.add(t);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}

Removing JTable from content panel

I'm using a JTable to graphically display search results for an application I'm developing. I would like the ability to remove a table once it's not longer needed, and then replace it with a newly created table. Here is how I'm currently adding the table to my JFrame:
userLibrary = new CustomLibrary(users, LIBRARY_WIDTH, LIBRARY_HEIGHT);
userLibrary.setOpaque(true);
userLibrary.setBounds(LIBRARY_START_X, LIBRARY_START_Y, LIBRARY_WIDTH, LIBRARY_HEIGHT);
getContentPane().add(userLibrary);
My custom Library (Which extends JPanel) does the following:
public CustomLibrary(LinkedList<User> usernames, int width, int height) {
CustomTable table = new CustomTable(userRows,columnNames);
table.setPreferredScrollableViewportSize(new Dimension(width, height));
table.setFillsViewportHeight(true);
table.setAutoCreateRowSorter(true);
JScrollPane scrollPane = new JScrollPane(table);
// Add the scroll pane to this panel.
add(scrollPane);
}
now this all works fine and displays my table, but I can't figure out how to completely remove the table from my content pane. I've tried calling
getContentPane().remove(userLibrary);
But this appears to do nothing.
So my general question is. How do I completely remove a table from my JFrame once I've already created it and drawn it?
I would like the ability to remove a table once it's not longer needed, and then replace it with a newly created table.
The easiest way is to just replace the TableModel of the JTable:
table.setModel( yourNewlyCreatedTableModel );
No need to create a JTable or a JScrollPane.
To remove and replace it with another component:
contentPanel.remove(table);
contentPanel.add(component, BorderLayout.CENTER);
After adding/removing components you should do:
panel.add(...);
panel.revalidate();
panel.repaint(); // sometimes needed
Usually a JTable is displayed in a JScrollPane. So maybe a better solution is to use:
scrollPane.setViewportView( anotherComponent );

Categories

Resources