Results 1 to 4 of 4
- 06-07-2011, 05:58 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
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?
-
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(); } }); } }
- 06-07-2011, 06:33 AM #3
Member
- Join Date
- Jun 2011
- Posts
- 2
- Rep Power
- 0
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.
-
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
-
Closing 1 Desktop App, But Not The Other
By longphant in forum AWT / SwingReplies: 4Last Post: 08-25-2010, 10:38 PM -
closing FileOutputStream?
By mgrootsch in forum New To JavaReplies: 1Last Post: 05-17-2010, 05:36 PM -
Closing an Editor
By janpol1 in forum EclipseReplies: 0Last Post: 02-14-2009, 04:00 PM -
Can i use a resultset more than once without closing it
By haneeshrawther in forum JDBCReplies: 5Last Post: 06-26-2008, 03:16 AM -
Adding custom buttons - JOptionPane.showOptionDialog(...)
By Java Tip in forum Java TipReplies: 0Last Post: 12-17-2007, 09:46 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks