Duong Bui
Duong Bui

Reputation: 11

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.

Upvotes: 0

Views: 46

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

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

Upvotes: 2

Related Questions