Posted By:
Abdul_Ambalam
Posted On:
Thursday, July 26, 2001 10:39 PM
I added some 10 labels to JPanel each one on one row using GridBag layout. JPanel is added to the JScrollPane which is added to JFrame. When I resized the JFrame all labels were aligned to the center. I tried weightx and weighty field for the components to be placed at the same area. but still the components got adjusted to the x and y coordiantes when I resized the frame. I want all the components to be placed as it is start from left corner. How can I do it? I used null layout and setBounds() method as alternate way but ScrollBar in the SrollPane is not seen. Can you help me to rectify the problem. this is my code import javax.swing.*; import java.awt.*; public class swing extends JFrame{ publ
More>>
I added some 10 labels to JPanel each one on one row using GridBag layout. JPanel is added to the JScrollPane which is added to JFrame. When I resized the JFrame all labels were aligned to the center. I tried weightx and weighty field for the components to be placed at the same area. but still the components got adjusted to the x and y coordiantes when I resized the frame. I want all the components to be placed as it is start from left corner.
How can I do it? I used null layout and setBounds() method as alternate way but ScrollBar in the SrollPane is not seen. Can you help me to rectify the problem.
this is my code
import javax.swing.*;
import java.awt.*;
public class swing extends JFrame{
public swing(){
GridBagLayout gl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
JPanel p=new JPanel();
p.setLayout(gl);
JLabel l1=new JLabel("Label 1");
JLabel l2=new JLabel("Label 2");
JLabel l3=new JLabel("Label 3");
JLabel l4=new JLabel("Label 4");
gbc.gridx=0;gbc.gridy=0;
gbc.insets=new Insets(20,20,20,20);
gbc.weightx=0;gbc.weighty=0;
gbc.ipadx=0;gbc.ipady=0;gbc.gridwidth=1;gbc.gridheight=1;
gbc.fill=GridBagConstraints.NONE;
gbc.anchor=GridBagConstraints.NORTHWEST;
gl.setConstraints(l1,gbc);
p.add(l1);
gbc.gridx=0;gbc.gridy=1;
gbc.anchor=GridBagConstraints.NORTHWEST;
gl.setConstraints(l2,gbc);
p.add(l2);
gbc.gridx=0;gbc.gridy=2;
gbc.anchor=GridBagConstraints.NORTHWEST;
gl.setConstraints(l3,gbc);
p.add(l3);
gbc.gridx=0;gbc.gridy=3;
gbc.anchor=GridBagConstraints.NORTHWEST;
gl.setConstraints(l4,gbc);
p.add(l4);
JScrollPane sp=new JScrollPane(p);
getContentPane().add(sp);
setSize(200,200);
setVisible(true);
}
public static void main(String args[]){
swing s=new swing();
}
}
<<Less