0

I've been looking but have not been able to find something in simple enough terms for me to understand. I currently have a program running on a java console, and I make options available through text. I now want to make this into a window program that uses buttons instead of a text option.

I found info on how to create a JFrame, but I am having issues finding info on how to make a button that calls a method, and have text that changes to show updates.

Any links or info would be much appreciated, I dont know where to go and any nudge is a help!

Fyrebend
  • 123
  • 2
  • 2
  • 9
  • *"..having issues finding info on how to make a button that calls a method, and have text that changes to show updates."* Break the problem into smaller part. e.g. 1) Add a panel to the frame. By default, the panel will use a flow layout that allows more than one component to be added without needing any constraints. 2) Add a button and text field to the panel. 3) Make the button react to being activated. [How to Write an Action Listener](https://docs.oracle.com/javase/tutorial/uiswing/events/actionlistener.html).Just dump a string to the system out when it is activated. 4) Use .. – Andrew Thompson Mar 11 '17 at 07:42
  • 1
    .. the button action to write to the text field. [How to Use Text Fields](https://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html). Voting to close as 'too broad'. – Andrew Thompson Mar 11 '17 at 07:43
  • oh, ok. So add a panel, and then add the button and text to the panel? i think that may have been the part I was missing. Is that correct? – Fyrebend Mar 11 '17 at 07:50
  • *"So add a panel, and then add the button and text to the panel? i think that may have been the part I was missing. Is that correct?"* Well, there *are* other ways to do it. 1) You might use the default border layout of the content pane of the frame, and add the button and text field to different constraints of the content pane. 2) You might reset the content pane of the frame to use a flow layout. 3) .. - That's not only a singe part of the stated task, and there are multiple ways to do it. But from what you are asking, it seems you might also benefit from .. – Andrew Thompson Mar 11 '17 at 08:19
  • .. [Laying Out Components Within a Container](https://docs.oracle.com/javase/tutorial/uiswing/layout/). – Andrew Thompson Mar 11 '17 at 08:20
  • See also this related [Q&A](http://stackoverflow.com/q/24725420/230513). – trashgod Mar 11 '17 at 10:05

3 Answers3

1

You can put something like the following code into your "public static void main(String[] args)" method.

    JFrame frmMain = new JFrame(); // Create our JFrame
    // Set the layout for main frame. This controls how things get arranged on the screen
    frmMain.setLayout(new BorderLayout());

    // panels are what you put everything else on
    JPanel panel1 = new JPanel(new FlowLayout()); // another layout
    JPanel panel2 = new JPanel();
    BoxLayout box = new BoxLayout(panel2, BoxLayout.PAGE_AXIS); // another layout
    panel2.setLayout(box);

    // here are a couple of buttons
    JButton btnAdd = new JButton("Add");
    JButton btnRemove = new JButton("Remove");

    // here are a couple of textboxes. They accept typed in information
    JTextField txtFirstName = new JTextField();
    JTextField txtMiddleInitial = new JTextField();
    JTextField txtLastName = new JTextField();

    // add our buttons to panel1. It has a FlowLayout, so they will be centered left to right as we add them
    panel1.add(btnAdd); 
    panel1.add(btnRemove);

    // Create a panel to hold First Name
    JPanel pnlFirstName = new JPanel(new BorderLayout()); // also set its layout
    // here we add a JLabel.class they just display text, they don't allow input 
    pnlFirstName.add(new JLabel("first name"), BorderLayout.WEST);
    // here we put our text box onto the First name panel
    pnlFirstName.add(txtFirstName, BorderLayout.CENTER);

    // repeat for middle initial panel
    JPanel pnlMiddleInitial = new JPanel(new BorderLayout());
    pnlMiddleInitial.add(new JLabel("M.I."), BorderLayout.WEST);
    pnlMiddleInitial.add(txtMiddleInitial, BorderLayout.CENTER);

    // repeat for last name panel
    JPanel pnlLastName = new JPanel(new BorderLayout());
    pnlLastName.add(new JLabel("last name"), BorderLayout.WEST);
    pnlLastName.add(txtLastName, BorderLayout.CENTER);

    // put a 3 pixel border arounnd panel 2 to keep things away from the edge
    panel2.setBorder(new EmptyBorder(3, 3, 3, 3));
    // add all of our input panels to panel 2, according to BoxLayout (up above)
    panel2.add(pnlFirstName);
    panel2.add(pnlMiddleInitial);
    panel2.add(pnlLastName);

    // add panel1 and panel2 to the Frame. You have to add to the .getContentPane(), or you might mess things up.
    frmMain.getContentPane().add(panel1, BorderLayout.NORTH);
    frmMain.getContentPane().add(panel2, BorderLayout.CENTER);

    // This is how we tell the program what to do when the user presses the "Add" button.
    btnAdd.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            txtFirstName.setText("My First Name");
        }
    }); 
    // This is how we tell the program what to do when the user presses the "Remove" button.
    btnRemove.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            txtFirstName.setText("");
        }
    });

    // pack just makes everything take up it's proper space on the screen in as tight of a package as possible
    frmMain.pack();
    // if you don't set visible to true, you won't see your Frame
    frmMain.setVisible(true);
    // what to do when the user clicks the "X" to close or used "Close" from the context menu
    frmMain.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
bcr666
  • 2,157
  • 1
  • 12
  • 23
  • so i just put this together and theres only one thing i need different. instead of a text box where you can type, i need the button to display text that cannot be typed into. what would i use for this? -- it is specifically a countdown, if it helps. – Fyrebend Mar 11 '17 at 08:14
  • You can either use a JLabel (call it lblCountDown), and when the user clicks the button, you can call lblCountDown.setText("My Number"); or you can still use a text box and disable it txtCountDown.setEnabled(false); then users can't type in it, but it still looks different than a JLabel. you can change the text in it the same way as a JLable though (txtCountDown.setText("My Number"); – bcr666 Mar 11 '17 at 08:21
  • perfect! thank you so much for all the comments, its giving me a lot to study from! – Fyrebend Mar 11 '17 at 08:24
0

Go to bellow link here is the full example how to use button in java using Frame.

Here you got all the details about the Example.

http://www.javatpoint.com/java-jbutton

write your code in actionPerformed() method which you want to do on button click.

-1

going to file menu and export your project in runnable JAR file than you have a jar file to start in windows

  • I don't see how this relates to converting a command line application to a windowed application. It seems o be a decent (not great) answer to *"How to make an executable Jar?"* but that is.. **not** the question. You're also assuming that the OP is using a specific IDE. – Andrew Thompson Mar 11 '17 at 07:46
  • I don't know your ideas – MohammadReza Ghafarpour Mar 15 '17 at 09:47