I currently have a JPanel inside of a JPanel where I would like to place my JScrollPane/JTable. I am having trouble getting it placed where I want it. I would like to have the table fit the full width of the JPanel but only fill as much room as it needs to display all the dynamic data in the table vertically.
This is the code I currently have and the picture is what it looks like. The blue area is the nested panel for the table/scroll situation. Also why is there a stupid gray box under the table. How do I get rid of that? Sorry about the pic, the data is sensitive. Thanks.
JPanel panel = new JPanel();
panel.setBounds(50, 100, 300, 450);
panel.setBackground(Color.blue);
contentPane.add(panel); //contentPane is the outter panel
JScrollPane scrollpane = new JScrollPane(table); //table was created earlier
panel.add(scrollpane,BorderLayout.NORTH);
Example program (I don't want that big rectangle under the table)
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
public class Test {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(350,100,1000,800);
JPanel contentPane = new JPanel(new BorderLayout());
frame.setContentPane(contentPane);
JTable table = new JTable(5,5);
JScrollPane scrollpane = new JScrollPane(table);
table.setFillsViewportHeight(true);
contentPane.add(scrollpane,BorderLayout.NORTH);
frame.revalidate();
}
}
but only fill as much room as it needs to display all the dynamic data in the table vertically.
What if you have 100 rows, they won't all be able to display on the frame at one time?
Override the getPreferredScrollableViewportSize()
method of JTable to return a reasonable number, this number should include the height of the JTableHeader. So you would probably want to cap the height at a reasonable number of rows + the table header.
panel.add(scrollpane,BorderLayout.NORTH);
The NORTH constraint doesn't do anything. A JPanel uses a FlowLayout by default. You would need to set the layout of the panel to a BorderLayout.
Edit:
You have been asked to show your SSCCE that incorporates the suggestion made in these answers. You have not:
Other general comments.
Don't specify a size for the frame. That is what the pack() method is for. Even is you did you wouldn't hardcode a size. My monitor isn't even big enough to display a frame the size you specified.
Don't make a frame visible until you have added all the components to the frame.
The GUI should be created on the Event Dispatch Thread.
Also why is there a stupid gray box under the table. How do I get rid of that?
Try using table.setFillsViewportHeight(true);
after adding it to JSCrollPane
: as you are doing now:
JPanel panel = new JPanel();
// other code
panel.setLayout(new BorderLayout()); // not sure whither you are already doing this
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
panel.add(scrollPane, BorderLayout.CENTER);
invoking setFillsViewportHeight(true)
function set the fillsViewportHeight
property. When this property is true
the table
uses the entire height of the container, even if the table
doesn't have enough rows to use the whole vertical space.
Note:
I don't see you added a layout to the panel
to which you have added the JScrollPane
. I hope you know that JPanel
uses FlowLayout
as it's default layout which obeys component's preferred size hint. Again, panel.add(scrollpane, BorderLayout.NORTH);
produces ambiguity. As we use constraint: BorderLayout.NORTH
to the component which has BorderLayout
as it's layout manager.
Frame's content pane has BorderLayout
as it's default layout manager You are setting bound to panel
using setBounds()
which we usually do while working with AbsoluteLayout(null)
layout. And AbsoluteLayout
is no go while developing application in Swing
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.