Skip to content
Advertisement

Opening a new window on a button click in java

I’m just starting with using interfaces in java, I’m okay with having just one window and that doing what i need. But i now want to link two windows together e.g.

Frame 1 opens. user chooses button1 (enter data). Frame 2 opens so user can enter the data.

Code for Frame 1:

import javax.swing.*;

public class Task_3 extends JFrame {
    private Button btn1, btn2, btn3;
    public Task_3(){

      setLayout(new FlowLayout());

      btn1 = new Button("Enter data");
      add(btn1);
      btn2 = new Button("Check who is going");
      add(btn2);
      btn3 = new Button("View costs");
      add(btn3);

      setTitle("Event Costs");
      setSize(280, 150);
      setVisible(true);

      // close the window
      addWindowListener(new WindowAdapter()
      {
      public void windowClosing(WindowEvent e)
      {
         dispose();
         System.exit(0); //calling the method is a must
      }
      });

    }
    public static void main(String[] args){
        new Task_3();
    }
}    

Code for Frame 2:

import java.awt.*;
import java.awt.event.*;

public class Task1GUI extends Frame implements ActionListener {
    private Label lblInput;
    private Label lblOutput;
    private TextField tfInput;
    private TextField tfOutput;
    private int sum = 0;

    public Task1GUI(){
        setLayout( new FlowLayout());

        lblInput = new Label("Enter number of students: ");
        add(lblInput);

        tfInput = new TextField(5);
        add(tfInput);

        tfInput.addActionListener(this);

        lblOutput = new Label("The cost per student is: ");
        add(lblOutput);

        tfOutput = new TextField(20);
        tfOutput.setEditable(false);
        add(tfOutput);

        setTitle("Task1GUI");
        setSize(350, 120);
        setVisible(true);

        addWindowListener(new WindowAdapter()
      {
      public void windowClosing(WindowEvent e)
      {
         dispose();
         System.exit(0); //calling the method is a must
      }
      });
    }
    public static void main(String[] args){

        new Task1GUI();
    }

    @Override
    public void actionPerformed(ActionEvent evt){
        int numOfStudents = Integer.parseInt(tfInput.getText());
        int coachCost = 550;
        int entrycost = 30;
        int totalcost;
        int numFree;
        int Discount;
        int costPerPerson;
        if(numOfStudents<45){
        totalcost = coachCost+(numOfStudents*30);
        numFree = numOfStudents/10;
        Discount = numFree*30;
        costPerPerson = (totalcost-Discount)/numOfStudents;
        tfInput.setText("");
        tfOutput.setText(costPerPerson+"");
    }
    else{
        tfOutput.setText("Too mant students entered");
    }
    }
}

Basically I would like some help with linking these two programs together so that the user can open the first frame choose which action they would like to do.

I have only been working with the console up until very recently so if my code is not perfect I apologise but I would really appreciate any help with this.

I would eventually like to get this program https://repl.it/repls/FondAptXeme to have a GUI if this helps at all.

Thanks

Advertisement

Answer

I would suggest creating a new JFrame and setting the visibility of your new JFrame to “true” in the button’s action listener. Something like this:

private JFrame secondFrame = new JFrame("My 2nd Window!");

and then in your button1 action listener you do:

secondFrame.setVisible(true);

EDIT:

import javax.swing.*;

public class Task_3 extends JFrame {
    private Button btn1, btn2, btn3;
    private Task1GUI task1Gui = new Task1GUI();

    public Task_3(){

      setLayout(new FlowLayout());

      btn1 = new Button("Enter data");
      btn1.addActionListener(this); //this refers to your current frame
      add(btn1);
      btn2 = new Button("Check who is going");
      add(btn2);
      btn3 = new Button("View costs");
      add(btn3);

      setTitle("Event Costs");
      setSize(280, 150);
      setVisible(true);

      // close the window
      addWindowListener(new WindowAdapter()
      {
      public void windowClosing(WindowEvent e)
      {
         dispose();
         System.exit(0); //calling the method is a must
      }
      });

    }
    public static void main(String[] args){
        new Task_3();
    }

    public void actionPerformed(ActionEvent e) {
        task1Gui.setVisible(true);
    }
}    

Cheers.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement