Results 1 to 3 of 3
  1. #1
    aleplgr is offline Member
    Join Date
    Jul 2007
    Posts
    5
    Rep Power
    0

    Default BorderFactory to set position?

    Hi! I defined a BoxLayout JPanel because I need to show in this order:
    a checkbox
    a textfield
    another checkbox
    another texfield

    and this works fine:

    JPanel jPanelBoEx = new JPanel();
    jPanelVars.add(jPanelBoEx, BorderLayout.LINE_START);
    jPanelBoEx.setLayout(new BoxLayout(jPanelBoEx, BoxLayout.PAGE_AXIS));

    jPanelBoEx.add(jchkbox1);
    jPanelBoEx.add(jTextField1);
    jPanelBoEx.add(jchkbox2);
    jPanelBoEx.add(jTextField2);

    But I need not to show them in the center, they must be shifted some position to the right so I use this:

    jPanelBoEx.setBorder(BorderFactory.createEmptyBord er(0, 160, 0, 0));
    To shift the four of them to the right, but I need the two labels to be more to the right than the 2 checkboxes..

    How could I do that?
    Thanks in advance.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class Test {
        public static void main(String[] args) {
            JComponent[] comps = {
                new JCheckBox("CheckBox 1"), new JTextField(12),
                new JCheckBox("CheckBox 2"), new JTextField(12)
            };
            JPanel jPanelVars = new JPanel(new BorderLayout());
            JPanel jPanelBoEx = new JPanel();
            jPanelBoEx.setBorder(BorderFactory.createEmptyBorder(0, 160, 0, 0));
            jPanelVars.add(jPanelBoEx, BorderLayout.LINE_START);
            jPanelBoEx.setLayout(new BoxLayout(jPanelBoEx, BoxLayout.PAGE_AXIS)); 
    
            for(int j = 0; j < comps.length; j++) {
                adjustAlignment(comps[j]);
                if(j % 2 == 0) {
                    AbstractButton button = (AbstractButton)comps[j];
                    String text = button.getText();
                    button.setText("");
                    JLabel label = new JLabel(text);
                    //adjustAlignment(label);
                    Box box = Box.createHorizontalBox();
                    adjustAlignment(box);
                    box.add(button);
                    box.add(box.createHorizontalGlue());
                    box.add(label);
                    if(j == 2)
                        box.add(box.createHorizontalGlue());
                    jPanelBoEx.add(box);
                } else {
                    jPanelBoEx.add(comps[j]);
                }
            }
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(jPanelVars);        f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
        }
    
        private static void adjustAlignment(JComponent c) {
            String name = c.getClass().getName();
            name = name.substring(name.lastIndexOf(".")+1);
            System.out.printf("%10s alignmentX = %.3f  alignmentY = %.3f%n",
                               name, c.getAlignmentX(), c.getAlignmentY());
            float left   = Component.LEFT_ALIGNMENT;
            float center = Component.CENTER_ALIGNMENT;
            c.setAlignmentX(left);
        }
    }

  3. #3
    aleplgr is offline Member
    Join Date
    Jul 2007
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 03-28-2008, 10:26 AM
  2. How to get the position of character in TextArea
    By loganathan.lakshmanan in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 01-19-2008, 12:06 AM
  3. Help with position in java
    By fernando in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 07:54 PM
  4. Use the mouse position
    By susan in forum Java Applets
    Replies: 1
    Last Post: 07-28-2007, 11:10 PM
  5. JAVA Architect Position - Robert Half Technology
    By pegitha in forum Jobs Offered
    Replies: 0
    Last Post: 05-18-2007, 08:40 PM

Posting Permissions

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