Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 15 of 15

Thread: Making an input dialog box appear after closing a combo box

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Making an input dialog box appear after closing a combo box

    Hey guys, I'm trying to create a program that works like a menu for specials at a cafe. When it runs, the combo box appears and everything works as it should, but I can't figure out how to add an input dialog box to appear after the combo box is closed. Someone on dreamincode said this to me:

    You seem to miss some general concept

    GUI applications are event driven...
    you display some components an depending the way user interact with them you react to them

    JOptionPane is not such a component. The display of a JOptionPane should be triggered by a JButton, or another situation. You cannot display a JOptionPane in the normal GUI bulding process.
    So.. do I need to add a JButton to create the input dialog? I've added the JOptionPane.showInputDialog code before, after the Combo Box constructor end curly brace, and it worked. But, the input dialog box appeared before the combo box did, which is not something I'd like to happen. :p How do I go about doing this?

    Bear in mind, I'm pretty new to Java. I copied the code for this combo box out of my book, which is why you see variable names starting with flag (it was originally supposed to show flags and their descriptions). So I don't entirely understand what a JButton is or how to add one. I just really need some help.

    Here's my code:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Scanner;
     
    public class ComboBoxDemo extends JFrame {
     
      private double order = 0;
     
     
     // Declare an array of Strings for flag titles
     	private String[] flagTitles = {"The Big Easy", "The Sophisticated Ensemble", "The Delectable"};
     
     // Declare an ImageIcon array for the national flags of 9 countries
     private ImageIcon[] flagImage = {
     	new ImageIcon("Images/bigeasy.jpg"),
     	new ImageIcon("Images/sophisticatedensemble.jpg"),
     	new ImageIcon("Images/thedelectable.jpg"),
      };
     
     // Declare an array of strings for flag descriptions
    private String[] flagDescription = new String[3];
     
     // Declare and create a description panel
     
    private JTextPane Description = new JTextPane(); 
     
    private JLabel flagImageLabel = new JLabel(); 
     
    private JComboBox jcbo = new JComboBox(flagTitles);
     
     // Create a combo box for selecting countries
     
     
     public static void main(String[] args) {
     	ComboBoxDemo frame = new ComboBoxDemo();
     	frame.pack();
     	frame.setTitle("Modern Papyrus Cafe Menu");
     	frame.setLocationRelativeTo(null); // Center the frame
     	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     	frame.setVisible(true);
     }
     
     public ComboBoxDemo() {
     // Set text description
     	flagDescription[0] = "1: The Big Easy is, simply put, a big and easy meal!\n" +
    	"It consists of 1 hearty sandwich, stuffed with anything you want\n" +
    	"1 bag of crunchy and savory chips, of your choice\n" +
    	"and 1 large drink, all for $10.99!\n" +
    	"And we've got better news: you can also include any book you choose for 25% off,\n" +
    	"just for buying this meal!";
     
    	flagDescription[1] = "2: The Sophisticated Ensemble is for those that want to\n" +
     	"take delight in a warmer, sit-by-the-fire kind of meal. It contains:\n " +
     	"1 pastry, donut, brownie, or slice of cake\n" +
     	"and 1 coffee, latte, or tea, all for $8.99!\n" +
     	"To finalize the deal, you may add a book to your purchase for 15% off!";
     
    	flagDescription[2] = "3: The Delectable is for the sweet tooth! You may have\n" +
    	"1 milkshake, smoothie, or hot chocolate\n" +
    	"and 1 pastry, donut, brownie, or slice of cake, all for $7.99!\n" +
    	"To sweeten the deal, you may add a book to your purchase for 15% off!";
     
    	 // Set the first country (Canada) for display
     	setDisplay(0);
     
    	Description.setText(flagDescription[0]);
    	flagImageLabel.setIcon(flagImage[0]);
    	Description.setPreferredSize(new Dimension(400,300));
    	flagImageLabel.setPreferredSize(new Dimension(300,200));
     
     
     // Add combo box and description panel to the list
     	 add(jcbo, BorderLayout.NORTH);
     	 add(Description, BorderLayout.CENTER);
    	 add(flagImageLabel, BorderLayout.WEST);
     
     
     
     // Register listener
     	jcbo.addItemListener(new ItemListener() {
     /** Handle item selection */
     		public void itemStateChanged(ItemEvent e) {
     			Description.setText(flagDescription[jcbo.getSelectedIndex()]);
    			flagImageLabel.setIcon(flagImage[jcbo.getSelectedIndex()]);
     		}
     	});
     
     }
     
     /** Set display information on the description panel */
     public void setDisplay(int index) {
     
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making an input dialog box appear after closing a combo box

    after the combo box is closed
    When does the combo box close? The sample code you posted always shows the combo box.

    Can you explain the sequence of events you want the user to see?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making an input dialog box appear after closing a combo box

    Oh! I mean after the user closes the combo box. I'd like the user to see the combo box, initially, and when they close it, an input dialog box should appear.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making an input dialog box appear after closing a combo box

    When does a combo box close? How can a user close a combo box? What would a user do to close it?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making an input dialog box appear after closing a combo box

    Well.. when they push the red X at the top-right corner of the window of the combo box. When the code is run, I can view the combo box and everything in it just fine, and I can close the box by clicking on that big red X. After clicking the big red X, I'd like an input dialog box to appear.

    Am I missing something?

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Making an input dialog box appear after closing a combo box

    You description may be off. It appears that you want something to appear after they close the JFrame that holds the JComboBox -- is that correct? If so, there may be better ways to skin this cat.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making an input dialog box appear after closing a combo box

    Is the red X what closes the JFrame window? The posted close exits the program when the JFrame is closed.

    You probably want to use a modal JDialog window to display the combo box. When that dialog window is closed, you would be able to call the next dialog to get further user input.
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making an input dialog box appear after closing a combo box

    Quote Originally Posted by curmudgeon View Post
    You description may be off. It appears that you want something to appear after they close the JFrame that holds the JComboBox -- is that correct? If so, there may be better ways to skin this cat.
    Quote Originally Posted by Norm View Post
    Is the red X what closes the JFrame window? The posted close exits the program when the JFrame is closed.

    You probably want to use a modal JDialog window to display the combo box. When that dialog window is closed, you would be able to call the next dialog to get further user input.
    Oh! Yes, I'm sorry. I didn't understand the technical talk -- but now I see what you guys are getting at. Yes, I think that's what I'm trying to do. What, exactly, does that entail, though?

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making an input dialog box appear after closing a combo box

    Create a JDialog window, add the combo box to it and show it. When the user closes the JDialog window, get the user's input to the dialog and use it as you want.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making an input dialog box appear after closing a combo box

    I apologize, for I'm a mega-newbie, and I don't think I understand how to do that. Do you know of a website that may explain that in detail? Or, could you explain that to me?

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making an input dialog box appear after closing a combo box

    Okay, I read through, and while I don't fully understand the language, I think I have a general idea of what I need to do. I think, anyway..

    Do I need to use this layout?

    public JDialog(Frame owner,
    String title,
    boolean modal,
    GraphicsConfiguration gc)

    Am I on the right track?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making an input dialog box appear after closing a combo box

    Write it into a program and compile it. The compiler will tell you if you have coded it correctly.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Oct 2012
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Making an input dialog box appear after closing a combo box

    I kind of don't understand what I'm supposed to replace "Frame owner", "boolean modal", and "GraphicsConfiguration gc" with. I understand Strings, so I think I understand what to put there.

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Making an input dialog box appear after closing a combo box

    Look at the example programs in the tutorial.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Error with taking float numbers from an input dialog box.
    By deepcrimson76 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 26th, 2012, 12:58 PM
  2. Dialog box not allowing user input?
    By michaelgilbert in forum AWT / Java Swing
    Replies: 13
    Last Post: November 1st, 2011, 05:22 PM
  3. ClassCastException with Enum Combo Box
    By Diplo in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 26th, 2011, 01:22 PM
  4. Adding another related combo box ?
    By shad3d in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 19th, 2011, 01:53 PM
  5. Loading contents of a Database into a Combo box
    By tyolu in forum JDBC & Databases
    Replies: 3
    Last Post: June 22nd, 2009, 08:14 AM