Question

So basically I have this button. (rightbutton)

I want it so when you click on this button a notification/popup appears that says "Congratulations, you clicked a button." then just say "OK" and "Cancel" (both just exit out the panel, same "task" really)

I used a JOptionPane but that of course would not work because it has a big text bar which I don't want.

So I'm guessing you'd have to make a JPanel and add the JButtons "OK" and "Cancel" inside that panel? I'm stumped.

rightbutton = new JButton("Right.");
    add(rightbutton);
    rightbutton.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    //what do we want to happen when we
                    //click the button
                    JOptionPane.showInputDialog("Congratulations, you clicked the button.");

thanks!

No correct solution

OTHER TIPS

You can customize the JOptionPane component by passing some arguments to it.

public static int showOptionDialog(Component parentComponent,
                               Object message,
                               String title,
                               int optionType,
                               int messageType,
                               Icon icon,
                               Object[] options,
                               Object initialValue)
                        throws HeadlessException


optionType
Defines the set of option buttons that appear at the bottom of the dialog box:
    DEFAULT_OPTION
    YES_NO_OPTION
    YES_NO_CANCEL_OPTION
    OK_CANCEL_OPTION

Is OK_CANCEL_OPOTION what you want?

try this it may help

rightbutton = new JButton("Right.");
close = new JButton("close.");
    add(rightbutton);
    add(close)
    rightbutton.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    //what do we want to happen when we
                    //click the button
                    JOptionPane.showInputDialog("Congratulations, you clicked t
                        }

    close.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent event){
                    //what do we want to happen when we
                    //click the button
                                  int dialogButton = JOptionPane.YES_NO_OPTION;
            JOptionPane.showConfirmDialog (null, "Do you want to  close","Warning",dialogButton);

                if(dialogButton == JOptionPane.YES_OPTION){ //The ISSUE is here
                        System.exit(0);
                        }

I try to hide JOptionPane's title bar.It is not ok.But by using "Popup" window,it will help you to get what you want.I try to use it,"Popup".It works well.Code is here.

 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;
import javax.swing.Popup;
import javax.swing.PopupFactory;

public class SwingTest extends JFrame{
    JButton rightbutton,btnOk,btnCancel;
    JPanel panel;
    JLabel lblMsg;
    Popup popup;
    public SwingTest(){
        setSize(300,300);
        rightbutton=new JButton("Right");
        rightbutton.setBounds(100, 100, 80, 30);
        //Message you want to show
        lblMsg=new JLabel("Congratulations, you clicked the button.");
        lblMsg.setBounds(30, 30, 200, 18);
        btnOk=new JButton("Ok");
        btnOk.setBounds(30, 60, 30, 30);
        btnCancel=new JButton("Cancel");
        btnCancel.setBounds(70, 60, 80, 30);

        panel=new JPanel();
        panel.setSize(250,200);
        panel.add(lblMsg);
        panel.add(btnOk);
        panel.add(btnCancel);

        //Create Popup
        PopupFactory factory=PopupFactory.getSharedInstance();
        popup=factory.getPopup(this, panel, 100, 100);

        add(rightbutton);
        rightbutton.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent event){
                       popup.show();
                    }
                });


        btnOk.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent event){
                       popup.hide();
                    }
                });
        btnCancel.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent event){
                       popup.hide();
                    }
                });
    }

    public static void main(String[] args) {
        new SwingTest().setVisible(true);
    }

}

May this code be a part of that you want.Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top