Problem with layout

Hello,

When I add() object of the JParameterPanel class with Null layout to main panel with GridBagLayout then all guts on JParameterPanel changes their positions and take the unexpected view.

Why?

publicclass JParameterPanelextends javax.swing.JPanel{

/** Creates new form JParameterPanel */

public JParameterPanel(){

initComponents();

}

/** This method is called from within the constructor to

* initialize the form.

* WARNING: Do NOT modify this code. The content of this method is

* always regenerated by the Form Editor.

*/

// <editor-fold defaultstate="collapsed" desc=" Generated Code ">

privatevoid initComponents(){

jPanel1 =new javax.swing.JPanel();

jLabel1 =new javax.swing.JLabel();

jButton1 =new javax.swing.JButton();

setLayout(null);

setPreferredSize(new java.awt.Dimension( 1000, 1000 ) );

setMinimumSize(new java.awt.Dimension(1000, 1000));

jPanel1.setLayout(null);

jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

jLabel1.setText("jLabel1");

jPanel1.add(jLabel1);

jLabel1.setBounds(130, 40, 34, 14);

add(jPanel1);

jPanel1.setBounds(10, 10, 350, 160);

jButton1.setText("jButton1");

add(jButton1);

jButton1.setBounds(400, 190, 75, 23);

}// </editor-fold>

// Variables declaration - do not modify

private javax.swing.JButton jButton1;

private javax.swing.JLabel jLabel1;

private javax.swing.JPanel jPanel1;

// End of variables declaration

}

Thank you!

[2563 byte] By [mustheroa] at [2007-11-27 8:39:31]
# 1
posted code works OK for me.post a sample program that displays the problem
Michael_Dunna at 2007-7-12 20:37:29 > top of Java-index,Desktop,Core GUI APIs...
# 2
Could you please post your code, because I can not separate the code? :(
mustheroa at 2007-7-12 20:37:29 > top of Java-index,Desktop,Core GUI APIs...
# 3

public class JTestPanel extends javax.swing.JPanel {

/** Creates new form JTestPanel */

public JTestPanel() {

initComponents();

}

private void initComponents() {

java.awt.GridBagConstraints gridBagConstraints;

jButton1 = new javax.swing.JButton();

jPanel1 = new JParameterPanel();

jButton2 = new javax.swing.JButton();

jPanel2 = new javax.swing.JPanel();

setLayout(new java.awt.GridBagLayout());

jButton1.setText("jButton1");

gridBagConstraints = new java.awt.GridBagConstraints();

gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;

gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;

gridBagConstraints.weightx = 100.0;

add(jButton1, gridBagConstraints);

jPanel1.setLayout(null);

gridBagConstraints = new java.awt.GridBagConstraints();

gridBagConstraints.gridx = 0;

gridBagConstraints.gridy = 1;

gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;

gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;

gridBagConstraints.weightx = 100.0;

add(jPanel1, gridBagConstraints);

jButton2.setText("jButton2");

gridBagConstraints = new java.awt.GridBagConstraints();

gridBagConstraints.gridx = 0;

gridBagConstraints.gridy = 2;

gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;

gridBagConstraints.anchor = java.awt.GridBagConstraints.NORTH;

gridBagConstraints.weightx = 100.0;

add(jButton2, gridBagConstraints);

jPanel2.setLayout(null);

gridBagConstraints = new java.awt.GridBagConstraints();

gridBagConstraints.gridx = 0;

gridBagConstraints.gridy = 3;

gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;

gridBagConstraints.weightx = 100.0;

gridBagConstraints.weighty = 100.0;

add(jPanel2, gridBagConstraints);

}

private javax.swing.JButton jButton1;

private javax.swing.JButton jButton2;

private JParameterPanel jPanel1;

private javax.swing.JPanel jPanel2;

}

mustheroa at 2007-7-12 20:37:29 > top of Java-index,Desktop,Core GUI APIs...
# 4

JFrame f = new JFrame();

f.getContentPane().setLayout(new GridBagLayout());

f.getContentPane().add(new JParameterPanel(),new GridBagConstraints());

f.setSize(1200,1200);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

Michael_Dunna at 2007-7-12 20:37:30 > top of Java-index,Desktop,Core GUI APIs...
# 5

> [code]

>JFrame f = new JFrame();

> f.getContentPane().setLayout(new GridBagLayout());

> f.getContentPane().add(new JParameterPanel(),new

> GridBagConstraints());

>f.setSize(1200,1200);

> f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

>f.setVisible(true);

> /code]

