Removing JTable from content panel - java

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 );

Related

Change JFrame size dynamically to JTable contents

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 );

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);
}

make a jscrollpane (containing a jtable) fill its container

I have a JTable with autoResizeMode set to AUTO_RESIZE_LAST_COLUMN. I have added it to a panel by creating a JScrollPane with the JTable as its child widget, and then adding the JScrollPane to the panel.
I would like to set the size of the JScrollPane viewport to that of the parent JPanel, and have the JTable resize its last column dynamically.
JPanel have got implemented FlowLayout by defaut, you can place JScrollPane to the BorderLayout.CENTER
I think if you make the JPanel use GridLayout(1,1) and add the JScrollPane to it then you will get the desired result.
The first answer on this link How to make a JTable fill entire available space? worked perfectly for me.
I did the following
myPanel = new JPanel(new GridLayout());
myJtable = new JTable(MyTableModel);
myPanel.add(new JScrollPane(myJtable));

JTable inside JLayeredPane inside JScrollPane - how do you get it to work?

I want to put objects coming out of a JTable, layered on top of it, so using a JLayeredPane seems natural. However, getting this to paint properly, do the headers properly etc is very hard. How do I do this so that:
The Row headers appear and match up when it scrolls
The column headers appear and match up when it scrolls
The table paints properly
resizing doesn't mess everything up
Note that because JDesktopPane extends JLayeredPane, the answers to this question also allow you to have a JTable (or any other component) behind a JDesktopPane.
Similar but not identical questions which help:
Java - Is it possible to put a JLayeredPane inside JScrollPane? and How to display animation in a JTable cell and Swing GUI design with JScrollPane and JLayeredPane.
To do this properly there are three separate issues to consider: Sizing, Headers and UI changes.
Sizing
To scroll and paint properly the JScrollPane needs to know the size and preferred size of the component inside it, in this case the JLayeredPane. But you want the size to be set by the table, as other Components will be floating on top of the table. In this case the easiest way is to make the JLayeredPane delegate size related properties to the JTable as follows.
final JTable table = new JTable();
JLayeredPane layers = new JLayeredPane() {
#Override
public Dimension getPreferredSize() {
return table.getPreferredSize();
}
#Override
public void setSize(int width, int height) {
super.setSize(width, height);
table.setSize(width, height);
}
#Override
public void setSize(Dimension d) {
super.setSize(d);
table.setSize(d);
}
};
// NB you must use new Integer() - the int version is a different method
layers.add(label, new Integer(JLayeredPane.PALETTE_LAYER), 0);
JScrollPane scrolling = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
scrolling.setViewportView(layers);
If you didn't want the JTable to be the thing which determines the size of the JLayeredPane then it needs to be determined in some other way, and so does the table's size. Both will need setPreferredSize() and setSize() called on them explicitly.
Headers
As the JTable is no longer the viewport, you'll need to link the headers yourself. The following code will work:
scrolling.setColumnHeaderView(table.getTableHeader());
scrolling.setRowHeaderView(rowHeader);
UI
Also note that JTable does some nasty code in configureEnclosingScrollPane() and configureEnclosingScrollPaneUI(). If you want to get UI style changes to work properly, then you'll have to override these methods, but I haven't worked out how to do this yet.

Categories

Resources