BERWIN
BERWIN

Reputation: 29

BorderLayout(); buttons not showing up

Hey guys my buttons and textarea will not display on JFrame when compiled, i have tried everything and searched this site but no luck. Any help would be greatly appreciated. Due to them not letting me post without more detail i am just adding this part so i can hit the submit button.

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class DataManager extends JFrame {

private String students[] = {"John Smith","Ken Hanson","Michael Li","John Andersen","Fiona Harris","Angela Lim","Bob London","Sydney Shield","Tina Gillard",
                             "Ross Brinns","Scott Cairns","Grant Peterson","David Power","Joshua Kane","Alan Newton","Frady Morgan","Quinn Perth"};


private int english[] = {80,52,71,61,39,62,31,46,60,26,77,40,58,38,94,90,97};

private int maths[] = {60,45,77,90,45,55,66,87,31,42,65,55,80,71,51,55,95};

private int total[];

private JButton sortNameButton;

private JButton sortTotalButton;

private JTextField searchTextField;

private JButton statisticsButton;

private JButton exitButton;

private JTextArea infoTextArea;

private JPanel jPan;


public DataManager() {

super("Data Manager ");

jPan = new JPanel();

sortNameButton = new JButton("Sort By Name");

sortTotalButton = new JButton("Sort By Total");

searchTextField = new JTextField("Search");

statisticsButton = new JButton("Statistics");

exitButton = new JButton("Exit");

infoTextArea = new JTextArea();

setLayout(new BorderLayout());

jPan.add(sortNameButton, BorderLayout.NORTH);
jPan.add(sortTotalButton, BorderLayout.NORTH);
jPan.add(searchTextField, BorderLayout.NORTH);
jPan.add(statisticsButton, BorderLayout.NORTH);
jPan.add(exitButton, BorderLayout.NORTH);
jPan.add(infoTextArea, BorderLayout.CENTER);


}

public static void main(String[] args) {

    DataManager frame = new DataManager();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(800,600);
    frame.setVisible(true);

} // End of main method.

} // End of DataManager class

Upvotes: 1

Views: 913

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285430

You add your JButtons to the jPan JPanel but never add the jPan to anything -- it must be added to your JFrame, to this to be seen.

jPan.add(sortNameButton);
jPan.add(sortTotalButton);
jPan.add(searchTextField);
jPan.add(statisticsButton);
jPan.add(exitButton);
jPan.add(infoTextArea);

add(jPan);  // don't forget this! ************

Note other problems:

  • You set the JFrame's layout to BorderLayout -- it's already using BorderLayout
  • You add components to your jPan JPanel with BorderLayout constants, but it's not using a BorderLayout.
  • If it were, many buttons would not be seen since many are added to the same BorderLayout position and will cover the previous component added there.

In other words, read the tutorials as you're making wrong assumptions.

Better would be something like:

    // setLayout(new BorderLayout());
    jPan.setLayout(new BorderLayout());

    JPanel northPanel = new JPanel();  // **** to hold buttons

    northPanel.add(sortNameButton);
    northPanel.add(sortTotalButton);
    northPanel.add(searchTextField);
    northPanel.add(statisticsButton);
    northPanel.add(exitButton);

    jPan.add(northPanel, BorderLayout.PAGE_START);
    jPan.add(infoTextArea, BorderLayout.CENTER);

Upvotes: 4

Related Questions