0

I'm sorry for not giving much information; I'm new to Java. Please can you help me with this code? I want to align labels and textfields next to one other and all the buttons in a single row. How can i do this?

Here's how I'm creating a GroupLayout:

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);

Here's how I'm creating a horizontal group:

layout.setHorizontalGroup(

  layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

  .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)

  .addGroup(layout.createParallelGroup()

  .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)

  .addGroup(layout.createSequentialGroup()

  .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 400, javax.swing.GroupLayout.PREFERRED_SIZE))

  .addGroup(layout.createParallelGroup()
    .addGap(40, 40, 40)
    .addComponent(jLabel2)
    .addComponent(txtitem, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel3)
    .addComponent(txtprice, javax.swing.GroupLayout.DEFAULT_SIZE, 100,javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel4) 
    .addComponent(txtquantity, javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel5)
    .addComponent(txtreorder, javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(btnupdate)//,javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.DEFAULT_SIZE)//,javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.DEFAULT_SIZE)
    .addComponent(btndelete,javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.DEFAULT_SIZE)
    .addComponent(btnload,javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.DEFAULT_SIZE)
    .addComponent(btnadd)//,javax.swing.GroupLayout.DEFAULT_SIZE, 100, javax.swing.GroupLayout.DEFAULT_SIZE)
    .addComponent(txtreorder)
    .addComponent(jLabel5))
    )
  )
);

Here's how I'm creating a VerticalGroup:

layout.setVerticalGroup(
  layout.createParallelGroup()
    .addGroup(layout.createSequentialGroup()
    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addGap(40, 40, 40)
    .addGroup(layout.createSequentialGroup()
    .addComponent(jLabel2)
    .addComponent(txtitem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel3)
    .addComponent(txtprice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel4)
    .addComponent(txtquantity, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(jLabel5)
    .addComponent(txtreorder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    // .addComponent(btnadd))
    .addGap(40, 40, 40)
    .addComponent(btnadd,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(txtreorder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(btnupdate,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(txtreorder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(btndelete,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(txtreorder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(btnload,javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addComponent(txtreorder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE)
    .addContainerGap()
    )
  )
);
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • In the future, please format your code. If you can't read it, you probably can't debug it, and the same is true for us. If we can't read your code, we probably can't debug it either. Please give more information as well. Even if you're a newbie to Java, it's still generally expected that you explain what you want your code to do and what it's doing instead. This will help us understand what might be going wrong and how to fix it. – cf- Apr 03 '14 at 16:53
  • The code is destressingly hard to read (which is not unusual, and one of the reasons why I don't like these "visual GUI builder" things). Maybe a screenshot of the current state and a short description of the desired state could help to find alternative ways to what you want to achieve, possibly with some simple GridLayout (or GridBagLayout, but this is rarely really necessary) – Marco13 Apr 03 '14 at 17:42

2 Answers2

1

You are never forced to use a single panel for all components.

I want to align labels and textfields next to one other

Maybe use a panel with eight a SpringLayout or GridBagLayout. Read the section from the Swing tutorial on How to Use Layout Managers for more information and examples.

and all the buttons in a single row.

Maybe use a panel with a FlowLayout.

Then add the first panel to the CENTER of your dialog and the buttons panel to the SOUTH.

camickr
  • 321,443
  • 19
  • 166
  • 288
-2

Something like this:

label1.setBounds(2,2,50,25); 
textField1.setBounds(10,2,50,25);

ETC... for each item you want to programmatically place.

You'll want to play around with the (x,y,w,h) coordinates to fit your needs.

javaGeek
  • 304
  • 1
  • 4
  • 11
  • 2
    NEVER use `null`-Layout and manual `setBounds` calls for such a job! – Marco13 Apr 03 '14 at 17:40
  • 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 Apr 11 '14 at 02:32