Search code examples
javaswingjpaneljtextfieldlayout-manager

Multiple JTextFields?


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.


Solution

  • 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