Results 1 to 4 of 4
  1. #1
    Rafik is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default Help with closing JOptionPane.showOptionDialog

    Hey, so i'm doing a GUI project for the end of the year, and because this would be my first time actually making my own program from scratch, I didn't plan anything. Therefore, unless I start over (not possible at this point) i can't really repaint most of my components.

    Since I can't repaint things, I figured I could bypass this for buttons in my JOptionPane.showOptionDialog by making the popup close as soon as a button is clicked. Here is the code for the popup:

    JOptionPane.showOptionDialog(myFrame, "Click an item to use it:", "Inventory", JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null,
    new Object[]{slotOne, slotTwo, "not done yet", "Return to Main Screen"}, null);


    slotOne and slotTwo are both JButtons instantiated elsewhere. They have actionListeners, and everything works correctly.

    When this popup shows up, i see the slotOne button, the slotTwo button, and two other buttons that say "not done yet" and "return to main screen". When I click on slotOne and slotTwo, they do what they are supposed to do. When I click on the other two buttons, however, they exit the popup.

    My question is: how do i get the JButtons slotOne and slotTwo to exit the popup, as well as completing their functions? What command would i have to add to their ActionListeners?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I wouldn't add any buttons to the array that you pass into your JOptionPane. Rather create an array of String and then do actions based on the int returned from the JOptionPane. The int will either be an int corresponding to the array index of the button pushed or else it will be == to the JOption.CLOSED_OPTION constant if the user cancels the JOptionPane. For instance:

    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    
    public class MyFoo extends JPanel {
    
       public MyFoo() {
          JButton showOptions = new JButton("Show Options");
          showOptions.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                String[] options = {"Slot One", "Slot Two", "Not Done Yet",
                   "Return to Main Screen"};
    
                int result = JOptionPane.showOptionDialog(MyFoo.this,
                         "Click an item to use it:", "Inventory",
                         JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE,
                         null, options, null);
                
                if (result == JOptionPane.CLOSED_OPTION) {
                   return;
                }
    
                System.out.println("Option selected is: " + options[result]);
                // here we decide what to do based on which item was selected.
             }
          });
          
          add(showOptions);
       }
    
       private static void createAndShowUI() {
          JFrame frame = new JFrame("MyFoo");
          frame.getContentPane().add(new MyFoo());
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          java.awt.EventQueue.invokeLater(new Runnable() {
             public void run() {
                createAndShowUI();
             }
          });
       }
    }

  3. #3
    Rafik is offline Member
    Join Date
    Jun 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks, seeing that actually also indirectly helped me with other issues I have with GUI programming.

    One last question: I was adding JButtons because I wanted the buttons that appeared to have ImageIcons. How would you give the buttons images if the array is made up of Strings? Could I just make an array of JButtons instead of Strings?

    If it's too complicated, I can deal with just having text and no pictures.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I don't know how to do this with ImageIcons. You could always add 4 buttons, but then you'd come into the issue of closing the JOptionPane again. If you're going to all that trouble then you might as well create your own JDialog and use that.

Similar Threads

  1. Closing 1 Desktop App, But Not The Other
    By longphant in forum AWT / Swing
    Replies: 4
    Last Post: 08-25-2010, 11:38 PM
  2. closing FileOutputStream?
    By mgrootsch in forum New To Java
    Replies: 1
    Last Post: 05-17-2010, 06:36 PM
  3. Closing an Editor
    By janpol1 in forum Eclipse
    Replies: 0
    Last Post: 02-14-2009, 05:00 PM
  4. Replies: 5
    Last Post: 06-26-2008, 04:16 AM
  5. Replies: 0
    Last Post: 12-17-2007, 10:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •