The Artima Developer Community
Sponsored Link

Java Answers Forum
joptionpane troubles

3 replies on 1 page. Most recent reply: Feb 23, 2006 4:55 AM by Kondwani Mkandawire

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
kevynn ma

Posts: 1
Nickname: kevynn
Registered: Feb, 2006

joptionpane troubles Posted: Feb 9, 2006 7:08 PM
Reply to this message Reply
Advertisement
i am trying to create a program that asks the user for 3 integers in a single GUI input dialog box, using spaces as the delimiter. i dont know how to retrieve these integers after the user has input them, i know how to convert the string into an integer if only one integer is given, but not with multiple integers given. please help.


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: joptionpane troubles Posted: Feb 9, 2006 10:41 PM
Reply to this message Reply
The simpliest way: Create your own dialog, don't use JOption.

Use JFormattedTextfield for input to make sure the input text is a number.

Your dialog needs 3 private int variables
    int num1;
    int num2;
    int num3;


Your dialog should also offer 3 public or protected methods:
    int getNumber1() {
        return num1;
    }
    int getNumber2() {
        return num2;
    }
    int getNumber3() {
        return num1;
    }


On the close window event store the content of the 3 input fields to 3 int variables.

Thats's pretty much it.
If there is a OK or a CANCEL button you must write a logic for them, too. You can for example use a enmum for all avaiable buttons. Then you must offer a method which returns the pressed button after the dialog is closed.
    public static enum Buttons{OK, CANCEL};
    private Buttons pressedButton = Buttons.CANCEL;
    public Buttons getPresedButton() {
        return pressedButton;
    }



Access the dialog like this:
    Mydialog diag = new MyDialog();
    diag.setVisible(true);
    if (diag.getPressedButton() == MyDialog.Buttons.OK) {
        System.out.println("1st number: " + diag.getNumber1());
        System.out.println("2nd number: " + diag.getNumber2());
        System.out.println("3rd number: " + diag.getNumber3());
    } else {
        System.out.println("Aborted");
    }



I don't have too much experience with the JOptionpane, maybe you can extend a JOptionPane, so you don't have to implement the button-handlig yourself.

Darren Day

Posts: 1
Nickname: darrend
Registered: Feb, 2006

Re: joptionpane troubles Posted: Feb 22, 2006 12:57 PM
Reply to this message Reply
Matthias's suggestion is probably the best: give your user 3 input fields for each value.

Still, there are several ways to parse the string: StringTokenizer, the String#split methods, etc. A very terse but powerful way is to use Pattern and regular expressions:

package com.ppc.oepica.demo;
 
import javax.swing.*;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
 
public class JOptionPaneDemo
{
  public static void main(String[] args)
  {
    Pattern pattern = Pattern.compile("(\\d+)\\s(\\d+)\\s(\\d+)");
    String values;
    do
    {
      values = JOptionPane.showInputDialog("Enter 3 values");
      if(values!=null)
      {
        Matcher matcher = pattern.matcher(values);
        if(matcher.matches())
        {
          int firstVal = Integer.parseInt(matcher.group(1));
          int secondVal = Integer.parseInt(matcher.group(2));
          int thirdVal = Integer.parseInt(matcher.group(3));
          System.out.println(firstVal+" "+secondVal+" "+thirdVal);
        }
      }
    } while(values!=null);
    System.exit(0);
  }
}
 

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: joptionpane troubles Posted: Feb 23, 2006 4:55 AM
Reply to this message Reply
Here is a nasty hack:

use:
component = getComponent(i) 
//and 
if (component instanceof JTextField())
     //  override the behaviour of textField
     //  by creating a class that javax.swing.text.PlainDocument
    //  and override insertString...
}

Not sure but that just might work...

Flat View: This topic has 3 replies on 1 page
Topic: java.sql.Types.NUL - DON'T USE IT!! Previous Topic   Next Topic Topic: java virtual installer. Program will exit ....Please please help!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use