0

I'm working with JFrames in java to make a GUI. I'm having a problem where my 2 JFrames are painting over eachother though.

public VidbergGUI() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
    super("Automatic Output Verifier");
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    setBounds(100, 100, 600, 400);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container con = this.getContentPane();
    con.add(titlePane);

    titlePane.setLayout(new BoxLayout(titlePane, BoxLayout.PAGE_AXIS));
    componentPane.setLayout(new BoxLayout(componentPane, BoxLayout.LINE_AXIS));

    programsLoaded = new JTable(data, columnNames) {
        @Override
        public boolean isCellEditable(int row, int col) {
            if (col == 3) return false;
            return true;
        }
    };
    programsLoaded.getColumnModel().getColumn(2).setCellEditor(new FileChooserEditor());

    tableHolder = new JScrollPane(programsLoaded);

    titleLabel.setFont(new Font("Ariel", Font.BOLD, 28));

    addButton.setSize(40, 40);
    removeButton.setSize(40, 40);

    titlePane.add(titleLabel, BorderLayout.PAGE_START);
    con.add(componentPane);
    componentPane.add(tableHolder, BorderLayout.LINE_END);
    componentPane.add(addButton, BorderLayout.EAST);
    componentPane.add(removeButton, BorderLayout.EAST);
    setVisible(true); // make frame visible
}

With this setup, only the componentPane is visible. If I comment out con.add(componentPane), only the titlePane is visible. Is there a way I can assign some sort of layout so the 2 frames stack vertically?

Nat
  • 890
  • 3
  • 11
  • 23
  • 1) See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) 2) For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example). 3) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Nov 13 '14 at 03:00

1 Answers1

3

You do understand that BorderLayout can only layout a single component in each of it's 5 available slots? Probably not...

Create a third JPanel and add titlePane and componentPane to it and then add this to the CENTER position of the BorderLayout

You could use a GridLayout or GridBagLayout for this panel...

JPanel centerPane = new JPanel(new GridLayout(2, 1));
centerPane.add(titlePane);
centerPane.add(componentPane);
con.add(centerPane);

Or just add titlePane to the NORTH position...

con.add(titlePane, BorderLayout.NORTH);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366