0

I have two methods: createGui and createButton. I called createGui method in main method. GUI created.

Now I want to add other components like JButton in the JFrame by using createButton method in createGui method

How to add a button in a frame by calling createButton method?

public class JavaGui {

    public static void main(String[] args){
        CreateGui.createGui();
    }
}

class CreateGui{

    static GraphicsConfiguration gc;

    public static void createGui(){
        JFrame frame= new JFrame(gc);   
        frame.setTitle("gui");
        frame.setSize(600, 400);
        frame.setLocation(200, 200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
    }

    public static void createButton(){
        JButton button=new JButton();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • To create a button have the method return it : `private JButton createButton(){ return new JButton(); }`. You can add a button using `frame.getContentPane().add(createButton());` – c0der Feb 24 '19 at 14:32

3 Answers3

0

You should have a class that extends JFrame Java class and then you can easily add to it other components (i.e. CreateGui extends JFrame and then add a JPanel to it and then add components). The way you did it makes it look more complicated than it should be.

Guy_g23
  • 316
  • 2
  • 7
  • 2
    "You should have a class that extends JFrame Java class" , no , you certainly do not have to. In fact it is often [discouraged](https://stackoverflow.com/a/15867284/3992939) – c0der Feb 24 '19 at 14:29
0

In short:

frame.getContentPane().add(myBytton);

After that you'll need to read about layout managers.

Sergey S.
  • 45
  • 5
0

I think there are few things you can improve in your code.

  • In object oriented programing, it's better to use nouns as class names. So, CreateGui is not a good class name.
  • In object oriented programing, try to minimize the use of static.
  • Do you really need 2 methods createGui() and createButton()? I think you can do this with a single method createGui().

Considering above points, below example code demonstrates how you can build a simple UI like this.

import javax.swing.*;
import java.awt.BorderLayout;

public class JavaGui {
  public static void main(String[] args) {
    JFrame gui = createGui();
    gui.setVisible(true);
  }

  private static JFrame createGui() {
    JFrame frame= new JFrame();
    frame.setTitle("gui");
    frame.setSize(600, 400);
    frame.setLocation(200, 200);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);

    frame.getContentPane().add(new JScrollPane(new JTextArea()), BorderLayout.CENTER);
    frame.getContentPane().add(new JButton("Button"), BorderLayout.SOUTH);

    return frame;
  }
}
Prasad Karunagoda
  • 2,048
  • 2
  • 12
  • 16
  • no i want both methods . wanna call first methods to create jframe then add buttons on that frame as needed on runtime... or dynamically call createButton based on results from another class. – info catalyst Feb 25 '19 at 16:16