I have a button on a JFrame that when clicked I want a dialog box to popup with multiple text areas for user input. I have been looking all around to try to figure out how to do this but I keep on getting more confused. Can anyone help?
If you don't need much in the way of custom behavior, JOptionPane is a good time saver. It takes care of the placement and localization of OK / Cancel options, and is a quick-and-dirty way to show a custom dialog without needing to define your own classes. Most of the time the "message" parameter in JOptionPane is a String, but you can pass in a JComponent or array of JComponents as well.
Example:
JTextField firstName = new JTextField();
JTextField lastName = new JTextField();
JPasswordField password = new JPasswordField();
final JComponent[] inputs = new JComponent[] {
new JLabel("First"),
firstName,
new JLabel("Last"),
lastName,
new JLabel("Password"),
password
};
int result = JOptionPane.showConfirmDialog(null, inputs, "My custom dialog", JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
System.out.println("You entered " +
firstName.getText() + ", " +
lastName.getText() + ", " +
password.getText());
} else {
System.out.println("User canceled / closed the dialog, result = " + result);
}
Try this simple class for customizing a dialog to your liking:
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;
public class CustomDialog
{
private List<JComponent> components;
private String title;
private int messageType;
private JRootPane rootPane;
private String[] options;
private int optionIndex;
public CustomDialog()
{
components = new ArrayList<>();
setTitle("Custom dialog");
setMessageType(JOptionPane.PLAIN_MESSAGE);
setRootPane(null);
setOptions(new String[] { "OK", "Cancel" });
setOptionSelection(0);
}
public void setTitle(String title)
{
this.title = title;
}
public void setMessageType(int messageType)
{
this.messageType = messageType;
}
public void addComponent(JComponent component)
{
components.add(component);
}
public void addMessageText(String messageText)
{
JLabel label = new JLabel("<html>" + messageText + "</html>");
components.add(label);
}
public void setRootPane(JRootPane rootPane)
{
this.rootPane = rootPane;
}
public void setOptions(String[] options)
{
this.options = options;
}
public void setOptionSelection(int optionIndex)
{
this.optionIndex = optionIndex;
}
public int show()
{
int optionType = JOptionPane.OK_CANCEL_OPTION;
Object optionSelection = null;
if(options.length != 0)
{
optionSelection = options[optionIndex];
}
int selection = JOptionPane.showOptionDialog(rootPane,
components.toArray(), title, optionType, messageType, null,
options, optionSelection);
return selection;
}
public static String getLineBreak()
{
return "<br>";
}
}
This lesson from the Java tutorial explains each Swing component in detail, with examples and API links.
If you use the NetBeans IDE (latest version at this time is 6.5.1), you can use it to create a basic GUI java application using File->New Project and choose the Java category then Java Desktop Application.
Once created, you will have a simple bare bones GUI app which contains an about box that can be opened using a menu selection. You should be able to adapt this to your needs and learn how to open a dialog from a button click.
You will be able to edit the dialog visually. Delete the items that are there and add some text areas. Play around with it and come back with more questions if you get stuck :)
Well, you essentially create a JDialog, add your text components and make it visible. It might help if you narrow down which specific bit you're having trouble with.
i created a custom dialog API. check it out here https://github.com/MarkMyWord03/CustomDialog. It supports message and confirmation box. input and option dialog just like in joptionpane will be implemented soon.
Sample Error Dialog from CUstomDialog API:
CustomDialog Error Message
Related
I have a main window called MainFrame which is a jForm to which I update the data depending on a timer, but the problem is that I cannot update the data in the same MainFrame after using the jdialog, since I end up creating another duplicate window, but with the data changed, one with the original timer and the other with the new timer, I know that I can close the first window with dispose() and then keep the second, but I would like to avoid changing windows so much
the code with which I create another window when pressing the jDialog button is the following
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
String textoFieldTimer = jTextField1.getText();
int timeUserConfig = Integer.parseInt(textoFieldTimer);
Timer timeDefault = new Timer(timeUserConfig, null);
TokenAccess token = new TokenAccess();
token.access_code = code;
MainFrame mainFrame = new MainFrame(token);
mainFrame.setVisible(true);
mainFrame.timeDefault.stop();
mainFrame.timeDefault = timeDefault;
mainFrame.setUpdateTime(timeUserConfig);
this.dispose();
}//GEN-LAST:event_jButton1ActionPerformed
Is there any alternative to update the window? something like mainFrame.update(); or maybe send the value of the jTextField from the jDialog to mainFrame? since the previous code creates another MainFrame for me.
Method main setLabel and Timer.start/stop
public void setUpdateTime(int timeUserConfig) {
this.timeUserConfig = timeUserConfig;
if (timeUserConfig == 0) {
timeDefault.start();
timeDefault.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
setLabelText();
String timeUserConfigStr = Integer.toString(timeDefaultInt);
tiempoActualizado.setText("Tiempo de Actualizado: " + timeUserConfigStr+"ms");
}
});
} else {
timeDefault.stop();
timeDefault = new Timer(timeUserConfig, null);
timeDefault.start();
timeDefault.addActionListener(new java.awt.event.ActionListener() {
#Override
public void actionPerformed(java.awt.event.ActionEvent evt) {
setLabelText();
String timeUserConfigStr = Integer.toString(timeUserConfig);
tiempoActualizado.setText("Tiempo de Actualizado: " + timeUserConfigStr+"ms");
}
});
}
}
setLabelText is a method set of label
public void setLabelText() {
String humedadStr = String.valueOf(humedad);
String temperaturaStr = String.valueOf(temperatura);
String presionStr = String.valueOf(co2);
temporalHum.setText(humedadStr);
temporalTemperatura.setText(temperaturaStr);
temporalPresion.setText(presionStr);
}
Any help would be appreciated.
Thanks for the update, and I found another solution without using an OptionPane from this question: programmatically close a JPanel which is displayed in JDialog.
I cannot replicate your codings
Start with the MainFrame, assuming you opened the JDialog by clicking on a button and wants to setText() to label lbSomething:
private void btInputActionPerformed(java.awt.event.ActionEvent evt) {
// Open new JDialog when button is clicked
NewJDialog dialog = new NewJDialog(new javax.swing.JFrame, true);
dialog.setVisible(true);
// Get user input from JDialog
String temp = dialog.getInput();
if (temp != null) {
/*
* Perform jButton1ActionPerformed() content here
* Including timeUserConfig, timeDefault and setUpdateTime() here
* so that you don't have to access mainFrame in the JDialog.
*/
lbSomething.setText(temp);
}
}
Then about the JDialog (with simple input detection):
public class NewJDialog extends javax.swing.JDialog {
// Set the variable as class variable
private String textTOFieldTimer;
public NewJDialog(java.awt.Frame parent, boolean modal) {
// default contents
}
#SupressWarinings("unchecked")
private void initComponents() {
// default contents
}
private void btSaveAction Performed(java.awt.event.ActionEvent evt) {
// Check if input correct and whether to disable JDialog
if (tfInput.getText.length() != 0) {
input = tfInput.getText();
// Connect to the whole JDialog by getWindowAncestor()
Window window = SwingUtilities.getWindowAncestor(NewJDialog.this);
// Just setVisible(false) instead of dispose()
window.setVisible(false);
} else {
JOptionPane.showMessageDialog(this, "Wrong Input");
}
}
public String getInput() {
return textToFieldTimer;
}
// default variables declarations
}
Hope this answer helps you well.
Would be better if you displayed the source code, but a simple solution to update values to an existing JFrame is by using setText() and getText().
For example:
String input = JOptionPane.showInputDialog(this, "Nuevo valor");
lbPresionActual.setText(input);
If you created a self-defined JDialog, it is about to transfer the input value when closing the JDialog, and that could be a different question.
Please can someone tell me if there is a convenient way of prevent JOptionPane from closing upon clicking OK unless the conditions for user input fields are met?
Or do I have no choice but to use JFrame?
My validation logic so far. Doesn't seem to work because the buttons are one-time clickable to some reason...
final JDialog dialog3 = new JDialog(OmniGUI.getFrame(), "Create new Node - id:" + newNodeID);
dialog3.setContentPane(theOPane);
dialog3.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
theOPane.addPropertyChangeListener(new PropertyChangeListener(){
public void propertyChange(PropertyChangeEvent e) {
if(e.getSource() == theOPane){
String val = (String) ((JOptionPane) e.getSource()).getValue();
if(val=="Create"){
System.out.println("Checking content");
if(!valid){
System.out.println("closing the window");
dialog3.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog3.removeAll();
dialog3.dispatchEvent(new WindowEvent(dialog3, WindowEvent.WINDOW_CLOSING));
}
}
}
}
});
dialog3.setLocation(p);
dialog3.pack();
dialog3.setVisible(true);
You can create your own Custom JDialog to check user input etc before closing or moving on. See this link:
Stopping Automatic Dialog Closing
By default, when the user clicks a JOptionPane-created button, the
dialog closes. But what if you want to check the user's answer before
closing the dialog? In this case, you must implement your own property
change listener so that when the user clicks a button, the dialog does
not automatically close.
Here is an example I made:
If you type wrong/no text and click Enter a validation message will be displayed:
If you click X to close Dialog or click Cancel a validation message will be shown also:
If correct text is entered (in this case "David") and enter is clicked a message is shown and JDialog is exited:
CustomDialog.java:
import java.awt.*;
import java.awt.event.*;
import java.beans.*;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
class CustomDialog extends JDialog
implements ActionListener,
PropertyChangeListener {
private String typedText = null;
private JTextField textField;
private String magicWord;
private JOptionPane optionPane;
private String btnString1 = "Enter";
private String btnString2 = "Cancel";
/**
* Returns null if the typed string was invalid; otherwise, returns the
* string as the user entered it.
*/
public String getValidatedText() {
return typedText;
}
/**
* Creates the reusable dialog.
*/
public CustomDialog(Frame aFrame, String aWord) {
super(aFrame, true);
magicWord = aWord.toUpperCase();
setTitle("Quiz");
textField = new JTextField(10);
//Create an array of the text and components to be displayed.
String msgString1 = "What was Dr. SEUSS's real last name?";
String msgString2 = "(The answer is \"" + magicWord
+ "\".)";
Object[] array = {msgString1, msgString2, textField};
//Create an array specifying the number of dialog buttons
//and their text.
Object[] options = {btnString1, btnString2};
//Create the JOptionPane.
optionPane = new JOptionPane(array,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION,
null,
options,
options[0]);
//Make this dialog display it.
setContentPane(optionPane);
//Handle window closing correctly.
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
//Ensure the text field always gets the first focus.
addComponentListener(new ComponentAdapter() {
#Override
public void componentShown(ComponentEvent ce) {
textField.requestFocusInWindow();
}
});
//Register an event handler that puts the text into the option pane.
textField.addActionListener(this);
//Register an event handler that reacts to option pane state changes.
optionPane.addPropertyChangeListener(this);
pack();
}
/**
* This method handles events for the text field.
*/
#Override
public void actionPerformed(ActionEvent e) {
optionPane.setValue(btnString1);
}
/**
* This method reacts to state changes in the option pane.
*/
#Override
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if (isVisible()
&& (e.getSource() == optionPane)
&& (JOptionPane.VALUE_PROPERTY.equals(prop)
|| JOptionPane.INPUT_VALUE_PROPERTY.equals(prop))) {
Object value = optionPane.getValue();
if (value == JOptionPane.UNINITIALIZED_VALUE) {
//ignore reset
return;
}
//Reset the JOptionPane's value.
//If you don't do this, then if the user
//presses the same button next time, no
//property change event will be fired.
optionPane.setValue(
JOptionPane.UNINITIALIZED_VALUE);
if (btnString1.equals(value)) {
typedText = textField.getText();
String ucText = typedText.toUpperCase();
if (magicWord.equals(ucText)) {
JOptionPane.showMessageDialog(this, "Correct answer given");
exit();
} else {
//text was invalid
textField.selectAll();
JOptionPane.showMessageDialog(this,
"Sorry, \"" + typedText + "\" "
+ "isn't a valid response.\n"
+ "Please enter "
+ magicWord + ".",
"Try again",
JOptionPane.ERROR_MESSAGE);
typedText = null;
textField.requestFocusInWindow();
}
} else { //user closed dialog or clicked cancel
JOptionPane.showMessageDialog(this, "It's OK. "
+ "We won't force you to type "
+ magicWord + ".");
typedText = null;
exit();
}
}
}
/**
* This method clears the dialog and hides it.
*/
public void exit() {
dispose();
}
public static void main(String... args) {
//create JDialog and components on EDT
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new CustomDialog(null, "David").setVisible(true);
}
});
}
}
One thing about Stop Automatic Dialog Closing is that it is only helps if you want to prevent closing or validate and then close... basing a solution on the sample code in that tutorial, I could not get it to validate and stay open if the validation failed.
In retrospect, I think the reason my first attempt did not work may be because it used JOptionPanel.createDialog() (not what the example code did). Maybe letting the JOptionPanel create it's own JDialog set up some "background" dependencies in how the event processing worked... meh. In any case, I've got what I wanted now: David Kroucamp's code was very useful to me.
I'm posting my solution because it handles PropertyChangeEvents differently than David, so it might be useful to some people. You'll see that much of the code is identical to his (thanks David)
This class checks for file existance and lets the user provide a new name or cancel. It takes some args in the constructor that it uses to validate users' input. The validation is
if(!Files.exists(rootPathArg.resolve(input))) { // close the dialog }
class GetPathNameDialog extends JDialog implements ActionListener, PropertyChangeListener {
/**
* contains the users input
*/
private JTextField textField;
/**
* the option pane that holds all fields and controls in this dialog
*/
private JOptionPane optionPane;
/**
* label for the left button that represents "OK"
*/
private String button1Str;
/**
* label for the right button that represents "Cancel"
*/
private String button2Str;
/**
* path containing the named entity to be renamed.
*/
private Path rootPath;
/**
* Creates the reusable dialog.
*/
/**
* Creates the dialog, panel and all GUI components, sets up listeners.
*
* #param rootPath the path where the file or folder we are renaming lives
* #param initialText the initial text to display in the text field (i.e. current name)
* #param title title of the JDialog itself
* #param textFieldWidth number of columns in the JTextField that will contain the file/folder name
* #param promptStr the propmt to display in the panel
* #param button1Str the label for the "OK" button
* #param button2Str the label for the "Cancel" button
*/
public GetPathNameDialog(Path rootPath, String initialText, String title, int textFieldWidth, String promptStr, String button1Str, String button2Str) {
super((Frame) null, true);
// init class variables
this.rootPath = rootPath;
this.button1Str = button1Str;
this.button2Str = button2Str;
setTitle(title);
textField = new JTextField(textFieldWidth);
textField.setText(initialText);
//Create an array of the text and components to be displayed.
Object[] array = {promptStr, textField};
//Create an array specifying the number of dialog buttons
//and their text.
Object[] options = {button1Str, button2Str};
//Create the JOptionPane.
optionPane = new JOptionPane(
array,
JOptionPane.QUESTION_MESSAGE,
JOptionPane.YES_NO_OPTION,
null,
options,
options[0]);
//Make this dialog display it.
setContentPane(optionPane);
//Handle window closing correctly.
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
//Ensure the text field always gets the first focus.
addComponentListener(new ComponentAdapter() {
#Override
public void componentShown(ComponentEvent ce) {
textField.requestFocusInWindow();
}
});
// Register an event handler that puts the text into the option pane INPUT_VALUE_PROPERTY
textField.addActionListener(this);
// Register an event handler that reacts to option pane state changes.
optionPane.addPropertyChangeListener(this);
// tell this dialog to display close to the current mouse pointer
setLocation(MouseInfo.getPointerInfo().getLocation());
pack();
}
/**
* This method handles events for the text field.
*/
#Override
public void actionPerformed(ActionEvent e) {
// this will fire a INPUT_VALUE_PROPERTY PropertyChangeEvent... takes the user input to the validaton code in the property handler
optionPane.setInputValue(textField.getText());
}
/**
* This method reacts to property changes.
*/
#Override
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if (isVisible() && (e.getSource() == optionPane)) {
// the VALUE_PROPERTY is not the same as the INPUT_VALUE_PROPERTY. we make use of the INPUT_VALUE_PROPERTY to carry our data
// but the JOptionPane uses the VALUE_PROPERTY for other stuff
if (JOptionPane.VALUE_PROPERTY.equals(prop)) {
// newValues delivered by VALUE_PROPERTY PropertyChangeEvent can be the actual labels of the button clicked,
// that's sooo counter-intuitive to me, but it's how we know which button got clicked
if (button1Str.equals(e.getNewValue())) {
// "OK" functionality...
// ...this will fire the event that takes the user input to the validation code
optionPane.setInputValue(textField.getText());
} else if (button2Str.equals(e.getNewValue())) {
// "CANCEL" functionality
optionPane.setInputValue(null);
exit();
}
} else if (JOptionPane.INPUT_VALUE_PROPERTY.equals(prop)) {
Object value = optionPane.getInputValue();
// null or empty strings in the text field (ie in the INPUT_VALUE_PROPERTY) are ignored
if (null != value && ((String) value).length() > 0) {
// here is the validation code
if (Files.exists(rootPath.resolve(textField.getText()))) {
// already exists, tell user
JOptionPane.showMessageDialog(this,
"Sorry, " + rootPath.resolve(textField.getText()).toString() + " already exists.\n\n Please enter another name.",
"OK",
JOptionPane.ERROR_MESSAGE);
// Make sure PropertyChangeEvent will fire next time...
// ...PropertyChangeEvents don't fire in setInputValue(newVal)...
// ...if newVal is equal to the current value, but if the user clicks...
// ...button 1 or hits enter in the text field without changing his text,...
// ...we still want to fire another event...
// ...so we reset the property without changing the text in the textField
optionPane.setInputValue(null);
} else {
// does not exist.. we are keeping the users input...
// ... it gets delivered to the user in getInputValue()
exit();
}
}
}
}
}
/**
* returns the users's validated input. Validated means !Files.exists(rootPath.resolve(input)).
*
* #return the text entered by the user, UNINITIALIZED_VALUE if the user X closed, null the user canceled
*/
public Object getInputValue() {
return optionPane.getInputValue();
}
/**
* closes the dialog and triggers the return from setVisible()
*/
public void exit() {
dispose();
}
}
The code to invoke it is:
GetPathNameDialog tempD = new GetPathNameDialog(
someFolderPath,
"theFileNameThatMustBeChanged.txt",
"Change File Name",
50,
"someFolderPath already contains a file named theFileNameThatMustBeChanged.txt." + ".\n\nPlease enter a different file name:",
"Copy the file with the new name", "Do not copy the file");
tempD.setVisible(true);
Object inputObj = tempD.getInputValue();
String input = (inputObj == JOptionPane.UNINITIALIZED_VALUE || null == inputObj ? "" : (String) inputObj);
if (input.length() > 0) {
// we now have a new file name. go ahead and do the copy or rename or whatever...
}
I'm building an application which has a few radio buttons and based on the selection the user makes I have to do one thing or another. Now I've used an OptionGroup to create the radio buttons but I don't seem to be able to understand how I can differentiate between radio buttons. In pure java it's pretty straightforward as I would create each radio button and then group them together with a ButtonGroup object but in vaadin I really don't know. The documentation is as usual abysmal, so I'm a bit stuck. Here is some code for you:
public class ConverterComponent extends CustomComponent{
private TextField name2 = new TextField();
private OptionGroup single;
private TextField userInput;
private TextField result;
private Button submit;
private Button reset;
private static final String conversion1 = "Km to miles";
private static final String conversion2 = "Miles to Km";
private static final long serialVersionUID = 1L;
public ConverterComponent(){
submit = new Button("Submit");
reset = new Button("Reset");
result = new TextField();
userInput = new TextField();
result.setEnabled(false);
result.setVisible(false);
userInput.setVisible(false);
reset.setVisible(false);
submit.setVisible(false);
single = new OptionGroup("Select the conversion");
single.addItems(conversion1, conversion2);
reset.addClickListener(new Button.ClickListener(){
#Override
public void buttonClick(ClickEvent event){
clearFields();
getResult().setVisible(false);
}
});
submit.addClickListener(new Button.ClickListener(){
#Override
public void buttonClick(ClickEvent event) {
getResult().setVisible(true);
//NEED TO KNOW WHICH RADIO BUTTON HAS BEEM CLICKED SO THAT i CAN DECIDE WHICH CONVERSION TO USE
}
});
single.addValueChangeListener(new Property.ValueChangeListener(){
#Override
public void valueChange(ValueChangeEvent event) {
clearFields();
/*System.out.println("You chose: " + event.getProperty().getValue().toString() + "\n");
System.out.println("other line " + event.getProperty() + "\n" + " id is " + single.getId() + " size " + single.size());*/
//System.out.println("event is " + event.getProperty().getValue());
switch(event.getProperty().getValue().toString()){
case conversion1:
System.out.println(conversion1);
break;
case conversion2:
System.out.println(conversion2);
break;
}
displayFields();
}
});
}
public OptionGroup getRadioButtons(){
return single;
}
public TextField getResult(){
return result;
}
public TextField getUserInput(){
return userInput;
}
public void displayFields(){
//getResult().setVisible(true);
getUserInput().setVisible(true);
getResetButton().setVisible(true);
getSubmitButton().setVisible(true);
}
public Button getResetButton(){
return reset;
}
public Button getSubmitButton(){
return submit;
}
public void clearFields(){
getResult().setValue("");
getUserInput().setValue("");
}
public void validateInputs(){
}
}
Bear in mind that I have to add more options in the future, but what I'm trying to get to is, when the the user selects a radio button, no matter which, he will get two input boxes one for his input and the other one - read only - displaying the conversion. The point is that when the selection is made and the input boxes are displayed I have to know already what selection the user has made because I have to be able to grab the input and convert it correctly. In the code above I'm displaying the user's choice but I don't have anything to check it or compare it to. Ideally what I would like to do is:
-click the first radio button
-determine which radio button has been selected so I know which conversion to use.
You can use any objects as item ids in Vaadin. For example you could do something like this:
If you for example have an enum presenting different conversions
public enum Conversion {
KM_TO_MILES,
MILES_TO_KM
}
then you could do something like this:
OptionGroup optionGroup = new OptionGroup();
optionGroup.addItem(Conversion.KM_TO_MILES);
optionGroup.setItemCaption(Conversion.KM_TO_MILES, "Km to miles");
optionGroup.addItem(Conversion.MILES_TO_KM);
optionGroup.setItemCaption(Conversion.MILES_TO_KM, "Miles to Km");
optionGroup.addValueChangeListener(e -> {
if (e.getProperty().getValue() == Conversion.KM_TO_MILES) {
// km to miles selected
}
});
Okay, so here's the deal. Currently, I am using this:
String[] choices = {"Rock", "Paper", "Scissors"};
String input = (String) JOptionPane.showInputDialog(null, "Please, make your choice", "Rock Paper Scissors!", JOptionPane.QUESTION_MESSAGE, null, choices, choices[0]);
Which is what I need. It creates a drop down menu that allows the user to select Rock, Paper, or Scissors and then outputs it into a String. The problem is, the window that it pops in is REALLY small, and is in the center of the screen. I want to re-size it to be 970 pixels by 300 pixels, and to appear at the location of 950 pixels and 0 pixels.
Now, before you say it, I HAVE tried to use JFrames for this, because I know how to get it the size and at the location I want it. However, I can't get the ActionListener to behave in the way that I want it to.
public static void main(String args[]) throws IOException
{
JFrame hi = new JFrame("Hi");
hi.setSize(970, 300);
hi.setLocation(950, 0);
System.out.println("Hi");
Picture Hi = new Picture("c:/The Game/Cool.png");
Hi.display();
JButton B = new JButton("Hey There!");
hi.add(B);
int c = Output(hi);
}
public int Output(JFrame b)
{
int j = 0;
j = //CODE NEEDED HERE
return j;
}
#Override
public void actionPerformed(ActionEvent arg0) {
}
So, the problem with this is that I need the JFrame to pop up in then "CODE NEEDED HERE" section, and then, upon clicking the button, to return a certain value, and then to close out of the JFrame. However, the JFrame doesn't wait for the Output() function, and it immediately returns j, which is equal to 0. Instead, it just does whatever is in the actionPerformed function.
So, I am asking for a solution to either one of these problems. How to either re-size the JOptionPane.showInputDialog() or to get the JFrame to return an int value upon clicking a button.
Sorry if this is really poorly explained, I'm really new to JOptionPane and JFrames.
JOptionPane is quite configurable, it's also nicely self contained, taking a lot of the repetitive, boil plate code and hiding it away in an easy to use package.
But that doesn't mean you have to use it that way, you can simply create an instance of JOptionPane, which is just an ancestor of JComponent and add it to what ever you want.
The difficulty is plumbing all the functionality back together, so you can respond to the buttons, for example.
Just beware, your example places the dialog under my task bar (yes, mine is at the top of the screen), so I can tell you, as a user, that will annoy me!
So, this example basically wraps up all the boiler plate code into a simple class/method, which makes it easy to repeatedly prompt the user the same question, over and over again...
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import static javax.swing.JOptionPane.OK_CANCEL_OPTION;
import static javax.swing.JOptionPane.UNINITIALIZED_VALUE;
import static javax.swing.JOptionPane.VALUE_PROPERTY;
import javax.swing.border.EmptyBorder;
public class Test {
public static void main(String[] args) {
String pick = Picker.pick();
System.out.println("You picked " + pick);
System.exit(0);
}
public static class Picker {
public static String pick() {
String[] choices = {"Rock", "Paper", "Scissors"};
JOptionPane pane = new JOptionPane("Please, make your choice", JOptionPane.QUESTION_MESSAGE,
OK_CANCEL_OPTION, null, null, null);
pane.setWantsInput(true);
pane.setSelectionValues(choices);
pane.setInitialSelectionValue(choices[0]);
JDialog dialog = new JDialog();
dialog.setModal(true);
PropertyChangeListener listener = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent event) {
// Let the defaultCloseOperation handle the closing
// if the user closed the window without selecting a button
// (newValue = null in that case). Otherwise, close the dialog.
if (dialog.isVisible()
&& (event.getPropertyName().equals(VALUE_PROPERTY))
&& event.getNewValue() != null
&& event.getNewValue() != JOptionPane.UNINITIALIZED_VALUE) {
dialog.setVisible(false);
}
}
};
WindowAdapter adapter = new WindowAdapter() {
private boolean gotFocus = false;
public void windowClosing(WindowEvent we) {
pane.setValue(null);
}
public void windowClosed(WindowEvent e) {
dialog.removePropertyChangeListener(listener);
dialog.getContentPane().removeAll();
}
public void windowGainedFocus(WindowEvent we) {
// Once window gets focus, set initial focus
if (!gotFocus) {
pane.selectInitialValue();
gotFocus = true;
}
}
};
dialog.addWindowListener(adapter);
dialog.addWindowFocusListener(adapter);
dialog.addComponentListener(new ComponentAdapter() {
public void componentShown(ComponentEvent ce) {
// reset value to ensure closing works properly
pane.setValue(JOptionPane.UNINITIALIZED_VALUE);
}
});
pane.addPropertyChangeListener(listener);
dialog.add(pane);
//dialog.pack();
//dialog.setLocationRelativeTo(null);
dialog.setSize(970, 300); // This is bad idea, use an EmptyBorder instead
dialog.setLocation(950, 0);
dialog.setVisible(true);
String pick = null;
Object value = pane.getInputValue();
if (value != UNINITIALIZED_VALUE) {
pick = value.toString();
}
return pick;
}
}
}
The reason you're having problems with JFrame is because it's not designed to block the code execution when displayed, a JDialog can, see How to Make Dialogs for more details
I have a button on a JFrame that when clicked I want a dialog box to popup with multiple text areas for user input. I have been looking all around to try to figure out how to do this but I keep on getting more confused. Can anyone help?
If you don't need much in the way of custom behavior, JOptionPane is a good time saver. It takes care of the placement and localization of OK / Cancel options, and is a quick-and-dirty way to show a custom dialog without needing to define your own classes. Most of the time the "message" parameter in JOptionPane is a String, but you can pass in a JComponent or array of JComponents as well.
Example:
JTextField firstName = new JTextField();
JTextField lastName = new JTextField();
JPasswordField password = new JPasswordField();
final JComponent[] inputs = new JComponent[] {
new JLabel("First"),
firstName,
new JLabel("Last"),
lastName,
new JLabel("Password"),
password
};
int result = JOptionPane.showConfirmDialog(null, inputs, "My custom dialog", JOptionPane.PLAIN_MESSAGE);
if (result == JOptionPane.OK_OPTION) {
System.out.println("You entered " +
firstName.getText() + ", " +
lastName.getText() + ", " +
password.getText());
} else {
System.out.println("User canceled / closed the dialog, result = " + result);
}
Try this simple class for customizing a dialog to your liking:
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRootPane;
public class CustomDialog
{
private List<JComponent> components;
private String title;
private int messageType;
private JRootPane rootPane;
private String[] options;
private int optionIndex;
public CustomDialog()
{
components = new ArrayList<>();
setTitle("Custom dialog");
setMessageType(JOptionPane.PLAIN_MESSAGE);
setRootPane(null);
setOptions(new String[] { "OK", "Cancel" });
setOptionSelection(0);
}
public void setTitle(String title)
{
this.title = title;
}
public void setMessageType(int messageType)
{
this.messageType = messageType;
}
public void addComponent(JComponent component)
{
components.add(component);
}
public void addMessageText(String messageText)
{
JLabel label = new JLabel("<html>" + messageText + "</html>");
components.add(label);
}
public void setRootPane(JRootPane rootPane)
{
this.rootPane = rootPane;
}
public void setOptions(String[] options)
{
this.options = options;
}
public void setOptionSelection(int optionIndex)
{
this.optionIndex = optionIndex;
}
public int show()
{
int optionType = JOptionPane.OK_CANCEL_OPTION;
Object optionSelection = null;
if(options.length != 0)
{
optionSelection = options[optionIndex];
}
int selection = JOptionPane.showOptionDialog(rootPane,
components.toArray(), title, optionType, messageType, null,
options, optionSelection);
return selection;
}
public static String getLineBreak()
{
return "<br>";
}
}
This lesson from the Java tutorial explains each Swing component in detail, with examples and API links.
If you use the NetBeans IDE (latest version at this time is 6.5.1), you can use it to create a basic GUI java application using File->New Project and choose the Java category then Java Desktop Application.
Once created, you will have a simple bare bones GUI app which contains an about box that can be opened using a menu selection. You should be able to adapt this to your needs and learn how to open a dialog from a button click.
You will be able to edit the dialog visually. Delete the items that are there and add some text areas. Play around with it and come back with more questions if you get stuck :)
Well, you essentially create a JDialog, add your text components and make it visible. It might help if you narrow down which specific bit you're having trouble with.
i created a custom dialog API. check it out here https://github.com/MarkMyWord03/CustomDialog. It supports message and confirmation box. input and option dialog just like in joptionpane will be implemented soon.
Sample Error Dialog from CUstomDialog API:
CustomDialog Error Message