Rafa579
Rafa579

Reputation: 19

ActionListener to call another Jpanel from a Jpanel

I am trying to make a Wizard without using libraries that I Have seen to easily make wizards lol its for a project, I have done the layout and frames and panels, what am having trouble with is when I click the "-->" it does not go to panel2 , nothing happens, it does store the name but thatt is it. Could anybody help me out?

EDIT it works now :) now am having trouble displaying the second "panel2" it goes into nothing after i click the arrow. lol

package project4;

import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class WizardGUI extends JFrame implements ActionListener {
    private JLabel enterName;
    private JTextField name;
    private JButton prev, fow;
    private String storeName = "";

WizardGUI(){
    super("Wizard");
  name();

}

void name()
{
      JPanel FPanel = new JPanel();
 this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  this.setLayout(new FlowLayout());
  // JLabel textLabel = new JLabel("lol");
 //textLabel.setPreferredSize(new Dimension(300, 100));
 // frame.getContentPane().add(textLabel); 
  //prev = new JButton("<--");
  fow = new JButton ("-->");

 this.add(FPanel);  
  enterName = new JLabel("Enter Your Name: ");
  name = new JTextField(10);
  enterName.setBounds(60, 30,120,30);
  name.setBounds(80,60,130,30);
  this.setSize(300,390); //set frame size
this.setVisible(true);
 FPanel.add(enterName);
 FPanel.add(name);
  //FPanel.add(prev);
   FPanel.add(fow);
   fow.addActionListener(this);

}


 void enter()
       {
JPanel panel2 = new JPanel();
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
  this.setLayout(new FlowLayout());

//prev = new JButton("<--");
  fow = new JButton ("-->");

 this.add(panel2);  
  enterName = new JLabel("Enter Your Name: ");
  name = new JTextField(10);
  enterName.setBounds(60, 30,120,30);
  name.setBounds(80,60,130,30);
  this.setSize(300,390); //set frame size
this.setVisible(true);
 panel2.add(enterName);
 panel2.add(name);
  //FPanel.add(prev);
   panel2.add(fow);
   fow.addActionListener(this);  
this.getContentPane().removeAll();
validate();
repaint();
this.add(panel2);
       }

void add()
{

}

void select()
{

}
 @Override
    public void actionPerformed(ActionEvent e) {
       if  (e.getSource() == fow) {
storeName = name.getText();
enter();

//JOptionPane.showMessageDialog(null, "hello " + storeName);
}


       }
    }

Thanks :)

Upvotes: 0

Views: 434

Answers (1)

Pace
Pace

Reputation: 43817

You both extend JFrame and you create your own JFrame. This means you have two instances of JFrame. One is your WizardGUI class which I'm guessing is referenced in your main somewhere and the other is a local variable named frame.

In the constructor you are building everything in the frame instance. In the Sscreen method you are modifying the this instance so nothing that you've done to the frame instance is modified.

You should get rid of the local variable frame and replace all references to it with this.

Also, you should call super("Wizard") as your first line in the WizardGUI constructor. Calling parent constructors is important and everyone forgets to do that.

Upvotes: 1

Related Questions