alwaysLearning
alwaysLearning

Reputation: 11

Is it possible to display multiple text fields for a user to input, in each, how many items they want?

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.

Upvotes: 0

Views: 7318

Answers (1)

Joseph118
Joseph118

Reputation: 505

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.

Upvotes: 1

Related Questions