Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8803887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:27:33+00:00 2026-06-14T01:27:33+00:00

For some reason, the Java Text JLabel does not center. I’ve looked up how

  • 0

For some reason, the “Java Text” JLabel does not center. I’ve looked up how to do it and seen various examples, the most promising being http://www.java2s.com/Code/Java/Swing-JFC/AsimpledemonstrationoftextalignmentinJLabels.htm but it’s not working. Here’s the whole code if you wish to run it yourself with a few in-code comments so you don’t have to bother searching through its entirety:

            import java.awt.BorderLayout;
            import java.awt.Color;
            import java.awt.Font;
            import java.awt.GridLayout;
            import java.awt.event.ActionEvent;
            import java.awt.event.ActionListener;

            import javax.swing.ButtonGroup;
            import javax.swing.JCheckBox;
            import javax.swing.JComboBox;
            import javax.swing.JFrame;
            import javax.swing.JLabel;
            import javax.swing.JPanel;
            import javax.swing.JRadioButton;
            import javax.swing.SwingConstants;

            public class FontViewer
            {
                static JCheckBox checkBoxBold;
                static JCheckBox checkBoxItalic;
                static JCheckBox checkBoxCenter;
                static JPanel textPanel;
                static JLabel textLabel;
                static JComboBox fontName;
                static JComboBox fontSize;
                static JRadioButton redButton;
                static JRadioButton whiteButton;
                static JRadioButton blueButton;


                static ActionListener listener;

                public static void main(String[] args)
                {
                    final int FRAME_SIZE_X = 250;
                    final int FRAME_SIZE_Y = 400;

                    JFrame frame = new JFrame();
                    frame.setSize(FRAME_SIZE_X, FRAME_SIZE_Y);

                    JPanel face = new JPanel();
                    face.setLayout(new GridLayout(2, 1));

            // listener inner class
                    class FontListener implements ActionListener
                    {
                        public void actionPerformed(ActionEvent event)
                        {
                            int fontStyle = 0;
                            if (checkBoxBold.isSelected())
                                fontStyle = fontStyle + Font.BOLD;
                            if (checkBoxItalic.isSelected())
                                fontStyle = fontStyle + Font.ITALIC;

            // this if statement does not work
                            if (checkBoxCenter.isSelected())
                                textLabel.setHorizontalAlignment(SwingConstants.CENTER);

                            String textFont = (String) fontName.getSelectedItem();

                            int textSize = Integer.parseInt((String) fontSize.getSelectedItem());

                            textLabel.setFont(new Font(textFont, fontStyle, textSize));

                            if (redButton.isSelected())
                                textLabel.setForeground(Color.RED);
                            else if (whiteButton.isSelected())
                                textLabel.setForeground(Color.WHITE);
                            else if (blueButton.isSelected())
                                textLabel.setForeground(Color.BLUE);

                            textLabel.repaint();
                        }
                    }

                    listener = new FontListener();

                    JPanel bottomFace = new JPanel();
                    bottomFace.setLayout(new GridLayout(3, 1));

                    textPanel = createTextPanel();

                    JPanel checkBoxPanel = createCheckBoxPanel();

                    JPanel comboPanel = createComboPanel();

                    JPanel radioButtonsPanel = createButtonsPanel();

                    face.add(textPanel);

                    bottomFace.add(checkBoxPanel);
                    bottomFace.add(comboPanel);
                    bottomFace.add(radioButtonsPanel);

                    face.add(bottomFace);

                    frame.add(face);

                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                }

                private static JPanel createTextPanel()
                {
                    final int DEFAULT_FONT_SIZE = 12;

                    textPanel = new JPanel();

                    textPanel.setLayout(new BorderLayout());
                    textLabel = new JLabel("Java Text");
                    textLabel.setFont(new Font("Times", 0, DEFAULT_FONT_SIZE));
                    textPanel.add(textLabel, BorderLayout.WEST);

                    return textPanel;
                }

            // check boxes created and programmed here
                private static JPanel createCheckBoxPanel()
                {
                    JPanel checkBoxPanel = new JPanel();

                    checkBoxBold = new JCheckBox("Bold");
                    checkBoxItalic = new JCheckBox("Italic");
                    checkBoxCenter = new JCheckBox("Center");

                    checkBoxBold.addActionListener(listener);
                    checkBoxItalic.addActionListener(listener);
                    checkBoxCenter.addActionListener(listener);

                    checkBoxPanel.add(checkBoxBold);
                    checkBoxPanel.add(checkBoxItalic);
                    checkBoxPanel.add(checkBoxCenter);

                    return checkBoxPanel;
                }

                private static JPanel createComboPanel()
                {
                    JPanel comboPanel = new JPanel();

                    fontName = new JComboBox();
                    fontName.addItem("Times");
                    fontName.addItem("Serif");
                    fontName.addItem("Courier");

                    fontSize = new JComboBox();
                    fontSize.addItem("12");
                    fontSize.addItem("24");
                    fontSize.addItem("36");

                    comboPanel.add(fontName);
                    comboPanel.add(fontSize);

                    fontName.addActionListener(listener);
                    fontSize.addActionListener(listener);

                    return comboPanel;
                }

                private static JPanel createButtonsPanel()
                {
                    JPanel radioButtonsPanel = new JPanel();

                    redButton = new JRadioButton("Red");
                    whiteButton = new JRadioButton("White");
                    blueButton = new JRadioButton("Blue");

                    redButton.addActionListener(listener);
                    whiteButton.addActionListener(listener);
                    blueButton.addActionListener(listener);

                    ButtonGroup colors = new ButtonGroup();
                    colors.add(redButton);
                    colors.add(whiteButton);
                    colors.add(blueButton);

                    radioButtonsPanel.add(redButton);
                    radioButtonsPanel.add(whiteButton);
                    radioButtonsPanel.add(blueButton);

                    return radioButtonsPanel;
                }
            }

I’m completely puzzled by this anomaly and any help or advice is greatly appreciated! Thank you so much in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:27:34+00:00Added an answer on June 14, 2026 at 1:27 am

    The textLabel is anchored to the BorderLayout.WEST position of your textPanel. Move it to the center:

    textPanel.add(textLabel, BorderLayout.CENTER);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've written a JavaFX application that uses Java Web Start. For some reason, the
I am having an odd error with Thread.sleep() on Java. For some reason, when
For some reason the script below is not working. This is the code I
For some reason this code results in a truncated text.txt file. It should (according
I have a bufferedreader and for some reason it wont read the text from
I'm making an application where I need to scroll some text on a java.awt.Canvas
For some reason, I can't get text to show up in my list view.
I always have issues for some reason when trying to set a textviews text
I have the following task and for some reason is not matching my file:
For some reason, this line of code is returning undefined for $(this).attr(href) $(a).attr(href, javascript:page('

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.