Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to group add buttons in Swing(use Netbeans Swing library)

 
Lava Shen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I used Netbeans to design a GUI which display a bunch of buttons(used to lauch a specific feature). The number of buttons changes when different user log in.
I can generate these buttons fine, but I am running into troubles of add these buttons to the GUI designed by the netbeans swing layout.
Does anyone know how to add an array of buttons into the GUI without so much trouble?

here's part of the code Netbeans generated(with fixed number of buttons(3),which I don't want):

org.jdesktop.layout.GroupLayout jPanel4Layout = new org.jdesktop.layout.GroupLayout(jPanel4);
jPanel4.setLayout(jPanel4Layout);
jPanel4Layout.setHorizontalGroup(
jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel4Layout.createSequentialGroup()
.addContainerGap()
.add(jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jButton4)
.add(jButton3)
.add(jButton2)) <-------- how to add more in when needed?
.addContainerGap(49, Short.MAX_VALUE))
);

jPanel4Layout.linkSize(new java.awt.Component[] {jButton2, jButton3, jButton4}, org.jdesktop.layout.GroupLayout.HORIZONTAL);

jPanel4Layout.setVerticalGroup(
jPanel4Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel4Layout.createSequentialGroup()
.addContainerGap()
.add(jButton2)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton3)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
.add(jButton4)
.addContainerGap(198, Short.MAX_VALUE))
);

jPanel4Layout.linkSize(new java.awt.Component[] {jButton2, jButton3, jButton4}, org.jdesktop.layout.GroupLayout.VERTICAL);


How to add more buttons in at will?

Thanks in advance

[ July 21, 2006: Message edited by: Lava Shen ]
[ July 21, 2006: Message edited by: Lava Shen ]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an article about GroupLayout - the layout that you're using in your GUI. GroupLayout is a really nice layout when you know what components you have ahead of time and they stay the same - but it's hard to use when you're dynamically adding components because all the components are laid out in relation to each other.

It looks like you're just laying all the buttons out in a line - why not change to FlowLayout?

 
Pay attention! Tiny ad!
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!