newbie java swing string query.

Started by
5 comments, last by bagsundereyes 20 years, 5 months ago
/* * @(#)NumberBox.java 1.0 03/11/03 */ import java.awt.event.*; import java.awt.*; import java.applet.*; import javax.swing.*; import java.util.*; public class NumberBox extends JApplet implements ActionListener{ JButton medianButton; JButton avgHighLowButton; JTextField inputNumbers; ArrayList userInput; public void init() { Container c = getContentPane(); inputNumbers = new JTextField(100); avgHighLowButton = new JButton ("List Details"); medianButton = new JButton("Get Median"); userInput = new ArrayList(); c.add(avgHighLowButton); c.add(medianButton); c.add(inputNumbers); getContentPane().setLayout(null); inputNumbers.setBounds(150,50,300,30); avgHighLowButton.setBounds(390,260,100,30); avgHighLowButton.addActionListener(this); avgHighLowButton.setActionCommand("avg"); medianButton.setBounds(10,260,100,30); medianButton.addActionListener(this); medianButton.setActionCommand("median"); } public void actionPerformed (ActionEvent e){ if ("median".equals(e.getActionCommand())) { System.out.println("median pressed"); JOptionPane.showMessageDialog(null, "The Median of this Set is: \n"); } else { System.out.println("avg pressed"); JOptionPane.showMessageDialog(null, "The Average of this Set is: \nThe Highest Number is: \nThe Lowest Number is: \n"); } } public void ActionPerformed (ActionEvent e){ //String = string; JOptionPane.showInputDialog(null,inputNumbers.getText()); } //string = " " + inputNumbers; //System.out.println("User Input"+ string); } basically i want the user to type in a comma seperated list of numbers into a JtextField and tokenise this for manipulation. what i don''t really know (and can''t seem to find a simple answer too) is how to convert this input text into a String in the first place. either my basic understanding of the language is very skewed (likely) or i''m missing something very simple (likely). any suggestions, other than STFW. java.sun/google is good and all but as stated i can''t seem to find a simple answer.
Advertisement
ok, so what you are going to want to do is: first you can create a sting from the JTextField, but doing TextField.paramString() it returns a string, so you could say String fieldContents = new String(textField.paramString());
and then go from there to tokenize it. There is a constructor for StringTokenizer that will allow for this,
quote:
StringTokenizer
public StringTokenizer(String str,
String delim)Constructs a string tokenizer for the specified string. The characters in the delim argument are the delimiters for separating tokens. Delimiter characters themselves will not be treated as tokens.

Parameters:
str - a string to be parsed.
delim - the delimiters.

if you need any other help, just post and I will see what I can do.
thanks for taking the time to reply, i''l try this out and see how it goes.
C:\Program Files\Xinox Software\JCreator LE\MyProjects\numberBox\NumberBox.java:28: paramString() has protected access in javax.swing.JTextField


it''s throwing this error at compilation, im not sure how to resolve it.
oops
quote:
paramString

protected String paramString()

Returns a string representation of this JTextField. This method is intended to be used only for debugging purposes, and the content and format of the returned string may vary between implementations. The returned string may be empty but may not be null.


Overrides:
paramString in class JTextComponent



Returns:
a string representation of this JTextField

I think you actually have to create a listener for the JTextField, so that when the user actually changes what is in the field, you can manipulate what is placed there. Here is how to actually implement a JTextField clicky, sorry for any confusion. There is an actual getText() method of the JTextField, so basically the same thing I said earlier, just with Sting contents = new String(textField.getText());, I will post source of what should happen a little later as an example.
Actually, in java 1.42, StringTokenizer is a deprecated class.


Use String.split instead. It takes a regular expression, and returns an array of strings with that regular expression as the delimiter. Its actually quite nice.
quote:Original post by bastard2k5
There is an actual getText() method of the JTextField, so basically the same thing I said earlier, just with Sting contents = new String(textField.getText());, I will post source of what should happen a little later as an example.


Don''t use new String. The String already exists. Just use:

String contents=textField.getText();

And AP, StringTokenizer is not deprecated.


First make it work,
then make it fast.

--Brian Kernighan

The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities. We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)

Do not interrupt your enemy when he is making a mistake. - Napolean Bonaparte
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][LWJGL Tutorials for NeHe][LWJGL Wiki][jMonkey Engine]

This topic is closed to new replies.

Advertisement