I am having problems in setting the GroupLayout as desired in Java.
My code is given below. The desired placement for components is:
++++++++++++++++++++++++++++++++++++++++++++++++++++
+LABEL_A caseStudyComboBox LABEL_B +
+LABEL_C TextfieldE BtnD +
++++++++++++++++++++++++++++++++++++++++++++++++++++
Instead of the above, the output is:
+++++++++++++++++++++++++++++++++++++++++++++++++++
+LABEL_A +
+ caseStudyCombBox +
+ LABEL_B +
+ LABEL_C IS LONGER THAN A: +
+ TextfieldE +
+ BtnD +
+++++++++++++++++++++++++++++++++++++++++++++++++++
Please suggest some remedy.
Thanks.
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.*;
import static javax.swing.GroupLayout.Alignment.*;
public class EXP1 extends JFrame {
String [] caseStudyList = {
"",
"Case A",
"Case B"
};
//
public EXP1() {
JLabel Label_A = new JLabel("LABEL A ");
JComboBox caseStudyComboBox = new JComboBox(caseStudyList);
JLabel Label_B = new JLabel("LABEL B");
JLabel Label_C = new JLabel("LABEL C IS LONGER THAN A: ");
JButton BtnD = new JButton("BUTTON D");
JTextField TextFieldE = new JTextField();
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(TRAILING))
.addComponent(Label_A, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Label_C, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(LEADING))
.addComponent(caseStudyComboBox, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(TextFieldE, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(LEADING))
.addComponent(Label_B, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(BtnD, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
layout.linkSize(SwingConstants.HORIZONTAL, Label_B, BtnD);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(BASELINE))
.addComponent(Label_A)
.addComponent(caseStudyComboBox)
.addComponent(Label_B)
.addGroup(layout.createParallelGroup(LEADING))
.addComponent(Label_C)
.addComponent(TextFieldE)
.addComponent(BtnD)
);
setTitle("EXPERIMENT");
this.pack();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//BPAOntoEIAUI mainUI = new BPAOntoEIAUI();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(
"javax.swing.plaf.metal.MetalLookAndFeel");
// "com.sun.java.swing.plaf.motif.MotifLookAndFeel");
//UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception ex) {
ex.printStackTrace();
}
new EXP1().setVisible(true);
}
});
}
}
Looks like there is a bit of confusion with brackets when you set up horizontal and vertical groups. The controls fall out of the intended groups that you create.
Instead of
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup())
.addComponent(Label_A)
.addComponent(Label_C)
It should actually be:
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(Label_A)
.addComponent(Label_C))
In first case you're adding labels to the sequential group instead of the parallel group like in the second case.
Here is the result with the following slightly modified code:
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(TRAILING)
.addComponent(Label_A, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(Label_C, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(caseStudyComboBox, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(TextFieldE, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(Label_B, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(BtnD, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.linkSize(SwingConstants.HORIZONTAL, Label_B, BtnD);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(BASELINE)
.addComponent(Label_A)
.addComponent(caseStudyComboBox)
.addComponent(Label_B))
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(Label_C)
.addComponent(TextFieldE)
.addComponent(BtnD))
);
Related
I am getting the message Baseline must be used along vertical axis. I've tried using all the enums for alignment, to no avail and made sure that all components have been added to both horizontal and vertical axes.
Here is the exception chain
at javax.swing.GroupLayout$BaselineGroup.checkAxis(GroupLayout.java:2926)
at javax.swing.GroupLayout$BaselineGroup.calculateSize(GroupLayout.java:2706)
at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1602)
at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1346)
at javax.swing.GroupLayout$Group.getSpringSize(GroupLayout.java:1638)
at javax.swing.GroupLayout$Group.calculateSize(GroupLayout.java:1624)
at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1602)
at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1346)
at javax.swing.GroupLayout$Group.getSpringSize(GroupLayout.java:1638)
at javax.swing.GroupLayout$Group.calculateSize(GroupLayout.java:1625)
at javax.swing.GroupLayout$Group.calculatePreferredSize(GroupLayout.java:1602)
at javax.swing.GroupLayout$Spring.getPreferredSize(GroupLayout.java:1346)
at javax.swing.GroupLayout$SequentialGroup.setValidSize(GroupLayout.java:2017)
at javax.swing.GroupLayout$Group.setSize(GroupLayout.java:1587)
at javax.swing.GroupLayout.calculateAutopadding(GroupLayout.java:1079)
at javax.swing.GroupLayout.layoutContainer(GroupLayout.java:918)
at java.awt.Container.layout(Container.java:1513)
at java.awt.Container.doLayout(Container.java:1502)
at java.awt.Container.validateTree(Container.java:1698)
at java.awt.Container.validateTree(Container.java:1707)
at java.awt.Container.validateTree(Container.java:1707)
at java.awt.Container.validateTree(Container.java:1707)
at java.awt.Container.validateTree(Container.java:1707)
at java.awt.Container.validate(Container.java:1633)
at java.awt.Container.validateUnconditionally(Container.java:1670)
at java.awt.Window.pack(Window.java:818)
at resumebuilder.ResumeBuilder.(ResumeBuilder.java:32)
at resumebuilder.program.main(program.java:15)
Here is the code
private void initComponents() {
GroupLayout groupLayout = new GroupLayout(this);
groupLayout.setAutoCreateGaps(true);
groupLayout.setAutoCreateContainerGaps(true);
jLabel1 = new JLabel("Name");
jLabel1.setHorizontalAlignment(SwingConstants.RIGHT);
firstName = new JTextField(50);
firstName.setToolTipText("First Name");
lastName = new JTextField(50);
lastName.setToolTipText("Last Name");
jLabel2 = new JLabel("Address");
jLabel2.setHorizontalAlignment(SwingConstants.RIGHT);
address1 = new JTextField(50);
address1.setToolTipText("Address 1");
address2 = new JTextField(50);
address2.setToolTipText("Address 2");
add(address2);
toggleAddress3 = new javax.swing.JCheckBox();
toggleAddress3.setText("Show third address");
toggleAddress3.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
setToggle();
}
#Override
public void mousePressed(MouseEvent e) {
}
#Override
public void mouseReleased(MouseEvent e) {
}
#Override
public void mouseEntered(MouseEvent e) {
}
#Override
public void mouseExited(MouseEvent e) {
}
});
address3 = new JTextField(50);
address3.setToolTipText("Address 3");
address3.setVisible(false);
city = new JTextField(50);
city.setToolTipText("City");
state = new JTextField(10);
state.setToolTipText("State");
postalCode = new JTextField(25);
postalCode.setToolTipText("Postal Code");
jLabel3 = new JLabel("Gender");
jLabel3.setHorizontalAlignment(SwingConstants.RIGHT);
add(jLabel3);
String[] items = {"", "Male", "Female"};
gender = new JComboBox<String>(items);
gender.setToolTipText("Gender");
jLabel4 = new JLabel("Date of Birth");
jLabel4.setHorizontalAlignment(SwingConstants.RIGHT);
dateOfBirth = new JSpinner();
groupLayout.setHorizontalGroup(groupLayout.createParallelGroup()
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(firstName)
.addComponent(lastName))
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(address1))
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(toggleAddress3)
.addComponent(address2))
.addComponent(address3)
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(city)
.addComponent(state)
.addComponent(postalCode))
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(gender))
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(dateOfBirth))
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addGroup(groupLayout.createSequentialGroup()
.addContainerGap()
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(firstName, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(lastName, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addGroup(groupLayout.createParallelGroup(
GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(address1)
)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3)
.addComponent(jLabel4))
.addGroup(groupLayout.createSequentialGroup()
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(address2, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(toggleAddress3))
.addComponent(address3, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(city, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(state, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(postalCode, GroupLayout.PREFERRED_SIZE,
GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(gender))
.addGroup(groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(dateOfBirth))
);
setLayout(groupLayout);
}
I'm using nested JSplitPane...
I want to use OneTouchExpandable, but running the code is not shown...
But in preview mode I can see it.
Here complete code:
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import java.awt.Dimension;
import java.util.logging.Level;
import java.util.logging.Logger;
package splitpane;
public class JFr_SplitPane extends JFrame {
private JButton jbLeftRight;
private JButton jbRightLeft;
private JButton jbRightRight;
private JLabel jlLeftLeft;
private JPanel jpLeftLeft;
private JPanel jpLeftRight;
private JPanel jpMain;
private JPanel jpRightLeft;
private JPanel jpRightRight;
private JSplitPane jspLeft;
private JSplitPane jspMain;
private JSplitPane jspRight;
private JTextField jtfLeftLeft;
private JTextField jtfLeftRight;
private JTextField jtfRightLeft;
private JTextField jtfRightRight;
public JFr_SplitPane() {
initComponents();
}
private void initComponents() {
jpMain = new JPanel();
jspMain = new JSplitPane();
jspLeft = new JSplitPane();
jpLeftLeft = new JPanel();
jlLeftLeft = new JLabel();
jtfLeftLeft = new JTextField();
jpLeftRight = new JPanel();
jbLeftRight = new JButton();
jtfLeftRight = new JTextField();
jspRight = new JSplitPane();
jpRightLeft = new JPanel();
jbRightLeft = new JButton();
jtfRightLeft = new JTextField();
jpRightRight = new JPanel();
jbRightRight = new JButton();
jtfRightRight = new JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jpMain.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jpMain.setPreferredSize(new Dimension(692, 36));
jspMain.setDividerLocation(344);
jspMain.setMinimumSize(new Dimension(276, 28));
jspMain.setOneTouchExpandable(true);
jspMain.setPreferredSize(new Dimension(688, 34));
jspLeft.setDividerLocation(169);
jspLeft.setMinimumSize(new Dimension(132, 25));
jspLeft.setOneTouchExpandable(true);
jspLeft.setPreferredSize(new Dimension(338, 30));
jpLeftLeft.setMinimumSize(new Dimension(60, 25));
jpLeftLeft.setPreferredSize(new Dimension(320, 25));
jlLeftLeft.setBorder(javax.swing.BorderFactory.createEtchedBorder());
jlLeftLeft.setPreferredSize(new Dimension(24, 18));
jtfLeftLeft.setPreferredSize(new Dimension(6, 25));
GroupLayout jpLeftLeftLayout = new GroupLayout(jpLeftLeft);
jpLeftLeft.setLayout(jpLeftLeftLayout);
jpLeftLeftLayout.setHorizontalGroup(
jpLeftLeftLayout.createParallelGroup(Alignment.LEADING)
.addGroup(jpLeftLeftLayout.createSequentialGroup()
.addGap(4, 4, 4)
.addComponent(jlLeftLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(2, 2, 2)
.addComponent(jtfLeftLeft, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
jpLeftLeftLayout.setVerticalGroup(
jpLeftLeftLayout.createParallelGroup(Alignment.LEADING)
.addGroup(jpLeftLeftLayout.createSequentialGroup()
.addGap(3, 3, 3)
.addComponent(jlLeftLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jtfLeftLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
);
jspLeft.setLeftComponent(jpLeftLeft);
jpLeftRight.setMinimumSize(new Dimension(60, 25));
jpLeftRight.setPreferredSize(new Dimension(320, 25));
jbLeftRight.setPreferredSize(new Dimension(30, 21));
jtfLeftRight.setPreferredSize(new Dimension(6, 25));
GroupLayout jpLeftRightLayout = new GroupLayout(jpLeftRight);
jpLeftRight.setLayout(jpLeftRightLayout);
jpLeftRightLayout.setHorizontalGroup(
jpLeftRightLayout.createParallelGroup(Alignment.LEADING)
.addGroup(jpLeftRightLayout.createSequentialGroup()
.addGap(4, 4, 4)
.addComponent(jbLeftRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(2, 2, 2)
.addComponent(jtfLeftRight, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
jpLeftRightLayout.setVerticalGroup(
jpLeftRightLayout.createParallelGroup(Alignment.LEADING)
.addGroup(jpLeftRightLayout.createParallelGroup(Alignment.CENTER)
.addComponent(jbLeftRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(jtfLeftRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
);
jspLeft.setRightComponent(jpLeftRight);
jspMain.setLeftComponent(jspLeft);
jspRight.setDividerLocation(169);
jspRight.setMinimumSize(new Dimension(132, 25));
jspRight.setOneTouchExpandable(true);
jspRight.setPreferredSize(new Dimension(338, 30));
jpRightLeft.setMinimumSize(new Dimension(60, 25));
jpRightLeft.setPreferredSize(new Dimension(320, 25));
jbRightLeft.setPreferredSize(new Dimension(30, 21));
jtfRightLeft.setPreferredSize(new Dimension(6, 25));
GroupLayout jpRightLeftLayout = new GroupLayout(jpRightLeft);
jpRightLeft.setLayout(jpRightLeftLayout);
jpRightLeftLayout.setHorizontalGroup(
jpRightLeftLayout.createParallelGroup(Alignment.LEADING)
.addGroup(jpRightLeftLayout.createSequentialGroup()
.addGap(4, 4, 4)
.addComponent(jbRightLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(2, 2, 2)
.addComponent(jtfRightLeft, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
jpRightLeftLayout.setVerticalGroup(
jpRightLeftLayout.createParallelGroup(Alignment.LEADING)
.addGroup(jpRightLeftLayout.createSequentialGroup()
.addGap(2, 2, 2)
.addComponent(jbRightLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jtfRightLeft, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
);
jspRight.setLeftComponent(jpRightLeft);
jpRightRight.setMinimumSize(new Dimension(60, 25));
jpRightRight.setPreferredSize(new Dimension(320, 25));
jbRightRight.setPreferredSize(new Dimension(30, 21));
jtfRightRight.setPreferredSize(new Dimension(6, 25));
GroupLayout jpRightRightLayout = new GroupLayout(jpRightRight);
jpRightRight.setLayout(jpRightRightLayout);
jpRightRightLayout.setHorizontalGroup(
jpRightRightLayout.createParallelGroup(Alignment.LEADING)
.addGroup(jpRightRightLayout.createSequentialGroup()
.addGap(4, 4, 4)
.addComponent(jbRightRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(2, 2, 2)
.addComponent(jtfRightRight, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
jpRightRightLayout.setVerticalGroup(
jpRightRightLayout.createParallelGroup(Alignment.LEADING)
.addGroup(jpRightRightLayout.createSequentialGroup()
.addGap(2, 2, 2)
.addComponent(jbRightRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jpRightRightLayout.createSequentialGroup()
.addComponent(jtfRightRight, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE))
);
jspRight.setRightComponent(jpRightRight);
jspMain.setRightComponent(jspRight);
GroupLayout jpMainLayout = new GroupLayout(jpMain);
jpMain.setLayout(jpMainLayout);
jpMainLayout.setHorizontalGroup(
jpMainLayout.createParallelGroup(Alignment.LEADING)
.addComponent(jspMain, Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
);
jpMainLayout.setVerticalGroup(
jpMainLayout.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, jpMainLayout.createSequentialGroup()
.addGap(0, 0, 0)
.addComponent(jspMain, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
);
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(Alignment.LEADING)
.addGap(0, 702, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(Alignment.LEADING)
.addGroup(Alignment.TRAILING, layout.createSequentialGroup()
.addGap(0, 0, 0)
.addComponent(jpMain, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap()))
);
layout.setVerticalGroup(
layout.createParallelGroup(Alignment.LEADING)
.addGap(0, 54, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(0, 0, 0)
.addComponent(jpMain, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
);
pack();
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
Logger.getLogger(JFr_SplitPane.class.getName()).log(Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
Logger.getLogger(JFr_SplitPane.class.getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(JFr_SplitPane.class.getName()).log(Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
Logger.getLogger(JFr_SplitPane.class.getName()).log(Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new JFr_SplitPane().setVisible(true);
}
});
}
}
In preview mode:
Running is not shown...
What's the problem?
How can I to solve it?
Testing Look And Feel
Windows
Nimbus
Windows Classic
CDE/Motif
Metal
Probably you are looking for SplitPane.oneTouchButtonOffset:
import java.awt.*;
import javax.swing.*;
public class OneTouchButtonOffsetTest {
public JComponent makeUI() {
System.out.println(UIManager.getInt("SplitPane.oneTouchButtonOffset"));
UIManager.put("SplitPane.oneTouchButtonOffset", 0);
UIDefaults d = new UIDefaults();
d.put("SplitPane:SplitPaneDivider[Enabled+Vertical].foregroundPainter",
new Painter<JComponent>() {
#Override public void paint(Graphics2D g, JComponent c, int w, int h) {
/* empty */
}
});
JSplitPane splitPane = new JSplitPane();
splitPane.putClientProperty("Nimbus.Overrides", d);
splitPane.setLeftComponent(makePanel());
splitPane.setRightComponent(makePanel());
splitPane.setOneTouchExpandable(true);
JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
p.add(splitPane, BorderLayout.NORTH);
return p;
}
private static JComponent makePanel() {
JPanel p = new JPanel(new BorderLayout());
p.add(new JButton(), BorderLayout.WEST);
p.add(new JTextField("aaaaaaaa"));
return p;
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
try {
for (UIManager.LookAndFeelInfo laf : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(laf.getName())) {
UIManager.setLookAndFeel(laf.getClassName());
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new OneTouchButtonOffsetTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
Ok, I have a program I am doing for class. I have to take the input form the text field, ad throw it into an overloaded constructor that takes a string as an argument. There are a few other things I have to do with constructors as well, so in my code you'll see some stuff that is outside of this question. also, I am not posting my methods because it's a big part of the homework, and those all work fine, (prior to my attempt at passing info to the constructor). What I would like to know is i there is a way to pass the text field info into the constructor, because I have been going at this for a couple hours now, but it's becoming plainly obvious that I don't understand this. If you just want to direct me to somewhere that I can learn how to do this I would appreciate it.
Code:
package lab4;
import java.awt.*;
import java.awt.List;
import javax.swing.*;
import java.awt.event.*;
import java.lang.Iterable.*;
import java.util.*;
public class Lab4 extends JFrame{
private JTextField jTextField1;
private JButton doSome;
private JLabel ansTxt;
private JLabel asciSum;
private JLabel jLabel1;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
private JLabel jLabel6;
private JLabel lowCnt;
private JLabel numDigit;
private JLabel upCnt;
private JLabel vowelCnt;
private String userInput = jTextField1.getText();
public Lab4()
{
initComponents();
}
public Lab4(String x)
{
this.userInput = x;
}
public Lab4(char[] x)
{
}
public Lab4(byte[] x)
{
}
private void initComponents() {
jTextField1 = new JTextField();
jLabel1 = new JLabel();
doSome = new JButton();
ansTxt = new JLabel();
jLabel3 = new JLabel();
jLabel4 = new JLabel();
jLabel5 = new JLabel();
jLabel6 = new JLabel();
asciSum = new JLabel();
vowelCnt = new JLabel();
numDigit = new JLabel();
upCnt = new JLabel();
lowCnt = new JLabel();
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setAutoRequestFocus(false);
setFont(new java.awt.Font("Comic Sans MS", 1, 12)); // NOI18N
jLabel1.setText("Enter a string Here and I'll show you some info about it!");
doSome.setToolTipText("This Will Do Something");
doSome.setInheritsPopupMenu(true);
doSome.setLabel("Click Me When Your Done!");
doSome.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
doSomeActionPerformed(evt);
}
});
ansTxt.setText("ASCII SUM:");
jLabel3.setText("Number of Vowels:");
jLabel4.setText("Number of Digits:");
jLabel5.setText("Number Of Uppercase:");
jLabel6.setText("Number Of Lowercase:");
GroupLayout layout = new GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
.addComponent(ansTxt)
.addComponent(jLabel5, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel6, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(84, 84, 84)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addComponent(lowCnt)
.addComponent(vowelCnt)
.addComponent(asciSum)
.addComponent(numDigit)
.addComponent(upCnt)))
.addGroup(layout.createSequentialGroup()
.addGap(95, 95, 95)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
.addComponent(jTextField1)
.addComponent(jLabel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(doSome, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
.addContainerGap(239, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(34, 34, 34)
.addComponent(jLabel1)
.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(doSome)
.addGap(34, 34, 34)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(ansTxt)
.addComponent(asciSum))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(vowelCnt))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(numDigit))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(upCnt))
.addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(jLabel6)
.addComponent(lowCnt))
.addContainerGap(157, Short.MAX_VALUE))
);
pack();
}
private void doSomeActionPerformed(ActionEvent evt)
{
String takeTfIn = this.jTextField1.getText();
this.asciSum.setText(getAsciiSum(takeTfIn));
this.vowelCnt.setText(getNumVowels(takeTfIn));
this.numDigit.setText(getNumDigits(takeTfIn));
this.upCnt.setText(getNumUpperCase(takeTfIn));
this.lowCnt.setText(getNumLowerCase(takeTfIn));
}
// Excluded methods go here...
public static void main(String[] args)
{
Lab4 test = new Lab4();
test.setVisible(true);
}
}
You'll need to create new instance of lab4 after you fill tex field value and click on submit. on click of submit you can do like this
this.setVisible(false);//Hide current instance
Lab4 test = new Lab4(this.jTextField1.getText()); // create new instance
test.setVisible(true); // set new instance visible
Don't forget to add following constructor:
public Lab4 (String value)
{
this.jTextField1.setText(value); // if you like to set value back to textbox
}
I am awful at Java GUI and used a generator to make most of the GUI for me. I made it in Netbeans and then copied it over to Eclipse. It lags out my pretty powerful computer for about 5 seconds on start and although I've tried timing it and stuff, I can't figure out why. I don't like to blame the generator but it is 100% coming from that UI code.
I would really appreciate someone helping me figure out what is causing it, and just in general improving the ugly UI code. The other code is unfinished, and this is just the interface part.
note that you can remove all of my code, even the gridLayout, and it will still lag, and if i compile it to a .jar and compile it with a -classpath argument it has the same lag
package ...
imports ...
public class x extends javax.swing.JFrame {
public x() {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Point centrePoint = new Point(screenSize.width / 2, screenSize.height / 2);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
schMon = new javax.swing.JLabel();
schTue = new javax.swing.JLabel();
schWed = new javax.swing.JLabel();
schThu = new javax.swing.JLabel();
schFri = new javax.swing.JLabel();
schSat = new javax.swing.JLabel();
schSun = new javax.swing.JLabel();
timeLabel = new javax.swing.JLabel();
schedulePanel = new javax.swing.JPanel();
holdingPanel = new javax.swing.JPanel();
isEnabled = new javax.swing.JCheckBox();
dropboxPath = new javax.swing.JTextField();
fileButton = new javax.swing.JButton();
saveButton = new javax.swing.JButton();
resetButton = new javax.swing.JButton();
sep2 = new javax.swing.JSeparator();
sep = new javax.swing.JSeparator();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
schMon.setText("Monday");
schTue.setText("Tuesday");
schWed.setText("Wednesday ");
schThu.setText("Thursday");
schFri.setText("Friday");
schSat.setText("Saturday");
schSun.setText("Sunday");
Font font = new Font("Calibri", Font.BOLD, 13);
timeLabel.setFont(font);
timeLabel.setText("");
schedulePanel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
schedulePanel.setLayout(new GridLayout(7, 24, -1, -1));
schedulePanel.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent mouseEvent) {
//JPanel clickedPanel = (JPanel) getComponentAt(mouseEvent.getPoint());
// System.out.println(clickedPanel.getName().toString());
}
});
ArrayList<ArrayList<JPanel>> times = new ArrayList<ArrayList<JPanel>>();
for (int day = 0; day < 7; day++) {
times.add(new ArrayList<JPanel>());
for (int hour = 0; hour < 24; hour++) {
times.get(day).add(new JPanel());
JPanel current = times.get(day).get(hour);
current.setSize(42, 42);
current.setBorder(BorderFactory.createLineBorder(Color.black));
current.setMaximumSize(current.getSize());
current.setBackground(Color.LIGHT_GRAY);
schedulePanel.add(current);
}
}
holdingPanel.setPreferredSize(new java.awt.Dimension(2, 100));
isEnabled.setText("Enable Scheduler");
dropboxPath.setText("Path to your Dropbox.exe");
dropboxPath.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jTextField1ActionPerformed(evt);
}
});
fileButton.setText("Find");
fileButton.setPreferredSize(new java.awt.Dimension(60, 22));
saveButton.setText("Save");
resetButton.setText("Reset");
resetButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton3ActionPerformed(evt);
}
});
sep2.setOrientation(javax.swing.SwingConstants.VERTICAL);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
holdingPanel);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(isEnabled)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(dropboxPath, GroupLayout.PREFERRED_SIZE, 480, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(fileButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(sep2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addGap(58)
.addComponent(saveButton)
.addPreferredGap(ComponentPlacement.RELATED)
.addComponent(resetButton)
.addGap(2))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(Alignment.TRAILING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel2Layout.createParallelGroup(Alignment.BASELINE)
.addComponent(isEnabled, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(sep2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(dropboxPath, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(fileButton, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(saveButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(resetButton, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap())
);
holdingPanel.setLayout(jPanel2Layout);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(sep)
.addComponent(
holdingPanel,
javax.swing.GroupLayout.DEFAULT_SIZE,
630, Short.MAX_VALUE)
.addGroup(
layout.createSequentialGroup()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(
schSat)
.addComponent(
schSun)
.addComponent(
timeLabel)
.addComponent(
schMon)
.addComponent(
schTue)
.addComponent(
schWed)
.addComponent(
schThu)
.addComponent(
schFri))
.addPreferredGap(
javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(
schedulePanel,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE)))
.addContainerGap()));
layout.setVerticalGroup(layout
.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addContainerGap()
.addGroup(
layout.createParallelGroup(
javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(
layout.createSequentialGroup()
.addGap(10)
.addComponent(
schMon)
.addGap(30)
.addComponent(
schTue)
.addGap(30)
.addComponent(
schWed)
.addGap(30)
.addComponent(
schThu)
.addGap(30)
.addComponent(
schFri)
.addGap(30)
.addComponent(
schSat)
.addGap(30)
.addComponent(
schSun)
.addGap(12)
.addComponent(
timeLabel)
.addGap(0,
0,
Short.MAX_VALUE))
.addComponent(
schedulePanel,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
Short.MAX_VALUE))
.addGap(10, 10, 10)
.addComponent(sep,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(1, 1, 1)
.addComponent(holdingPanel,
javax.swing.GroupLayout.PREFERRED_SIZE,
45,
javax.swing.GroupLayout.PREFERRED_SIZE)));
pack();
Point newLocation = new Point(centrePoint.x - (this.getWidth() / 2), centrePoint.y - (this.getHeight() / 2));
setLocation(newLocation);
}
private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
}
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
}
public static void main(String args[]) {
try {
javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager
.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(x.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(x.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(x.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(x.class.getName()).log(
java.util.logging.Level.SEVERE, null, ex);
}
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new x().setVisible(true);
}
});
}
private javax.swing.JButton fileButton;
private javax.swing.JButton saveButton;
private javax.swing.JButton resetButton;
private javax.swing.JCheckBox isEnabled;
private javax.swing.JLabel schMon;
private javax.swing.JLabel schTue;
private javax.swing.JLabel schWed;
private javax.swing.JLabel schThu;
private javax.swing.JLabel schFri;
private javax.swing.JLabel schSat;
private javax.swing.JLabel schSun;
private javax.swing.JLabel timeLabel;
private javax.swing.JPanel holdingPanel;
private javax.swing.JSeparator sep;
private javax.swing.JSeparator sep2;
private javax.swing.JTextField dropboxPath;
private javax.swing.JPanel schedulePanel;
}
If the lag is during WindowBuilder's opening of the design page (parsing), the lag is normal.
I would like to center content of a GroupLayout in Java applet. After long research I still cannot figure out how to do it.
Code below shows my applet. All elements are centered to layout but the layout is not center to applet.
JPanel cp=new JPanel();
String[] s = new String[2];
s[0] = "Price";
s[1] = "Name";
JComboBox c = new JComboBox(s);
JProgressBar pb=new JProgressBar(17, 23);
pb.setValue(20);
pb.setStringPainted(true);
JLabel l=new JLabel("Name of product");
JButton b=new JButton("Send a message");
b.setEnabled(true);
cp.add(c);
cp.add(pb);
cp.add(l);
cp.add(b);
GroupLayout layout = new GroupLayout(cp);
cp.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(GroupLayout.Alignment.CENTER)
.addComponent(c,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(pb,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(l,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(b,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addComponent(c,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(pb,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(l,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(b,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
);
I will appriciate any help.
The important lines are:
Container cont = getContentPane();
cont.setLayout(new GridBagLayout());
add(cp);
Screenshot
SSCCE
The 'full' 79 line code is:
//<applet code='TestApplet' width='250' height='250'></applet>
import java.awt.*;
import javax.swing.*;
public class TestApplet extends JApplet {
public void init() {
JPanel cp=new JPanel();
Container cont = getContentPane();
cont.setLayout(new GridBagLayout());
add(cp);
String[] s = new String[2];
s[0] = "Price";
s[1] = "Name";
JComboBox c = new JComboBox(s);
JProgressBar pb=new JProgressBar(17, 23);
pb.setValue(20);
pb.setStringPainted(true);
JLabel l=new JLabel("Name of product");
JButton b=new JButton("Send a message");
b.setEnabled(true);
cp.add(c);
cp.add(pb);
cp.add(l);
cp.add(b);
GroupLayout layout = new GroupLayout(cp);
cp.setLayout(layout);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(
GroupLayout.Alignment.CENTER)
.addComponent(c,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(pb,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(l,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(b,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE))
);
layout.setVerticalGroup(
layout.createSequentialGroup()
.addComponent(c,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(pb,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(l,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
.addComponent(b,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE,
GroupLayout.PREFERRED_SIZE)
);
}
}
You can create vertical and horizontal Glue elements from the Box factory and add these to the sides of your layout. When the layout is set out, the glue expands and forces the rest of the layout into the center.