Results 1 to 4 of 4
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default component orientation

    This is a test program that should make me understand component orientation
    in a local context (run the code and you'll see what I mean)!
    I wonder why I can not use the line:
    l.setHorizontalAlignment(SwingConstants.LEADING);
    see code line 48




    Java Code:
    package swing;
    
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class TestOrientation {
        static JFrame f;
        static JPanel p;
        static JLabel l;
        public static void main(String[] args) {
            f = new JFrame("Right-to-Left FlowLayout #2");
            p = new JPanel();
    
            ActionListener bl = new buttonListener();
            Insets margin = new Insets(5, 14, 5, 14);
            p.setLayout(new FlowLayout(FlowLayout.LEADING, 4, 4));   
            p.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
            System.out.println("LEADING LEFT_TO_RIGHT");
            
            for (int i = 1; i <= 4; i++) {
                JButton b = new JButton("" + i);
                l = new JLabel("button " + i);
                b.setActionCommand("" + i);
                l.setLabelFor(b);
                b.addActionListener(bl);
                b.setMargin(margin);
                p.add(l);
                p.add(b);
            }
            f.getContentPane().add(p);
            f.addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent evt) {
                    System.exit(0);
                }
            });     
            f.setBounds(0, 0, 700, 80);
            f.setVisible(true);
        }
    
    
        static class buttonListener implements ActionListener{
    
            public void actionPerformed(ActionEvent ae) {
                String cmd = ae.getActionCommand();
                    if ((cmd.equals("1"))) {
                        System.out.println("LEADING LEFT_TO_RIGHT");
                      //  l.setHorizontalAlignment(SwingConstants.LEADING);
                        p.setLayout(new FlowLayout(FlowLayout.LEADING, 4, 4));
                        p.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
                        p.revalidate();
                    }
                    if ((cmd.equals("2"))) {
                        System.out.println("TRAILING LEFT_TO_RIGHT");
                     //   l.setHorizontalAlignment(SwingConstants.TRAILING);
                        p.setLayout(new FlowLayout(FlowLayout.TRAILING, 4, 4));
                        p.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
                        p.revalidate();
                    }
                    if ((cmd.equals("3"))) {
                        System.out.println("LEADING RIGHT_TO_LEFT");
                      //  l.setHorizontalAlignment(SwingConstants.LEADING);
                        p.setLayout(new FlowLayout(FlowLayout.LEADING, 4, 4));
                        p.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                        p.revalidate();
                    }
                    if ((cmd.equals("4"))) {
                        System.out.println("TRAILING RIGHT_TO_LEFT");
                      //  l.setHorizontalAlignment(SwingConstants.TRAILING);
                        p.setLayout(new FlowLayout(FlowLayout.TRAILING, 4, 4));
                        p.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                        p.revalidate();
                    }
            }
    
        }
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,501
    Rep Power
    5

    Default Re: component orientation

    Since the particular method is setting a Java Beans bound property I assume it expects you to implement a propertyListener to catch changes to the
    alignment property and make the appropriate changes.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: component orientation

    OK jim that goes still over my head, but I am getting deeper into it and might understand your comment soon, thanks!
    BTW what about this quote from the JFC book
    Applying the component orientation to a complete application is not as simple as you might think. What you really want is to be able to set a default ComponentOrientation and have that applied to each Component as it is created. As long as your application did not change locale, this would enable you to entirely forget about manipulating the orientation once the correct default was selected.

    "Unfortunately, this feature is not implemented....."
    Is that the case nowadays?

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: component orientation

    Of course it does not work jim, because I invoke the position change on the label and not on the (layout) of the label, hahaha

    l.setHorizontalAlignment(SwingConstants.LEADING);

    where "l" is JLabel

Similar Threads

  1. Object Orientation Help
    By rookiecoder in forum New To Java
    Replies: 7
    Last Post: 09-19-2012, 12:58 PM
  2. Object Orientation?
    By atul_kumar2038 in forum New To Java
    Replies: 2
    Last Post: 09-14-2010, 07:21 PM
  3. Object Orientation - Arrays
    By Boysie in forum Jobs Discussion
    Replies: 4
    Last Post: 04-14-2010, 09:32 PM
  4. change viewing platform orientation
    By Bit2_Gosu in forum New To Java
    Replies: 1
    Last Post: 02-15-2009, 08:46 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
  •