Henry Fantacussi
Henry Fantacussi

Reputation: 11

Making a JPanel Popup

I'm developing an app for computer, and i have a JFrame with a lot of JPanel on it, and when i click on a button, i want another JPanel to popup.

Example: When i click on this button http://i62.tinypic.com/c2fzr.jpg enter image description here

I want this window to popup http://i62.tinypic.com/2qi0in7.jpgenter image description here

I already tried making a popup menu, but i don't want a menu, i want a window, and i can't seen to find out how to do it :( It's probably easy, but i don't have enough knowledge in java

Any help? thanks guys!

Upvotes: 1

Views: 15512

Answers (3)

queeg
queeg

Reputation: 9394

Very likely you do not want a JFrame to popup but a modal dialog, and you might want your code to continue once the user has entered the necessary information.

The code for your button would look like this:

JButton button = new JButton("Nova Festa...");
button.addActionListener(new ActionListener() {

    NovaFestaPanel nfp = new NovaFestaPanel();  // this is a panel you create that contains all the fields that you expect and getters/setters for them
    nfp.setXXXX(); // use the setters to prepopulate the panel if you need

    if (JOptionPane.showInputDialog(this, nfp)==JOptionPane.OK_OPTION) {

        // the user entered some data and pressed the OK button.
        // use the getters and do the needful with these values
        String xxxx = nfp.getXXXX();

    }
});

Upvotes: 0

Vulovic Vukasin
Vulovic Vukasin

Reputation: 1748

Ok so,for this you will need 2 JFrames. First one is where the buttons and everything is and the second one is the one that will popup. You will have 3 classes: Main, classWhere1stJframeis, ClassWhere2ndJframeis.

This is main:

package proba;

import javax.swing.JFrame;

public class mejn {

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

        Frame1 frejm = new Frame1();
        frejm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frejm.setVisible(true);
        frejm.setSize(250, 300);
    }
}

This is Frame1:

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

import javax.swing.JButton;
import javax.swing.JFrame;

public class Frame1 extends JFrame {
    JFrame Frame = new JFrame();
    JButton Button1 = new JButton();

    public Frame1()
    {
        super("The title");

        Frame = new JFrame();
        Button1 = new JButton();
        Frame.add(Button1);

        thehandler handler = new thehandler();
        Button1.addActionListener(handler);
    }


    private class thehandler implements ActionListener
    {
        public void actionPerformed(ActionEvent event)
        {
            if(event.getSource()==Button1)
            {
                Frejm2 frejm = new Frejm2();
                frejm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frejm.setVisible(true);
            }
        }
    }
}

This is Frame2:

import javax.swing.JFrame;

public class Frejm2 extends JFrame {
    JFrame Frame2 = new JFrame();

    public Frejm2()
    {
        super("Title");
    }
}

Upvotes: 3

David Coler
David Coler

Reputation: 453

that is not just a panel you want to pop up that would be considered a whole other frame. I would suggest making a different JFrame class that when the button is clicked instantiates the other frame.

Upvotes: 1

Related Questions