Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Set Horizontal Alignment on JLabel - wrong implementation?

843806Dec 17 2007 — edited Dec 17 2007
Hi again.

I am studying by myself, and my last resort is to show you my unfinished code. This is supposed to move the alignment of the JLabel at the top of the window depending on the ComboBox choice. I did not implement the other ItemListeners yet because I can't make the first one work. I know I have an error somewhere... anyway thanks in advance for your time.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DemoJLabel extends JFrame {

    private ImageIcon icon = new ImageIcon("image/grapes.gif");
    private JLabel jlblGrape = new JLabel("Grapes", icon, SwingConstants.CENTER);
    private String[] horizontalAlign = {"LEFT", "CENTER", "RIGHT",
        "LEADING", "TRAILING"
    };
    private String[] verticalAlign = {"TOP", "CENTER", "BOTTOM"};
    private String[] horizontalTP = {"LEFT", "CENTER", "RIGHT", "LEADING",
        "TRAILING"
    };
    private String[] verticalTP = {"TOP", "CENTER", "BOTTOM"};
    private JComboBox jcboHA = new JComboBox(horizontalAlign);
    private JComboBox jcboVA = new JComboBox(verticalAlign);
    private JComboBox jcboHT = new JComboBox(horizontalTP);
    private JComboBox jcboVT = new JComboBox(verticalTP);

    public DemoJLabel() {

        JPanel p1 = new JPanel();
        p1.setLayout(new FlowLayout());
        p1.setBorder(BorderFactory.createLineBorder(Color.BLACK));
        p1.add(jlblGrape);

        JPanel p2 = new JPanel();
        p2.setLayout(new GridLayout(2, 1));
        p2.add(new JLabel("Horizontal"));
        p2.add(new JLabel("Vertical"));

        JPanel p3 = new JPanel();
        p3.setLayout(new GridLayout(2, 1));
        p3.add(jcboHA);
        p3.add(jcboVA);


        JPanel p4 = new JPanel();
        p4.setLayout(new BorderLayout());
        p4.setBorder(BorderFactory.createTitledBorder("Alignment"));
        p4.add(p2, BorderLayout.WEST);
        p4.add(p3, BorderLayout.EAST);

        JPanel p5 = new JPanel();
        p5.setLayout(new GridLayout(2, 1));
        p5.add(new JLabel("Horizontal"));
        p5.add(new JLabel("Vertical"));

        JPanel p6 = new JPanel();
        p6.setLayout(new GridLayout(2, 1));
        p6.add(jcboHT);
        p6.add(jcboVT);

        JPanel p7 = new JPanel();
        p7.setLayout(new BorderLayout());
        p7.setBorder(BorderFactory.createTitledBorder("Text Position"));
        p7.add(p5, BorderLayout.WEST);
        p7.add(p6, BorderLayout.EAST);

        JPanel p8 = new JPanel();
        p8.setLayout(new GridLayout());
        p8.add(p4);
        p8.add(p7);

        setLayout(new GridLayout(2, 1));
        add(p1);
        add(p8);

        jcboHA.addItemListener(new ItemListener() {

            public void itemStateChanged(ItemEvent e) {
                //For debugging
                System.out.println(jcboHA.getSelectedItem());
                String choice = jcboHA.getSelectedItem().toString();
                if (choice.equals("LEFT")) {
                    jlblGrape.setHorizontalAlignment(JLabel.LEFT);
                } else if (choice.equals("CENTER")) {
                    jlblGrape.setHorizontalAlignment(JLabel.CENTER);
                } else if (choice.equals("RIGHT")) {
                    jlblGrape.setHorizontalAlignment(JLabel.RIGHT);
                } else if (choice.equals("LEADING")) {
                    jlblGrape.setHorizontalAlignment(JLabel.LEADING);
                } else {
                    jlblGrape.setHorizontalAlignment(JLabel.TRAILING);
                }
            }
        });

    // Other ItemListeners for the other combo boxes
    }

    public static void main(String[] args) {
        DemoJLabel frame = new DemoJLabel();
        frame.setTitle("Demonstrating JLabel");
        frame.setSize(400, 350);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 14 2008
Added on Dec 17 2007
2 comments
410 views