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:
Quote:
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:
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) {
}
}
Re: Making an input dialog box appear after closing a combo box
Quote:
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?
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.
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?
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? :(
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.
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.
Re: Making an input dialog box appear after closing a combo box
Quote:
Originally Posted by
curmudgeon
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
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?
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.
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?
Re: Making an input dialog box appear after closing a combo box
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?
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.
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.
Re: Making an input dialog box appear after closing a combo box
Look at the example programs in the tutorial.