Simple popup java form with at least two fields
You should at least consider one of the JOptionPane
methods such as showInputDialog()
or showMessageDialog()
.
Addendum: The choice to use JOptionPane
hinges more on the suitability of modality, rather than on the number of components shown. See also How to Make Dialogs.
Addendum: As noted in a comment by @camickr, you can set the focus to a particular component using the approach discussed in Dialog Focus, cited here.
package gui;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.*;
/** @see https://stackoverflow.com/a/3002830/230513 */
class JOptionPaneTest {
private static void display() {
String[] items = {"One", "Two", "Three", "Four", "Five"};
JComboBox<String> combo = new JComboBox<>(items);
JTextField field1 = new JTextField("1234.56");
JTextField field2 = new JTextField("9876.54");
JPanel panel = new JPanel(new GridLayout(0, 1));
panel.add(combo);
panel.add(new JLabel("Field 1:"));
panel.add(field1);
panel.add(new JLabel("Field 2:"));
panel.add(field2);
int result = JOptionPane.showConfirmDialog(null, panel, "Test",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
System.out.println(combo.getSelectedItem()
+ " " + field1.getText()
+ " " + field2.getText());
} else {
System.out.println("Cancelled");
}
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
display();
}
});
}
}
How to create JDialog with 3 sliders and 2 drop lists?
As you want the dialog's results "only after it closes," simply collect the results at that time. Starting from this complete example, the following update produces the output and appearance shown:
…
JSlider slider = new JSlider();
panel.add(slider);
int result = JOptionPane.showConfirmDialog(null, panel, "Test",
JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
System.out.println(combo.getSelectedItem()
+ " " + field1.getText()
+ " " + field2.getText()
+ " " + slider.getValue());
} else {
…
One 1234.56 9876.54 50
How to add multiple results of an action event listener to a dialog
If both textfields will be filled every time (expected input is both fields, and expected output is both) then you can create two separate event handlers and attach one to each text field. Have the one for the first text field save the string in a global variable, then the second one concat the two strings together and then pop up your text box.
However, to handle all types of inputs (just one or both fields) I would add a third "submit" button. Have this submit button pull the strings from both text fields, then pop up your dialog. Initialize the strings to empty ("") to ensure no null pointers.
private String field1String = "";
private String field2String = "";
// you'll need one of these for each text field
private class TextField1Handler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
field1String = yourField1.getText();
}
}
// attach this to your button
private class ButtonClickHandler implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
JOptionPane.showMessageDialog(null, "Your textfields \n" +
field1String + field2String);
}
}
Input Dialog without icon and only OK option
Something like this:
JTextField field = new JTextField(20);
JLabel label = new JLabel("Enter your text here");
JPanel p = new JPanel(new BorderLayout(5, 2));
p.add(label, BorderLayout.WEST);
p.add(field);
JOptionPane.showMessageDialog(null, p, "Name required", JOptionPane.PLAIN_MESSAGE, null);
String text = field.getText();
System.out.println("You've entered: " + text);
show multiple MessageDialog at a time
When using the showX methods of JOptionPane you are creating modal (blocking and one at a time) dialogues as stated by the documentation.
You can Use the JOptionPane directly by creating it manually instead of using the showX methods.
create a new one manually and set it to not be modal:
optionPane = new JOptionPane(...);
dialog = optionPane.createDialog(null, "the title");
dialog.setModal(false);
dialog.show();
How do I open multiple JOptionPanes in Java at the same time?
By default all JOptionPane utility methods produce modal dialogs.
You can however create dialogs manually and call setModal(false) on the created JDialog instance...
public static void main(String[] args) throws Exception {
JOptionPane pane1 = new JOptionPane();
JDialog dialog1 = pane1.createDialog(null, "Window1");
dialog1.setModal(false);
dialog1.setVisible(true);
JOptionPane pane2 = new JOptionPane();
JDialog dialog2 = pane2.createDialog(null, "Window2");
dialog2.setModal(false);
dialog2.setVisible(true);
}
JTextField Output to a new window with the use of JButton
if you just want to display the content in the JTextFiled on a new window , u can try
JOptionPane.ShowMessageDialog(null,"text here");
this would pop up a msg box containing w.e text u put ... so the text you want would be :
address1.getText()+"\n"+....
and in order to make that happen when you click the submit button , you need to add an ActionListenner to that button , so it would be something like :
submitButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
JOptionPane.ShowMessageDialog(null,"text here");
}
});
Related Topics
Java Ee 6 @Javax.Annotation.Managedbean VS. @Javax.Inject.Named VS. @Javax.Faces.Managedbean
Order of Xml Attributes After Dom Processing
Java8 Lambdas VS Anonymous Classes
Rotating Coordinate Plane for Data and Text in Java
Why Is the Clone() Method Protected in Java.Lang.Object
Effective Gif/Image Color Quantization
What Does Java Option -Xmx Stand For
Get Source Jars from Maven Repository
Determining If an Object Is of Primitive Type
Javafx 11: Illegalaccesserror When Creating Label
Similarity String Comparison in Java
Why Is Stringtokenizer Deprecated
How to Get the Size of a Java.Sql.Resultset
Individual and Not Continuous Jtable's Cell Selection
Using Heapdumponoutofmemoryerror Parameter for Heap Dump for Jboss
Combining Raw Types and Generic Methods
How to Implement a Map with Multiple Keys
What Are Runtime.Getruntime().Totalmemory() and Freememory()