0

I have created a set of buttons that I want to be at the top of every page of my application.

Rather than having to recreate the setup in every class, is it possible to create it once and include it in every class, similar to a reusable view in Android.

My code for the button setup is below:

public Buttons() {
        setLayout(null);

        JButton btnNewButton = new JButton("New button");
        btnNewButton.setBounds(10, 11, 89, 23);
        add(btnNewButton);

        JButton btnNewButton_1 = new JButton("New button");
        btnNewButton_1.setBounds(101, 11, 89, 23);
        add(btnNewButton_1);

        JButton btnNewButton_2 = new JButton("New button");
        btnNewButton_2.setBounds(192, 11, 89, 23);
        add(btnNewButton_2);

        textField_2 = new JTextField();
        textField_2.setBounds(104, 42, 86, 20);
        add(textField_2);
        textField_2.setColumns(10);

        System.out.println("HELLO");
    }

My code to create the class is below:

public static void gui(){
        frame = new JFrame("name");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(true);
        frame.setLayout(new BorderLayout());
        frame.add(new Buttons(), BorderLayout.WEST);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setBounds(200, 0, 500, 500);
    }

I have tried adding the following line to this method to add a second set of buttons:

frame.add(new Extras(), BorderLayout.CENTER);

but it only adds the second object to the display.

cello
  • 5,356
  • 3
  • 23
  • 28
  • 3
    1) Java GUIs have to work on different OS', screen size, screen resolution etc. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 2) Put a `JToolBar` in the `PAGE_START` of a `BorderLayout`. Put a (panel with) `CardLayout` in the `CENTER`. Add the 'page' panels to the card layout. Done. – Andrew Thompson Oct 26 '14 at 10:43
  • 1
    What is Extras? What is "the second object"? I can't understand what your problem is. – JB Nizet Oct 26 '14 at 10:43
  • From the documentation you can see that frame.add requires a component and constraints to be supplied, in order to add them to the frame. So, Extras is the name of the object (Class) I wish to add to the frame. When I say the second I mean that there are 2 instances of this line: 'frame.add(new Buttons(), BorderLayout.WEST);' and 'frame.add(new Extras(), BorderLayout.CENTER);'. When they both appear in the code only the last one written appears in the display. –  Oct 26 '14 at 12:30

1 Answers1

0

Create a class (or method) that creates a JPanel which holds all those buttons you want. Then, just add a new instance of that JPanel to your GUI. Pseudo-code:

public JPanel createButtons() {
  JPanel panel = new JPanel();
  // add buttons to panel
  return panel;
}
public void gui() {
  frame = new JFrame("name");
  frame.setLayout(new BorderLayout());
  frame.add(createButtons(), BorderLayout.WEST);
  // ...
}
cello
  • 5,356
  • 3
  • 23
  • 28