0

Ex..

   Milk   [ text field ]

   Eggs   [ text field ]

   Yogurt [ text field ] 

   Chicken[ text field ]

I've been trying to add more than one textbox to a window, but I've come to realize that JTextBox doesn't support multiple lines.

I'm trying to create a file, similar to a grocery store, that displays multiple items and then the user can enter any number of items that they want and that number will be added to sum.

The only way, I've been able to do this so far is with JCheckBox, but it's hideous, and if possible id much rather do it the way shown above.

P.S. I've seen JTextArea, but it looks like that only allows a user to write freely, like in paragraph form.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • *"..but I've come to realize that `JTextBox` doesn't support multiple lines."* Given there is no such class in the J2SE, I'm not surprised. DYM `JTextField`? *"I've seen `JTextArea`, but it looks like that only allows a user to write freely, like in paragraph form."* And I don't even understand that. – Andrew Thompson Nov 25 '14 at 13:54
  • Why do you need more lines in a single textbox if you want to add more textboxes to your form? – Seunhaab Nov 25 '14 at 13:55
  • There is no reason you can't add more than one textfield. Add one for each item (eg, Milk, Eggs, etc). – forgivenson Nov 25 '14 at 13:55
  • 1
    BTW - consider using a `JSpinner` with a `SpinnerNumberModel` for accepting numbers.. – Andrew Thompson Nov 25 '14 at 13:56
  • Or a text field which only accepts numbers (as shown [here](http://stackoverflow.com/a/13424140/1076463)) – Robin Nov 25 '14 at 15:46

1 Answers1

1

Yes it is possible, however you need to set a layout where the JLabels and JTextbox will appear, else they will appear on top of each other.. (more about layouts can be found here: - https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html) I have used a layout on my panel to have 4 rows and 2 columns.

JPanel panel = new JPanel();
GridLayout gl = new GridLayout(4,2); // 4 rows, 2 columns
panel.setLayout(gl);

now add the a label and text box each time (from left to right)...

JLabel lblMilk = new JLabel("MILK");
JTextbox txbMilk = new JTextbox();
panel.add(lblMilk);
panel.add(txbMilk);
JLabel lblEggs = new JLabel("EGGS");
JTextbox txbEggs = new JTextbox();
panel.add(lblEggs);
panel.add(txbEggs);

then add the panel to your frame.

Joseph118
  • 505
  • 6
  • 21
  • Thanks, Ive been gone all day and eventually figured it out. well close to . Perfect! – alwaysLearning Nov 25 '14 at 21:25
  • When i use gridlayout the textbox stretches to fill the space. I was looking to format it similar to https://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html#constructors – alwaysLearning Nov 26 '14 at 00:05
  • @BobbyII gridlayout was an example, there are different layouts that you could use. Grid layout is similar of having a table format. Check the link in my answer – Joseph118 Nov 26 '14 at 00:28
  • SpringLayout is what i was looking for. Sorry frantic coding led me to overlook what you had already posted. Thanks again! – alwaysLearning Nov 26 '14 at 21:13