Results 1 to 8 of 8
  1. #1
    masa is offline Member
    Join Date
    Nov 2008
    Posts
    21
    Rep Power
    0

    Default arrangement of components

    hi
    i am trying to add some components to a jframe. but as its layout is flowlayout by default its arranging one by one. i want to know by using which layout i can arrange components wherever i like. please help me out.
    for example i have login page i want to arrange the labels,textfields in the centre of the page and the login button below them.
    thanks in advance.........

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    masa is offline Member
    Join Date
    Nov 2008
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    You can use GridLayout for that in simple way.
    i have used gridlayout but the components are not starting in the middle of the JPanel....i am posting the code in which the components are displayed on the top of the jpanel. i want to move them to the centre...can you correct me please.

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.JFrame;
    import javax.swing.*;

    public class ExamplePanel extends JPanel implements ActionListener{
    private JLabel heading;
    private JLabel cabinetName;
    private JTextField cabinetField;
    private JLabel xmlLabel;
    private JTextField xmlField;
    private JLabel contentLabel;
    private JTextField contentField;
    private JButton xmlBtnBrowse;
    private JButton contentBtnBrowse;
    private JPanel panel;
    private JButton extract;

    public ExamplePanel()
    {
    initFields();
    setPanels();
    //setPanels1();
    }

    public void initFields()
    {
    heading=new JLabel("Example");
    cabinetName = new JLabel("Enter name : ");
    cabinetField = new JTextField();
    cabinetField.setColumns(15);
    xmlLabel = new JLabel(" File System Path :");
    xmlField = new JTextField();
    xmlField.setColumns(30);
    contentLabel = new JLabel("Another Path :");
    contentField = new JTextField();
    contentField.setColumns(30);
    xmlBtnBrowse = new JButton("Browse");
    contentBtnBrowse = new JButton("Browse");
    extract=new JButton("Extract");


    }

    public void actionPerformed(ActionEvent event)
    {
    JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_O NLY);
    if(event.getSource().equals(xmlBtnBrowse))
    {
    int returnval = fc.showOpenDialog(this);
    System.out.println("returnval " + returnval + "arpprove" + JFileChooser.APPROVE_OPTION);
    if( returnval == JFileChooser.APPROVE_OPTION)
    xmlField.setText(fc.getSelectedFile().getPath());
    }
    if(event.getSource().equals(contentBtnBrowse))
    {
    int returnval = fc.showOpenDialog(this);
    System.out.println("returnval " + returnval + "arpprove" + JFileChooser.APPROVE_OPTION);
    if( returnval == JFileChooser.APPROVE_OPTION)
    contentField.setText(fc.getSelectedFile().getPath( ));
    }

    return;
    }

    private void setPanels()
    {
    panel = new JPanel();
    GridBagConstraints gbcBag;
    panel.setLayout(new GridBagLayout());
    gbcBag = new GridBagConstraints();
    gbcBag.gridwidth = 2;


    gbcBag.gridx = 0;
    gbcBag.gridy = 1;
    gbcBag.anchor = java.awt.GridBagConstraints.WEST;
    gbcBag.insets = new java.awt.Insets(4, 4, 4, 4);

    panel.add(cabinetName, gbcBag);

    gbcBag.gridx = 5;
    gbcBag.gridy = 1;
    gbcBag.anchor = java.awt.GridBagConstraints.WEST;
    gbcBag.insets = new java.awt.Insets(4, 4, 4, 4);

    panel.add(cabinetField, gbcBag);

    gbcBag.gridx = 0;
    gbcBag.gridy = 2;
    gbcBag.anchor = java.awt.GridBagConstraints.WEST;
    gbcBag.insets = new java.awt.Insets(4, 4, 4, 4);

    panel.add(xmlLabel, gbcBag);

    gbcBag.gridx = 5;
    gbcBag.gridy = 2;
    gbcBag.anchor = java.awt.GridBagConstraints.WEST;
    gbcBag.insets = new java.awt.Insets(4, 4, 4, 4);

    panel.add(xmlField, gbcBag);


    gbcBag.gridx = 10;
    gbcBag.gridy = 2;
    gbcBag.anchor = java.awt.GridBagConstraints.WEST;
    gbcBag.insets = new java.awt.Insets(4, 4, 4, 4);

    panel.add(xmlBtnBrowse, gbcBag);


    gbcBag.gridx = 0;
    gbcBag.gridy = 3;
    gbcBag.anchor = java.awt.GridBagConstraints.WEST;
    gbcBag.insets = new java.awt.Insets(4, 4, 4, 4);

    panel.add(contentLabel, gbcBag);

    gbcBag.gridx = 5;
    gbcBag.gridy = 3;
    gbcBag.anchor = java.awt.GridBagConstraints.WEST;
    gbcBag.insets = new java.awt.Insets(4, 4, 4, 4);

    panel.add(contentField, gbcBag);


    gbcBag.gridx = 10;
    gbcBag.gridy = 3;
    gbcBag.anchor = java.awt.GridBagConstraints.WEST;
    gbcBag.insets = new java.awt.Insets(4, 4, 4, 4);

    panel.add(contentBtnBrowse, gbcBag);

    gbcBag.gridx = 0;
    gbcBag.gridy = 4;
    gbcBag.anchor = java.awt.GridBagConstraints.WEST;
    gbcBag.insets = new java.awt.Insets(4, 4, 4, 4);

    panel.add(extract, gbcBag);


    add(panel);
    xmlBtnBrowse.addActionListener(this);
    contentBtnBrowse.addActionListener(this);
    extract.addActionListener(this);
    }

    private void addcontrols()
    {
    }

    public static void main(String[] args)
    {
    System.out.println("Building the frame");
    JFrame frmFrame = new JFrame();
    ExamplePanel ePanel = new ExamplePanel();
    frmFrame.setContentPane(ePanel);
    frmFrame.setSize(new Dimension(650, 400));
    frmFrame.setDefaultCloseOperation(JFrame.EXIT_ON_C LOSE);
    frmFrame.setVisible(true);
    frmFrame.show();
    }
    }


    i want to move the components to the center....

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  5. #5
    masa is offline Member
    Join Date
    Nov 2008
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    Are you look at vertical/horizontal spaces?

    i didn't get you....which vertical/horizontal spaces. what are the changes to be made to the code to move the components to the center...........

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    have you tried adding your ExamplePanel object to the contentPane using a GridBagLayout with default GridBagConstraints? Something like so:
    Java Code:
      public static void main(String[] args)
      {
        System.out.println("Building the frame");
        JFrame frmFrame = new JFrame();
        ExamplePanel ePanel = new ExamplePanel();
        
        JPanel contentPane = (JPanel)frmFrame.getContentPane();
        contentPane.setLayout(new GridBagLayout());
        frmFrame.add(ePanel, new GridBagConstraints());
        //frmFrame.setContentPane(ePanel);
        frmFrame.setPreferredSize(new Dimension(650, 400));
        frmFrame.pack();
        frmFrame.setLocationRelativeTo(null);
        frmFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmFrame.setVisible(true);
        //frmFrame.show();
      }

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by masa View Post
    i didn't get you....which vertical/horizontal spaces. what are the changes to be made to the code to move the components to the center...........
    In between controls you can define space. So what you have to do is, define the correct number of rows and columns in the grid according to your design. Then define the spaces in between all grids as well. Read GridLayout API for more information.

    Basically you have to work on with two constructors in GridLayout class.

    Java Code:
    public GridLayout(int rows, int columns)
    public GridLayout(int rows, int columns,
                      int horizontalGap, int verticalGap)

  8. #8
    masa is offline Member
    Join Date
    Nov 2008
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by Eranga View Post
    In between controls you can define space. So what you have to do is, define the correct number of rows and columns in the grid according to your design. Then define the spaces in between all grids as well. Read GridLayout API for more information.

    Basically you have to work on with two constructors in GridLayout class.

    Java Code:
    public GridLayout(int rows, int columns)
    public GridLayout(int rows, int columns,
                      int horizontalGap, int verticalGap)
    Thanks for all ......I got the solution from Fuberable. Now I have another problem. I have got a jtree with checkbox and I am able to display custom nodes and the tree structure. Now I want to check on any node in the tree and retrieve that nodes name and print it on the textfield on the other side. For this i used JSplitPane. Now whenever I am clicking on a node the name should be displayed on textfield of the next pane. how to write listener for this checkbox.........

Similar Threads

  1. Problem In Swing Components
    By SANDY_INDIA in forum AWT / Swing
    Replies: 1
    Last Post: 07-19-2008, 11:23 PM
  2. Components of SWingx.
    By SANDY_INDIA in forum Advanced Java
    Replies: 4
    Last Post: 07-14-2008, 10:46 AM
  3. <tr><td> arrangement in a method
    By am17mu in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-16-2008, 06:44 PM
  4. GUI components not display
    By Eranga in forum AWT / Swing
    Replies: 2
    Last Post: 03-12-2008, 03:16 AM
  5. Gui Components
    By Marty in forum New To Java
    Replies: 1
    Last Post: 05-28-2007, 05:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •