Hi friends I want to layout 4 entities in one frame
For this I created a JFrame and put 2 JPanels inside that JFrame. One JPanel contains a scrollablePanel which holds a JTable. And another JPanel contains 3 JButtons.
I expected the output to be like below:
But My table is not visible anymore, only the buttons are visible. Following is my code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class displayGui extends JFrame{
private JPanel topPanel;
private JPanel btnPanel;
private JScrollPane scrollPane;
public displayGui(JTable tbl){
setTitle("Company Record Application");
setSize(300,200);
setBackground(Color.gray);
topPanel = new JPanel();
btnPanel = new JPanel();
topPanel.setLayout(new BorderLayout());
getContentPane().add(topPanel);
getContentPane().add(btnPanel);
scrollPane = new JScrollPane(tbl);
topPanel.add(scrollPane,BorderLayout.CENTER);
JButton addButton = new JButton("ADD");
JButton delButton = new JButton("DELETE");
JButton saveButton = new JButton("SAVE");
btnPanel.add(addButton);
btnPanel.add(delButton);
}
}
The code in my main method:
displayGui dg = new displayGui(table);
dg.setVisible(true);
You need to specify a location for each panel on the frame...
Instead of...
getContentPane().add(topPanel);
getContentPane().add(btnPanel);
Try...
getContentPane().add(topPanel, BorderLayout.CENTER);
getContentPane().add(btnPanel, BorderLayout.SOUTH);
Side Note
The JFrame
s add method automatically redirects calls to it to the the contentPane
, so, technically, you only need to do ...
add(topPanel, BorderLayout.CENTER);
add(btnPanel, BorderLayout.SOUTH);
Updated
I should also point out that the default layout manager for a JFrame
is BorderLayout
. You can change this by simply calling JFrame#setLayout
, but the result you're after is best meet with the BorderLayout
...FYI
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.