1
votes

Using NetBeans IDE and Java, neither of which I'm overly familiar with. I have a JFrame laid out with a JPanel on it, and said JPanel has buttons on it. This JPanel is in the JFrames class. When one of these buttons is clicked, I would like to stop displaying this JPanel, and display another JPanel in a different class. I am using the GUI designer hence the panels being in different classes.

The buttonclick event in the JFrame class for one of the buttons I have:

    private void buttonActionPerformed(ActionEvent e) {                                         
    panel1.setVisible(false);
    Panel2 panel2 = new Panel2();
    this.add(panel2);
    panel2.setVisible(true);

This produces results of the panel1 disappearing however leaving a blank Jframe as panel2 does not display.

I have looked at CardLayout but I need the buttons on the panels to issue commands rather than buttons on another panel swapping around panels of a CardLayout.

EDIT: Have rewritten question to actually provide context and not just "help pls" as I've been told that what I was asking was too broad, which is completely correct. I've only just started learning Java so if this is still to broad aquestion, apologies.

3
Please see meta.stackoverflow.com/questions/284236/…... your question is too broad. Actually you don't have a specific question, you rather have the need that an experienced person sits down with you to discuss your idea, and to outline a solution, and who follows up with you during the 10, 20 steps required to get that working. But that is not what this community is for.GhostCat
Fair enough you are right. I'm just a bit frustrated as everything I've attempted so far has left me with a non-working app so this was kind of a last-ditch effort. I'll try to provide an edit that is more specific.user10772480
I understand such frustration. Maybe you have to focus a bit more on "basic" stuff. Instead of trying to turn your own vision / idea into a GUI app ... get a good book or tutorial, and first work through a complete example that others give you. Sure, it is not your idea then, but especially when doing GUI programming: there are a zillions of things you have to know in order to get it to do what you want to do. One subtle mistake, and the whole UI is messed up. Gaining that experience by trial and error is terribly hard. First gain experience by following a complete tutorial ... step by step.GhostCat
I have been that's why I'm here. I've been trial and error-ing for so long and I've not some to any concrete assertions as to where I'm going wrong or what I should be doing. I am currently rewriting to question with some context so as to not seem like a lost schoolkid.user10772480
Have updated question!user10772480

3 Answers

0
votes

Lets try to help a bit here ...

First of all, it is not a good idea to do more than "almost" nothing in action listener code. Especially instantiating a new panel object within that listener is a bad idea.

A more reasonable approach: create all your panel objects up front. Like: when your application comes up, maybe have a list of the required panel objects. And then, within your action listener code, simply add the one that needs to show up, and remove the one that isn't required any more.

Also note:

  • anything you "do" within an action listener happens on the event dispatcher thread (go google that term). here, that is probably what you want, but very often, it is not.
  • just calling setVisible() isn't enough. You might have to trigger the layout manager to do a repaint. Thus decide for a specific layout manager, and research that dynamically adding/hiding of components.
0
votes

i recomend to dispose the frame1 and set frame2 visible , where frame1 contaions the first panel and frame2 contains the second panel and i did not make the JButton btw just gave a solution , i did not compile it but i recommend doing something similar to this.

JFrame frame1;
JFrame frame2;
JPanel panel1;
JPanel panel2;

public ClassName() {
    frame1 = new JFrame("FIRST FRAME NAME");
    frame2 = new JFrame("SECOND FRAME NAME");

    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setSize(WIDTH , HEIGHT);
    frame1.add(panel1);
    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame2.setSize(WIDTH , HEIGHT);
    frame2.add(panel2);
    frame1.setVisible(true);
}

 private void buttonActionPerformed(ActionEvent e) {                                         
    frame1.dispose();
    frame2.setVisible(true);
}

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


0
votes

is this what your looking for ?

package project3;

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

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class File2 extends JFrame implements ActionListener{
    
    JPanel panel1;
    JPanel panel2;
    JButton button;
    JLabel label;
    
    public File2() {
        
        panel1 = new JPanel();
        panel2 = new JPanel();
        
        button = new JButton("CLICK ME");
        label = new JLabel("I AM A LABEL");
        
        button.addActionListener(this);
        
        panel1.add(button);
        panel2.add(label);
        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(400 , 400);
        this.add(panel2);
        this.add(panel1);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new File2();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        
        panel1.setVisible(false);
        this.remove(panel1);
        this.add(panel2);
        
    }

}