0

I am new to JAVA swing , where i develop two different JFrame if I click on button frame should move to another frame and previous frame should close by opening of next frame.

I click on button a next frame open but data inside frame is not displaying and previous frame is not closed on button . Please help to find the problem in code.

Frame 1:---------------------------------

package com.demo.test;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.demo.gui.TestjigWindow;

public class TestjigWindowCheck extends JFrame{

private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;

public TestjigWindowCheck() {

    initUI();
}

private void initUI() {

    mainFrame = new JFrame("Fuse Test jig");
    mainFrame.setSize(400,400);
    mainFrame.setLayout(new GridLayout(3, 1));

    headerLabel = new JLabel("", JLabel.CENTER);        
    statusLabel = new JLabel("",JLabel.CENTER);    
    statusLabel.setSize(500,500);

    controlPanel = new JPanel();
    controlPanel.setLayout(new FlowLayout());

    mainFrame.add(headerLabel);
    mainFrame.add(controlPanel);
    mainFrame.add(statusLabel);
    mainFrame.setVisible(true);  
}

public void showEventDemo(){
    //TestjigWindow frame1 = new TestjigWindow();
    headerLabel.setText("Fuse Test Jig"); 
    headerLabel.setFont(new Font( "Arial", Font.BOLD, 25));
    headerLabel.setBackground(Color.green);

    JButton startButton = new JButton("Start");
    startButton.setActionCommand("Start");

    JButton closeButton = new JButton("Close");
    closeButton.setActionCommand("close");

    startButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            try
            {
             if(e.getSource() == startButton)
             {
              TestjigWindow2 frame2 = new TestjigWindow2();
              frame2.setVisible(true);
              dispose();
             } 
             else if(e.getSource() == closeButton)
             {
                 System.exit(0);
             }

            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
    });

   closeButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            try
            {
              System.exit(0);
            }
            catch (Exception ex)
            {
                ex.printStackTrace();
            }
        }
    });

    controlPanel.add(startButton);
    controlPanel.add(closeButton);

    mainFrame.setVisible(true);  
 }

public static void main(String[] args) {

    TestjigWindowCheck test = new TestjigWindowCheck();
    test.showEventDemo();
    //test.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     }
 }  

Frame 2----------------------------------- .

package com.demo.test;

import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.*;

public class TestjigWindow2 extends JFrame{
private JFrame mainFrame;
private JLabel headerLabel;
private JLabel statusLabel;
private JPanel controlPanel;
private JPanel controlPanel1;

public TestjigWindow2()
{
    prepareGUI();
}

public static void main(String args[])
{
    TestjigWindow2 test = new TestjigWindow2();
    test.showRadioButton();
}

private void prepareGUI()
{

    mainFrame = new JFrame("Fuse Test2 jig");
    mainFrame.setSize(400,400);
    mainFrame.setLayout(new GridLayout(3, 1));

    headerLabel = new JLabel("", JLabel.CENTER);        
    statusLabel = new JLabel("",JLabel.CENTER);    
    statusLabel.setSize(500,500);

    controlPanel = new JPanel();
    controlPanel.setLayout(new FlowLayout());

    mainFrame.add(headerLabel);
    mainFrame.add(controlPanel);
    mainFrame.add(statusLabel);
    mainFrame.setVisible(true);

}

public void showRadioButton()
{
    headerLabel.setText("Fuse Mode"); 
    final JRadioButton setting =new JRadioButton("Setting");    
    final JRadioButton testing =new JRadioButton("Testing");

    setting.setBounds(75,50,100,30);    
    testing.setBounds(75,100,100,30);

    setting.setMnemonic(KeyEvent.VK_S);
    testing.setMnemonic(KeyEvent.VK_T);

    ButtonGroup group = new ButtonGroup();

    group.add(setting);
    group.add(testing);

    controlPanel.add(setting);
    controlPanel.add(testing);

    JButton button = new JButton("Next");
    button.setActionCommand("Next");

    controlPanel.add(button);
    mainFrame.setVisible(true);  
 }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
VSDT
  • 11
  • 1

2 Answers2

1

For this problem, I think it's a fairly simple solution, as Andrew commented, you don't need to keep creating JFrames, you can create your JFrame in your first program, and pass it to your second class through the constructor.

Why I think your program is closing is because you are calling dispose() after creating the new frame which might be destroying the components in your new frame.

You could take this approach, which uses only one frame creating in the opening class and carried over to the second class

For Example (using snipplets of your code):

Frame 1

  //This is where you are moving to the second frame.
  if(e.getSource() == startButton)
  {
      mainFrame.getContentPane().removeAll(); //removeAll() method wipes all components attached to the contentpane of the frame
      //Frame can be reused when passed to second class
      TestjigWindow2 frame2 = new TestjigWindow2(this.mainFrame);

   } 

Frame 2

//In your constructor you could have something like this

private JFrame mainFrame; 
/*
 * Other variables and constants go here
 *
 */

public TestjigWindow2(JFrame mainFrame)
{
   this.mainFrame = mainFrame;
   prepareGUI();
}

And then in prepareGUI(), you would then be adding your components to your frame, without creating a new frame. With this, your first page will be closed, and the second frame will be open, without you having to creating mutiple JFrames.

Kebab Programmer
  • 1,213
  • 2
  • 21
  • 36
0

You should just create a new Instance of TestjigWindow2 in the actionPerformed method within your first frame. Instead of adding actionPerformed on the startbutton and stopbutton seperately, implement ActionListener interface in your Frame1 class and just keep one method since you are checking for the source inside the method anyways.

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        try
        {
         if(e.getSource() == startButton)
         {
          TestjigWindow2 frame2 = new TestjigWindow2();
          //frame2.setVisible(true); do this inside the frame2 preparegui method
          dispose();
         } 
         else if(e.getSource() == closeButton)
         {
             System.exit(0);
         }

        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
});

Also the code will have a more generalized flow if you have the main method inside Frame1 and instantiate the Frame1 in it.

And you don't need to use setVisible inside the actionPerformed of Frame1.

Hari Kiran
  • 188
  • 13