JDialog & JOptionPane Problems

I want to display a message(propably JOptionPane) which doesn't have the default answers such as "YES", "NO" or "CANCEL". Bu i want to add my options. I try with JDialog but the owner is a JInternalFrame and not a JFrame as it must be.
What i must do?
Any help is appreciated?

Object[] options = {"java", "C++", "C#"};
int option =JOptionPane.showOptionDialog(null,"Which language do you like best?","PROGRAM CHOICE",
JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null,options,options[0]);
if(option==0) System.out.println("Youchose java");
else if(option==1) System.out.println("Youchose C++");
else System.out.println("Youchose C#");

Similar Messages

  • Change Coffee cup icon on JDialog & JOptionPane

    I try to change the coffee cup icon on my JDialog and JOptionPane.
    My JDialog box does not load the Parent Icon (JFrame icon), and not even show a coffee cup icon.
    My JOptionPane would just show a coffee cup icon, eventhough its parent is the above JDialog with no icon.
    Is there anyone know how to fix this?
    I'd like show my icon on the JOptionPane instead of the coffee cup icon.
    And also to show my icon on the JDialog instead of nothing, not even a coffee icon.
    Any help is appreciated.
    CL

    Passing a parent with the desired icon to the JDialog/JOptionPane should solve your problem, however there is a bug in swing that prevents any icon from showing in the titlebar if a JDialog is made non-resizable (through a call to setResizable(false)).
    The bug adatabse says that this has been resolved but I am using jdk 1.3.1 and continue to have this problem.

  • JDialog sizing problems

    First of all, just let me say I have read everything there is available on layout managers and how pack() and validate() work.
    My problem is simply that my JDialog appears far longer than its components, and I can't see why.
    I've created screenshots of the desired appearance and the actual appearance:
    http://www.komododave.co.uk/gallery/main.php?g2_itemId=167
    The first 'undesired' screenshot shows what the JDialog looks like upon instantiation. The second shows what it looks like once you manually shrink it in the Y direction by more than about 10 pixels.
    I don't understand how the maximum size can exceed the Dimension I set with 'dialog.setMaximumSize(...', since I'm using BoxLayout for the JDialog and that respects a given maximum size.
    The constructor creating the JDialog is shown here:
              public AbstractSelectionDialog(Vector<String> argVector, String title,
                        String message) {
                   Vector<String> vector = argVector;
                   // create new modal JDialog
                   final JDialog dialog = new JDialog(Silk.getFrame(), title, true);
                   // set boxlayout for dialog frame
                   dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(),
                             BoxLayout.Y_AXIS));
                   // set default close operation
                   dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                   // create ButtonGroup to enforce exclusive collection choice
                   final ButtonGroup buttonGroup = new ButtonGroup();
                   // initialise other components
                   // use fake space for label rather than use another HorizontalGlue
                   JLabel label = new JLabel("   " + message);
                   // panel containing collection selection
                   JPanel selectionPanel = new JPanel();
                   // run initialiser methods
                   JPanel buttonPanel = (JPanel) createButtons(buttonGroup, dialog);
                   AbstractButton selection[] = createSelection(vector, buttonGroup,
                             selectionPanel);
                   // scrollPane containing collection panel
                   JScrollPane scrollPane = new JScrollPane(selectionPanel,
                             JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                             JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                   // panel containing scroll pane
                   JPanel scrollPanel = new JPanel();
                   scrollPanel.setLayout(new BoxLayout(scrollPanel, BoxLayout.X_AXIS));
                   selectionPanel.setLayout(new GridLayout(vector.size(), 1));
                   // assemble component structure
                   scrollPanel.add(Box.createHorizontalGlue());
                   scrollPanel.add(scrollPane);
                   scrollPanel.add(Box.createHorizontalGlue());
                   dialog.add(label);
                   dialog.add(scrollPanel);
                   dialog.add(buttonPanel);
                   // set all components to be left-justified
                   label.setAlignmentX(0.0F);
                   scrollPanel.setAlignmentX(0.0F);
                   selectionPanel.setAlignmentX(0.0F);
                   buttonPanel.setAlignmentX(0.0F);
                   // set higher-level component sizes
                   Silk.setSizeAttributes(label, 300, 30, true);
                   selectionPanel.setMaximumSize(new Dimension(250, 400));
                   scrollPane.setMinimumSize(new Dimension(250, 50));
                   scrollPane.setMaximumSize(new Dimension(250, 400));
                   dialog.setMinimumSize(new Dimension(300, 200));
                   dialog.setMaximumSize(new Dimension(300, 500));
                   dialog.pack();
                   // set components to be opaque
                   label.setOpaque(true);
                   scrollPane.setOpaque(true);
                   selectionPanel.setOpaque(true);
                   // display dialog frame
                   //dialog.setResizable(false);
                   dialog.setVisible(true);
              }The 'createButtons' method it uses is here:
         public JPanel createButtons(ButtonGroup bg, JDialog jd) {
                   final ButtonGroup buttonGroup = bg;
                   final JDialog dialog = jd;
                   // confirmation buttons
                   JButton okBtn = new JButton();
                   JButton cancelBtn = new JButton();
                   JButton newBtn = new JButton();
                   // panel containing OK and CANCEL buttons
                   JPanel bothPanel = new JPanel();
                   // panel containing NEW COLLECTION button
                   JPanel newPanel = new JPanel();
                   // panel to contain all other button panels
                   JPanel confPanel = new JPanel();
                   confPanel.setLayout(new BoxLayout(confPanel, BoxLayout.X_AXIS));
                   newPanel.setLayout(new BoxLayout(newPanel, BoxLayout.X_AXIS));
                   bothPanel.setLayout(new BoxLayout(bothPanel, BoxLayout.Y_AXIS));
                   // definition of button Actions
                   okBtn.setAction(new AbstractAction() {
                        public void actionPerformed(ActionEvent ae) {
                             Actions.addScript(buttonGroup.getSelection()
                                       .getActionCommand(), null);
                             dialog.dispose();
                   cancelBtn.setAction(new AbstractAction() {
                        public void actionPerformed(ActionEvent ae) {
                             dialog.dispose();
                   newBtn.setAction(new AbstractAction() {
                        public void actionPerformed(ActionEvent ae) {
                             dialog.dispose();
                             new Actions.NewCollectionAction().actionPerformed(ae);
                             new Actions.NewScriptAction().actionPerformed(ae);
                   // override action-set button text
                   okBtn.setText("Ok");
                   cancelBtn.setText("Cancel");
                   newBtn.setText("Add New Collection");
                   // set button sizes
                   Silk.setSizeAttributes(okBtn, 115, 30, true);
                   Silk.setSizeAttributes(cancelBtn, 115, 30, true);
                   Silk.setSizeAttributes(newBtn, 250, 35, true);
                   // set opaque buttons
                   confPanel.setOpaque(true);
                   okBtn.setOpaque(true);
                   cancelBtn.setOpaque(true);
                   // assemble components
                   confPanel.add(Box.createHorizontalGlue());
                   confPanel.add(okBtn);
                   confPanel.add(Box.createRigidArea(new Dimension(20, 0)));
                   confPanel.add(cancelBtn);
                   confPanel.add(Box.createHorizontalGlue());
                   newPanel.add(Box.createHorizontalGlue());
                   newPanel.add(newBtn);
                   newPanel.add(Box.createHorizontalGlue());
                   bothPanel.add(Box.createRigidArea(new Dimension(0, 10)));
                   bothPanel.add(confPanel);
                   bothPanel.add(Box.createRigidArea(new Dimension(0, 5)));
                   bothPanel.add(newPanel);
                   bothPanel.add(Box.createRigidArea(new Dimension(0, 10)));
                   bothPanel.setMaximumSize(new Dimension(300, 100));
                   return bothPanel;
              }And the createSelection() method it uses is defined here:
         public AbstractButton[] createSelection(Vector<String> argVector,
                        ButtonGroup buttonGroup, JPanel selectionPanel) {
                   Vector<String> vector = argVector;
                   JRadioButton selection[] = new JRadioButton[vector.size()];
                   for (int i = 0; i < vector.size(); i++)
                        selection[i] = new JRadioButton(vector.get(i));
                   AbstractButton current;
                   for (byte i = 0; i < selection.length; i++) {
                        current = selection;
                        // set action command string to later identify button that's
                        // selected
                        current.setActionCommand(vector.get(i));
                        // fix button's size
                        Silk.setSizeAttributes(current, 250, 30, true);
                        // set button's colour
                        current.setBackground(Color.WHITE);
                        // add button to buttongroup
                        buttonGroup.add(current);
                        // add button to appropriate JPanel
                        selectionPanel.add(current);
                   // ensure one button is selected to prevent pressing OK with no
                   // selection
                   selection[0].setSelected(true);
                   return selection;
    Please help if you can.
    Thank you.
    - Dave

    Yeah sorry camickr, I need to get into the habit of posting SSCCEs.
    Here it is in SSCCE form:
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import javax.swing.AbstractAction;
    import javax.swing.AbstractButton;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.ButtonGroup;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JRadioButton;
    import javax.swing.JScrollPane;
    import java.util.Vector;
    public class Testing {
         // prevent instantiation
         private Testing() {
         public interface SelectionDialog {
              // produces a component containing all dialog buttons
              public Component createButtons(ButtonGroup bg, JDialog jd);
              // produces an array of the selection buttons to be used
              public abstract AbstractButton[] createSelection(
                        Vector<String> selectionVector, ButtonGroup buttonGroup,
                        JPanel selectionPanel);
         abstract static class AbstractSelectionDialog implements SelectionDialog {
              public AbstractSelectionDialog() {
                   Vector<String> vector = new Vector<String>();
                   String title = "Broken dialog box";
                   String message = "Why is the maximum size exceedable, hmm?";
                   /* for sscce */
                   vector.add("choice 1");
                   vector.add("choice 2");
                   vector.add("choice 3");
                   vector.add("choice 4");
                   vector.add("choice 5");
                   vector.add("choice 6");
                   vector.add("choice 7");
                   vector.add("choice 8");
                   vector.add("choice 9");
                   vector.add("choice 10");
                   vector.add("choice 11");
                   vector.add("choice 12");
                   vector.add("choice 13");
                   vector.add("choice 14");
                   vector.add("choice 15");
                   vector.add("choice 16");
                   vector.add("choice 17");
                   vector.add("choice 18");
                   // create new modal JDialog
                   final JDialog dialog = new JDialog(Silk.getFrame(), title, true);
                   // set boxlayout for dialog frame
                   dialog.getContentPane().setLayout(new BoxLayout(dialog.getContentPane(),
                             BoxLayout.Y_AXIS));
                   // set default close operation
                   dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
                   // create ButtonGroup to enforce exclusive collection choice
                   final ButtonGroup buttonGroup = new ButtonGroup();
                   // initialise other components
                   // use fake space for label rather than use another HorizontalGlue
                   JLabel label = new JLabel("   " + message);
                   // panel containing collection selection
                   JPanel selectionPanel = new JPanel();
                   // run initialiser methods
                   JPanel buttonPanel = (JPanel) createButtons(buttonGroup, dialog);
                   AbstractButton selection[] = createSelection(vector, buttonGroup,
                             selectionPanel);
                   // scrollPane containing collection panel
                   JScrollPane scrollPane = new JScrollPane(selectionPanel,
                             JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                             JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
                   // panel containing scroll pane
                   JPanel scrollPanel = new JPanel();
                   scrollPanel.setLayout(new BoxLayout(scrollPanel, BoxLayout.X_AXIS));
                   selectionPanel.setLayout(new GridLayout(vector.size(), 1));
                   // assemble component structure
                   scrollPanel.add(Box.createHorizontalGlue());
                   scrollPanel.add(scrollPane);
                   scrollPanel.add(Box.createHorizontalGlue());
                   dialog.add(label);
                   dialog.add(scrollPanel);
                   dialog.add(buttonPanel);
                   // set all components to be left-justified
                   label.setAlignmentX(0.0F);
                   scrollPanel.setAlignmentX(0.0F);
                   selectionPanel.setAlignmentX(0.0F);
                   buttonPanel.setAlignmentX(0.0F);
                   // set higher-level component sizes
                   Silk.setSizeAttributes(label, 300, 30, true);
                   selectionPanel.setMaximumSize(new Dimension(250, 400));
                   scrollPane.setMinimumSize(new Dimension(250, 50));
                   scrollPane.setMaximumSize(new Dimension(250, 400));
                   dialog.setMinimumSize(new Dimension(300, 200));
                   dialog.setMaximumSize(new Dimension(300, 500));
                   dialog.pack();
                   // set components to be opaque
                   label.setOpaque(true);
                   scrollPane.setOpaque(true);
                   selectionPanel.setOpaque(true);
                   // display dialog frame
                   // dialog.setResizable(false);
                   dialog.setVisible(true);
         protected static class ChooseCollectionDialog extends
                   AbstractSelectionDialog implements SelectionDialog {
              public ChooseCollectionDialog() {
                   super();
              public JPanel createButtons(ButtonGroup bg, JDialog jd) {
                   final ButtonGroup buttonGroup = bg;
                   final JDialog dialog = jd;
                   // confirmation buttons
                   JButton okBtn = new JButton();
                   JButton cancelBtn = new JButton();
                   JButton newBtn = new JButton();
                   // panel containing OK and CANCEL buttons
                   JPanel bothPanel = new JPanel();
                   // panel containing NEW COLLECTION button
                   JPanel newPanel = new JPanel();
                   // panel to contain all other button panels
                   JPanel confPanel = new JPanel();
                   confPanel.setLayout(new BoxLayout(confPanel, BoxLayout.X_AXIS));
                   newPanel.setLayout(new BoxLayout(newPanel, BoxLayout.X_AXIS));
                   bothPanel.setLayout(new BoxLayout(bothPanel, BoxLayout.Y_AXIS));
                   // definition of button Actions
                   okBtn.setAction(new AbstractAction() {
                        public void actionPerformed(ActionEvent ae) {
                             dialog.dispose();
                   cancelBtn.setAction(new AbstractAction() {
                        public void actionPerformed(ActionEvent ae) {
                             dialog.dispose();
                   newBtn.setAction(new AbstractAction() {
                        public void actionPerformed(ActionEvent ae) {
                   // override action-set button text
                   okBtn.setText("Ok");
                   cancelBtn.setText("Cancel");
                   newBtn.setText("Add New Collection");
                   // set button sizes
                   Silk.setSizeAttributes(okBtn, 115, 30, true);
                   Silk.setSizeAttributes(cancelBtn, 115, 30, true);
                   Silk.setSizeAttributes(newBtn, 250, 30, true);
                   // set opaque buttons
                   confPanel.setOpaque(true);
                   okBtn.setOpaque(true);
                   cancelBtn.setOpaque(true);
                   // assemble components
                   confPanel.add(Box.createHorizontalGlue());
                   confPanel.add(okBtn);
                   confPanel.add(Box.createRigidArea(new Dimension(20, 0)));
                   confPanel.add(cancelBtn);
                   confPanel.add(Box.createHorizontalGlue());
                   newPanel.add(Box.createHorizontalGlue());
                   newPanel.add(newBtn);
                   newPanel.add(Box.createHorizontalGlue());
                   bothPanel.add(Box.createRigidArea(new Dimension(0, 10)));
                   bothPanel.add(confPanel);
                   bothPanel.add(Box.createRigidArea(new Dimension(0, 5)));
                   bothPanel.add(newPanel);
                   bothPanel.add(Box.createRigidArea(new Dimension(0, 10)));
                   bothPanel.setMaximumSize(new Dimension(300, 100));
                   return bothPanel;
              public AbstractButton[] createSelection(Vector<String> argVector,
                        ButtonGroup buttonGroup, JPanel selectionPanel) {
                   Vector<String> vector = argVector;
                   JRadioButton selection[] = new JRadioButton[vector.size()];
                   for (int i = 0; i < vector.size(); i++)
                        selection[i] = new JRadioButton(vector.get(i));
                   AbstractButton current;
                   for (byte i = 0; i < selection.length; i++) {
                        current = selection;
                        // set action command string to later identify button that's
                        // selected
                        current.setActionCommand(vector.get(i));
                        // fix button's size
                        Silk.setSizeAttributes(current, 250, 30, true);
                        // set button's colour
                        current.setBackground(Color.WHITE);
                        // add button to buttongroup
                        buttonGroup.add(current);
                        // add button to appropriate JPanel
                        selectionPanel.add(current);
                   // ensure one button is selected to prevent pressing OK with no
                   // selection
                   selection[0].setSelected(true);
                   return selection;
         public static void main(String args[]) {
              javax.swing.SwingUtilities.invokeLater(new Runnable() {
                   public void run() {
                        new ChooseCollectionDialog();
    I considered that the GridLayout might be a problem, but surely the fact that it's ultimately contained in a BoxLayout (which [i]does respect maximum size) means the maximum size still shouldn't be exceedable?
    Thanks.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • JOptionPane Problem, pls reply

    Hi, I am having a problem with JOptionPane that I have noticed other people have had but I could not find a solution. Here it is:
    When I use a Showconfirmdialog. It works fine in the develop. environment buts has a bug when I view it in a browser. About 20% of the time, it cuts off the last two letters and replaces them with ... If I stretch the box out the message displays as it should.
    Here is my code:
    java.awt.Frame f = (java.awt.Frame)((MainTabPage)getController().getMainTabPage()).getParentContainer();
              int n = JOptionPane.showConfirmDialog(f,"Changes have been made. Would you like to save?", "User Profile", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
              if (n == JOptionPane.YES_OPTION){
                   this.btnSave_ActionPerformed(actionEvent);
                   return;
    and it goes on for the no and cancel option as well.
    ANY help would be greatly app.
    Thanks

    the best way should be to "pack()" the dialog, but as you can't access thatdialog instance...
    I suggest you use html tags :
    simply change your String :
    "Changes have been made. Would you like to save?"
    by
    "<html>Changes have been made. <br>Would you like to save?</html>"the <br> tag indicates a lineBreak
    moreover, you could use the tagts to display some parts of the string as bold or itaic of in colours...
    vincent

  • JDialog setModal problems

    hi, i have a JDialog that needs to be modal,
    in the jdialog is a JList that gets filled up with a DefaultListModel after the dialog is instantiated, the problem now is when i have setModal ( true ), the JList doesn't fill up ?
    Anyone know a solution to this ?

    Does your code look something like this?
    JDialog dialog = new JDialog(parent);
    dialog.show();
    dialog.fillJList();
    If so, it won't work because the line dialog.fillJList() is not executed until the dialog is closed. Make sure you populate the JList in the constructor of the dialog.

  • JDialog Notification Problem

    I have a class that makes a JDialog popup notification display, letting you know that you have new email. I have set the popup to be always on top. The problem is, I can't figure out how to keep the popup dialogs from making the current window, for example FireFox, become deactivated. When the Firefox window becomes deactivated, you need to click inside the window for it used again.
    Is there anyway to make the popups always display on top of other windows but force them to display in a 'ghosted out' state until the user clicks on them?
    Thanks in advance,
    Garrett

    SettingsWindow settings = new SettingsWindow(new JFrame());You don't use "new JFrame()". A new frame is not visible anywhere. You use your current application frame.

  • JDialog focus problem

    I have an application which contains different panels and displays a message box which is a JDialog. To avoid recreate JDialog, I create one static dialog which is shared by all panels. I only set it visible at anytime needed. It works fine. The problem is I can't get focus on that dialog at the first time it shows up, I have to click some button on that dislog to gain focus even though I call button.requestFocus() right before it shows up. The problem only occurs at the first time it shows up. Who can solve this problem?

    It needs to have an owner to go ontop of, like Dilaog has...
    Frame parent = new Frame ();
    Dialog d = new Dialog (parent, blah blah);
    So now it knows it has to be in front of 'parent'. I assume it's the same for JDialog, I forget though. Check it out anywho.

  • JDialog setLocation problem

    Hi,
    I am stuck in very weird problem:
    I have an Jinternal frame that invokes a non-modal Jdialog box.
    I need to set the location of the dialog box from my code.
    The setLocation takes effect until I drag my window to a new location with my mouse.
    Any calls to setLocation after dragging fails. I see that the dialog box is temporarily drawn at the new position but it quickly moves back to its last known position.(I can see a sort of flicker...)
    I tried SetBounds, reshape also - same behaviour...
    I am so puzzled.
    Any help will be appreciated.

    If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program (SSCCE) that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.
    Don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags so the posted code retains its original formatting.

  • JDialog size problem

    Hi,
    I'd appreciate if someone could help me with this problem: I try create a custom dialog window. It pops up OK, but it defaults to the SMALLEST size possible. So i subsequently have to drag it bigger... Also, how do I suspend access to the primary gui while the dialog box is still unanswered?
    Code:-
    AutoGen ag = new AutoGen(); // my custom gui for dialog
    JDialog dialog = new JDialog();
    dialog.getContentPane().setLayout(new BorderLayout());
    dialog.setContentPane(ag);
    dialog.setVisible(true);

    dialog.getContentPane().setLayout(new BorderLayout()); // not required BorderLayout is the default
    dialog.setContentPane(ag);
    dialog.pack(); // sizes the dialog to the preferred size of your AutoGen panel
    dialog.setVisible(true);

  • JOptionpane Problem

    Hey all
    I have a query on the following code i hope some one can help with. In the following code when it executes a frame is displayed with three buttons, when you click the first button it brings up an JOptionpane which is all working fine its just the System.exit(0) i cant seem to get it to work. I dont know where to put in in realtion the switch
    Thanks for all help
    Ambrose
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.event.*;
    import java.io.*;
    public class Switching extends JFrame implements ActionListener
         private JTextArea ta1;
         private JButton b1;
         private JButton b2;
         private JButton b3;
         private JLabel l1;
         //private JTextField t1;
         double number1;
         double number2;
         int productId = 1;
         double product1 = 0, product2 = 0, product3 = 0, product4 = 0, product5 = 0;
         public Switching(String str)
              super(str);
             //Container need to hold all the components
             Container c = getContentPane();
             c.setLayout(null);
              //Size and location
              setSize(507,550);
              setLocationRelativeTo(null);
              //Declared
              b1 = new JButton ("Start the Application");
              b2 = new JButton ("Print");
              b3 = new JButton ("Exit Application");
              ta1 = new JTextArea (5, 5);
              //t1 = new JTextField (5);
            //Scroll Pane needed for the text area
                JScrollPane scrollPane =  new JScrollPane(ta1,
            JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
            JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
            //Label added
            l1 = new JLabel(
            "Mail order House", SwingConstants.CENTER );
            //l7.setForeground( colorValues[ 0 ] );
               l1.setFont(
            new Font( "Comic Sans MS", Font.BOLD, 40 ) );
            //Components added to the container
            c.add (b1);
            c.add (b2);
            c.add (b3);
            c.add (scrollPane);
            c.add (l1);
            //c.add (t1);
            //Tool tips for accessibility options
            b1.setToolTipText ("Click to Calculate Start the Program");
              ta1.setText("Please use the Button Calculate to Interest Rates");
            //Locations of all the variables
            b1.setBounds (165, 390, 185, 25);
            b2.setBounds (165, 440, 185, 25);
            b3.setBounds (165, 490, 185, 25);
            //t1.setBounds (10, 120, 100, 25);
            scrollPane.setBounds (10, 170, 300, 100);
            l1.setBounds (36, 45, 465, 40);
            //Action Listener added to the buttons
            b1.addActionListener(this);
            b2.addActionListener(this);
            b3.addActionListener(this);
         public void actionPerformed( ActionEvent e )
                     //Target Source Needed
                     Object Target = e.getSource();
           if ( Target ==b1)
                     while (productId != 0)
                          String inputString = JOptionPane.showInputDialog
                          ("Enter product number ( 1-5 ) ( 0 to stop ):" );    
                          //String inputString = JOptionPane.showInputDialog( "Enter first integer:" );
                          productId = Integer.parseInt( inputString );
                          /*System.exit( 0 );*/ 
                     if ( productId >= 1 && productId <= 5 )    
                        String inputString1 = JOptionPane.showInputDialog( "Enter quantity sold:" );
                      int quantity = Integer.parseInt( inputString1 );        
                      switch ( productId )
                              case 1: product1 += quantity * 2.98; 
                                break;     
                             case 2: product2 += quantity * 4.50;   
                               break;      
                             case 3: product3 += quantity * 9.98;          
                                   break;          
                            case 4: product4 += quantity * 4.49;            
                              break;        
                            case 5: product5 += quantity * 6.87;    
                              break;      
                  else if ( productId != 0 )
                           String output = "Incorrect Number Entered\n Please Enter Again ! ";
                        JOptionPane.showMessageDialog(null, output, "Attempt One ",
                        JOptionPane.WARNING_MESSAGE);
                     if ( Target == b2)
                          System.out.println("ee");     
                          ta1.setText("");
                          ta1.setText("Product One:"+"    "+product1 +
                          "\nProduct Two:"+"    "+product2 +
                          "\nProduct Three: "+"  "+product3 +
                          "\nProduct Four:"+"    "+product4 +
                          "\nProduct Five:"+"    "+product5);
         public static void main ( String args [])
              //Create an new Instance of the Form
              Switching In = new Switching("Shop Form");
              //In.setDefaultLookAndFeelDecorated(true);
              In.show();
    }

    Add something like
    if (target == b3) {
        System.exit(0);
    }To your ActionListener. Make sure it's outside of the other if statements. Also, you can use JFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) to have the application exit when a user closes your window using the normal OS method (the X in windows).

  • JOptionPane. problems

    i am trying to do a program. i need to accept two numbers using two seperate dialog box and then add these numbers and present the result using an another dialog box . i am a new user to java. can somebody please help me out. PLease

    Hmmph - shudder and yuk!You're right, just showing the components to use and asking the OP to implement his own method for their use is not good.
    Here's a (perhaps) better version for the OP.
    import javax.swing.JOptionPane;
    class Testing
      public Testing()
        String sNum1, sNum2;
        int iNum1, iNum2, numsAdded = 0;
        boolean numbersValid = false;
        while(!numbersValid)
          try
            sNum1 = JOptionPane.showInputDialog("enter a number");
            iNum1 = Integer.parseInt(sNum1);
            sNum2 = JOptionPane.showInputDialog("enter another number");       
            iNum2 = Integer.parseInt(sNum2);
            numsAdded = iNum1 + iNum2;
            numbersValid = true;
          catch(Exception e){JOptionPane.showMessageDialog(null,"error in numbers, try again");}
        JOptionPane.showMessageDialog(null, "numbers added = " + numsAdded);
        System.exit(0);
      public static void main(String[] args) {new Testing();}
    }

  • JOptionPane Problem Plz help me

    I want to add an image button on JOptionPane
    how can i do?
    Means change the default button For JOptionPane.OK_OPTION
    Thanks in advance

    set your FORMS60_PATH on the client to Formspath;libpath;menupath so it looks in every dir named in the path.

  • JDialog visibility problem

    Hi,
    I am modifying the visibility of a JDialog inside a util.Timer with a simple method. However, although I use this.setVisible(false) to hide the dialog, sometimes it remains on the screen. When I use this.dispose() instead, it does not remain on the screen. Does anyone know why setVisible(false) does not work sometimes? I also deal with threads in the program, can it be related with my misuse of threads (however if it is the case, sometimes dispose() should have not worked, too I think)

    I also deal with threads in the
    program, can it be related with my misuse of threadsYes. You should never do actions that affect the GUI from a non-event dispatch thread. Use SwingUtilities.InvokeLater to make changes from other threads.

  • JDIalog appearance problem

    Hi all,
    can anybody help me out......
    my GUI application has appearance of being locked or frozen when modal dialog box present and
    when another application is brought to the foreground. The modal dialog is not
    present giving the main window the appearance of being frozen. User has to
    minimize all other applications and then the main window and the modal dialog
    will appear.
    Thanks in advance

    If you create a dialog using no parameters ...
    JDialog() creates a non-modal dialog without a specified Frame owner.
    A shared, hidden frame will be set as the owner of the dialog.
    When you create your JDialog pass you application's frame as the owner.

  • JOptionPane Problem: Pl Help

    I am trying to change the default selected option of showConfirmDialog to cancel instead of ok. Please suggest how i can do it.

    Hi,
    You can't change that with showConfirmDialog.
    Use showOptionDialog instead.
    One of the overloaded function takes the available options (say ok, cancel) and u can specify the default selection as well.
    Regards,
    [email protected]

Maybe you are looking for

  • E72 talk with the nokia customer care

    i tried to talk to them with all the problems we E72 users are facing, but seems somehow that they either keep avoiding the question, or don't answer the question, but give answers that slightly have something to do with the question. either way thei

  • Lost my serialnumber to Photoshop Elements 10. And my email isn't no longer in my mailbox

    Lost my serialnumber to Photoshop Elements 10. Downloaded it to my laptop at the time I bought it, and now I would like to have it on the stationery computer at home. Think I deleted the email from Adobe and if I look in my Adobe-ID there isn't any p

  • Types Of Datasources in BI 7.0

    Hi Gurus,    Pls let me know the types of Datasources  available in BI7.0. Regards Vishal

  • Help if possible for smart editor

    Hello java gurus, Presently Iam building my own editor for a new language.I want to make this an efficient editor. By an efficient editor i mean that when a very big file is opened into it, the whole file should not be there in the memory.I want only

  • K_PCA - EC-PCA: Responsibility Area, Profit Center

    Hi Experts Is there any report which will show which user has k_pca profit center access PFCG-> Z_USER_ROLE->AUTHORIZATIONS TAB-> change authorization data->Manually tap button type k_pca and press enter to insert authorization then you can see contr