Duong Bui Duong Bui - 1 year ago 84
Java Question

JFrame : cannot display two panels in jframe

I have problem to display two panel in Jframe. Please help me to fix the code below

public class quotingtable extends javax.swing.JFrame {
DefaultTableModel model;
JTable table;
JButton SetButton = new JButton("Set Symbol");
JButton VNStock = new JButton("VNStockChart");
JButton Global = new JButton("GlobalChart");
JPanel quotingpanel = new JPanel(new BorderLayout());
JPanel functionpanel = new JPanel(new BorderLayout());

public void run(){
model = new DefaultTableModel(col,row);
quotingpanel.add(table);
functionpanel.add(BorderLayout.CENTER,SetButton);
functionpanel.add(BorderLayout.WEST,VNStock);
functionpanel.add(BorderLayout.EAST,Global);
table = new JTable(model);

JScrollPane pane = new JScrollPane(table);
quotingpanel.add(pane);
getContentPane().add(BorderLayout.CENTER,functionpanel);
getContentPane().add(BorderLayout.SOUTH,quotingpanel);
setSize(800,800);
setLayout( new FlowLayout());
setLayout ( new BorderLayout());
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);


Any help is appreciated.

Answer Source

Remove:

setLayout( new FlowLayout());
setLayout ( new BorderLayout());

Using BorderLayout this way won't pick up the pre-existing components, so will ignore them and won't lay them out

And consider replacing setSize(800,800); with pack();

You may also want to change

getContentPane().add(BorderLayout.CENTER,functionpanel);
getContentPane().add(BorderLayout.SOUTH,quotingpanel);

to

getContentPane().add(functionpanel, BorderLayout.CENTER);
getContentPane().add(quotingpanel, BorderLayout.SOUTH);

it's simply a more consistent and preferred mechanism

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download