[java] how to create a window with two buttons that will open a new window

I need program - main JFrame have 2 buttons

  1. button
  2. button2

When I click button it has to open new JFrame window with new options, while if I click button2 then open another window.

In these 2 new windows I must add buttons like next and previous.

I have a problem, when I open button 1 , then open 2 windows and main JFrame still be visible.

My first program on swing:

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

public class example {

public static void main (String[] args){    
  JFrame frame = new JFrame("Test");
  frame.setVisible(true);
  frame.setSize(500,200);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  JPanel panel = new JPanel();
  frame.add(panel);
  JButton button = new JButton("hello agin1");
  panel.add(button);
  button.addActionListener (new Action1());

  JButton button2 = new JButton("hello agin2");
  panel.add(button2);
  button.addActionListener (new Action2()); 
}
static class Action1 implements ActionListener {        
  public void actionPerformed (ActionEvent e) {     
    JFrame frame2 = new JFrame("Clicked");
    frame2.setVisible(true);
    frame2.setSize(200,200);
    JLabel label = new JLabel("you clicked me");
    JPanel panel = new JPanel();
    frame2.add(panel);
    panel.add(label);       
  }
}   
static class Action2 implements ActionListener {        
  public void actionPerformed (ActionEvent e) {     
    JFrame frame3 = new JFrame("OKNO 3");
    frame3.setVisible(true);
    frame3.setSize(200,200);

    JLabel label = new JLabel("kliknales");
    JPanel panel = new JPanel();
    frame3.add(panel);
    panel.add(label);
  }
}   
}

This question is related to java swing jbutton actionlistener

The answer is


You add your ActionListener twice to button. So correct your code for button2 to

  JButton button2 = new JButton("hello agin2");
  panel.add(button2);
  button2.addActionListener (new Action2());//note the button2 here instead of button

Furthermore, perform your Swing operations on the correct thread by using EventQueue.invokeLater


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to swing

Calling another method java GUI Read input from a JOptionPane.showInputDialog box Call japplet from jframe Java JTable getting the data of the selected row What does .pack() do? How to add row of data to Jtable from values received from jtextfield and comboboxes How can I check that JButton is pressed? If the isEnable() is not work? Load arrayList data into JTable How to draw a circle with given X and Y coordinates as the middle spot of the circle? Simplest way to set image as JPanel background

Examples related to jbutton

Call japplet from jframe How can I check that JButton is pressed? If the isEnable() is not work? Java - Check if JTextField is empty or not how to create a window with two buttons that will open a new window How to close a GUI when I push a JButton? How to Disable GUI Button in Java How to add action listener that listens to multiple buttons Swing/Java: How to use the getText and setText string properly How to clear the JTextField by clicking JButton How do I add an image to a JButton

Examples related to actionlistener

Execute an action when an item on the combobox is selected JCheckbox - ActionListener and ItemListener? how to create a window with two buttons that will open a new window How to Disable GUI Button in Java How to add action listener that listens to multiple buttons Differences between action and actionListener How do you add an ActionListener onto a JButton in Java