No,no... I'm trying to add JParameterPanel with Null layout to JTestPanel with GridBagLayout! Not directly to JFrame because it really works!

mustheroa at 2007-7-12 20:37:30 > top of Java-index,Desktop,Core GUI APIs...
# 6

> No,no... I'm trying to add JParameterPanel with Null

> layout to JTestPanel with GridBagLayout! Not directly

> to JFrame because it really works!

What are you trying to achieve? Why do you want to use null layout when the layout managers offer so much assistance in maintaining a proper appearance of your app?

petes1234a at 2007-7-12 20:37:30 > top of Java-index,Desktop,Core GUI APIs...
# 7

[edit]

just spotted your JTestPanel code, forget the code below

[/edit]

same result, works OK

JPanel jTestPanel = new JPanel(new GridBagLayout());

jTestPanel.add(new JParameterPanel(),new GridBagConstraints());

JFrame f = new JFrame();

f.getContentPane().add(jTestPanel);

f.setSize(1200,1200);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

Message was edited by:

Michael_Dunn

Michael_Dunna at 2007-7-12 20:37:30 > top of Java-index,Desktop,Core GUI APIs...
# 8

> What are you trying to achieve? Why do you want to

> use null layout when the layout managers offer so

> much assistance in maintaining a proper appearance of

> your app?

why? - it's another question. Now I want to use null layout and it's does not work properly with GridBagLayout. I'm trying to resolve this problem and figure out the mistake.

mustheroa at 2007-7-12 20:37:30 > top of Java-index,Desktop,Core GUI APIs...
# 9

Could you try this please (using class JTestPanel)

JTestPanel jTestPanel = new JTestPanel ();

JFrame f = new JFrame();

f.getContentPane().add(jTestPanel );

f.setSize(1200,1200);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

and compare the result with

JParameterPanel jTestPanel = new JParameterPanel();

JFrame f = new JFrame();

f.getContentPane().add(jTestPanel );

f.setSize(1200,1200);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.setVisible(true);

mustheroa at 2007-7-12 20:37:30 > top of Java-index,Desktop,Core GUI APIs...
# 10
reduce the height of JParameterPanel and it seems to do what you want//setPreferredSize( new java.awt.Dimension( 1000, 1000 ) );setPreferredSize( new java.awt.Dimension( 1000, 500 ) );
Michael_Dunna at 2007-7-12 20:37:30 > top of Java-index,Desktop,Core GUI APIs...
# 11

> reduce the height of JParameterPanel and it seems to

> do what you want

> //setPreferredSize( new java.awt.Dimension(

> 1000, 1000 ) );

> setPreferredSize( new java.awt.Dimension( 1000, 500 )

> );

This just changes size of the panel. But guts change their sizes independently, in my case these two things are not related. Therefore I have the same problem but just size of JParameterPanel is smaller.

mustheroa at 2007-7-12 20:37:30 > top of Java-index,Desktop,Core GUI APIs...
# 12
with the size changed to ( 1000, 500 ), running it both ways (from reply #9),the size/relativity of the label and button (in JParameterPanel) is identicalwinxp pro, java 1.6.0_01
Michael_Dunna at 2007-7-12 20:37:30 > top of Java-index,Desktop,Core GUI APIs...
# 13
Thank you, finally it works! :)The problem was that I've messed up at the begining...
mustheroa at 2007-7-12 20:37:30 > top of Java-index,Desktop,Core GUI APIs...