0

I'm confused if you can have more than 1 JtextField inside of a single JPanel? If yes, then how with the code provided below.

my code:

private JPanel jp;
private JTextField jt;

jt = new JTextField();
jt.setBounds(1, 25, 60, 20);
jp.add(jt);
jt.setColumns(10);

JLabel npcId = new JLabel("npcId");
npcId.setBounds(15, 11, 92, 14);
jp.add(npcId);

What I'm building:

enter image description here

What I'm trying to accomplish here is have 5 JTextField objects: npcId, npcLocation, npcReg, npcAH, npcAA.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ryan
  • 359
  • 2
  • 8
  • 15
  • Of course you can. What have you tried and how did it fail? – John3136 Nov 28 '13 at 05:59
  • 3
    Please take a look at the documentation related to Swing Layout Manager http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html It is very flexible way to control position of GUI elements – XZen Nov 28 '13 at 06:01
  • 2
    `Don't use setBounds().` Where ever you found that example get rid of it. You should be using layout managers as suggested above. – camickr Nov 28 '13 at 06:03
  • Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Nov 28 '13 at 06:05
  • I was going off what I read in a java doc like a year ago & I couldn't find the book I saw it in, lost it in my house somewhere. But @ john I've tried copying npcId & changing it... failed. – Ryan Nov 28 '13 at 06:06

3 Answers3

4

Yes, it's very easy, you need to make use of an appropriate layout manager.

Take a look,at A Visual Guide to Layout Managers

I might suggest starting with something GridLayout, but eventually, you'll want to look at GridBagLayout.

Don't forget, you can use compound layouts to create sophisticated layouts

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

Also, you might consider those basic methods:

// Use of Textfield
usernameField = new JTextField(8);
usernameField.setLocation(0, 0);
usernameField.setSize(100, 30);
Simon
  • 2,643
  • 3
  • 40
  • 61
  • So `jt = new JTextField(); jt.setLocation(0,0); jt.setSize(100, 30);` how would I go about duplicating that to make more text fields....? – Ryan Nov 28 '13 at 06:25
  • for example `jt2 = new JTextField();` etc. – Simon Nov 28 '13 at 06:30
1

Yes you can it easily, what you realy have to is 1) declare all your JTextFields like :

jt = new JTextField();
jt.setBounds(1, 25, 60, 20);
jp.add(jt);
jt.setColumns(10);

2) declare your JPanel like

private JPanel jp;

jp = new JPanel();// here you have to set a layout manager for this panel
//for exampl:
jp.setLayout(new FlowLayout());

check the Layout Managers from here.

3) add all declared JTextFields to your panel :

jp.add(jt);
jp.setVisible(true);
Salah
  • 8,567
  • 3
  • 26
  • 43