I want to use two layout depending on circumstances. But when I build them in the constructor and save, when I use them they don`t work. When I build them every time before using it is work.
Printable View
I want to use two layout depending on circumstances. But when I build them in the constructor and save, when I use them they don`t work. When I build them every time before using it is work.
You'll probably receive faster and more constructive feedback if you provide more of a description - especially something in the form of an SSCCE with a clear definition of what you mean by 'doesn't work'.
It works:
{
GroupLayout groupLayout = new GroupLayout(jPanel);
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(GroupLayout.Alignm ent.LEADING).
addComponent(jLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE));
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(GroupLayout.Alignm ent.LEADING).
addComponent(jLabel, GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE));
jPanel.setLayout(groupLayout);
}
It does not work:
GroupLayout groupLayout;
{
groupLayout = new GroupLayout(jPanel);
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(GroupLayout.Alignm ent.LEADING).
addComponent(jLabel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE));
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(GroupLayout.Alignm ent.LEADING).
addComponent(jLabel, GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE));
}
.
.
.
{
jPanel.setLayout(groupLayout);
}
I want to create a panel with Open/Close Button. May be there are another good variant how to create this panel without changing layout. For example, when I change size it is not appropriate, because size is changed from right to left and it covers my button.
I suppose It is not also understandable.
I wrote a class.
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class LayoutTest extends JFrame {
public LayoutTest() {
initComponents();
}
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jPanel1.setBackground(new java.awt.Color(204, 204, 255));
jPanel1.setBorder(javax.swing.BorderFactory.create EtchedBorder());
jButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
LayoutManager tempGroupLayout = jPanel1.getLayout();
jPanel1.setLayout(layoutManager2);
layoutManager = layoutManager2;
layoutManager2 = tempGroupLayout;
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING).
addGroup(javax.swing.GroupLayout.Alignment.TRAILIN G, jPanel1Layout.createSequentialGroup().
addContainerGap(286, Short.MAX_VALUE).
addComponent(jButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)));
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING).
addComponent(jButton, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE));
layoutManager = jPanel1Layout;
javax.swing.GroupLayout jPanel1Layout2 = new javax.swing.GroupLayout(jPanel1);
jPanel1Layout2.setHorizontalGroup(
jPanel1Layout2.createParallelGroup(javax.swing.Gro upLayout.Alignment.LEADING).
addGroup(javax.swing.GroupLayout.Alignment.TRAILIN G, jPanel1Layout2.createSequentialGroup().
addComponent(jButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)));
jPanel1Layout2.setVerticalGroup(
jPanel1Layout2.createParallelGroup(javax.swing.Gro upLayout.Alignment.LEADING).
addComponent(jButton, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE));
layoutManager2 = jPanel1Layout2;
pack();
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new LayoutTest().setVisible(true);
}
});
}
private JButton jButton;
private JPanel jPanel1;
private LayoutManager layoutManager;
private LayoutManager layoutManager2;
}