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 6637693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:20:41+00:00 2026-05-25T23:20:41+00:00

I have a JLabel that contains variable text in a certain location in my

  • 0

I have a JLabel that contains variable text in a certain location in my GUI. The problem is that the text gets displayed at the bottom of the space where the JLabel is located. This does not convey to the end user the relevant information about the other contents of the GUI. Instead, I need the text of the JLabel to be printed in the middle of the vertical axis of the JLabel. A simplified version of my code is below. Can anyone show me how to alter it so that the text displays in the middle of the vertical axis instead of the bottom?

Main.java:

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

public class Main {
    public static void main(String[] args) {
        JFrame frame = new JFrame("Main");      
        frame.getContentPane().setLayout(new FlowLayout());
        frame.getContentPane().add(new VerticalLabel("Hello"));
        Dimension prefSize = new Dimension(400, 300);
        frame.setPreferredSize(prefSize);
        frame.setMinimumSize(prefSize);
        frame.pack();
        frame.setVisible(true);
    }
}

VerticalLabel.java:

import javax.swing.*;
import java.awt.*;
import javax.swing.border.EtchedBorder;

public class VerticalLabel extends JLabel {
    public VerticalLabel(String labelText) {
        Dimension myDim = new Dimension(15, 250);
        this.setPreferredSize(myDim);
        this.setHorizontalAlignment(LEFT);
        this.setVerticalAlignment(CENTER);
        this.setText(labelText);
        this.setVerticalTextPosition(CENTER);
        this.setUI(new VerticalLabelUI(false));
        this.setBorder(new EtchedBorder());
    }
}
  • 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-05-25T23:20:42+00:00Added an answer on May 25, 2026 at 11:20 pm

    Hardcoding a random preferred size is not a good idea.

    You wrote a custom UI, so it is the responsibility of the UI to paint the text in the proper position.

    Instead of creating a custom UI you can use the Text Icon approach to display vertical text. Create the label as follows:

    JLabel label = new JLabel();
    label.setHorizontalAlignment(JLabel.CENTER);
    TextIcon labelIcon = new TextIcon(label, "Hello", TextIcon.Layout.VERTICAL);
    label.setIcon( vIcon );
    

    Add the label to the CENTER of a panel using a BorderLayout and the vetical text will be centered vertically and horizontally.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a subclass of JLabel that forms a component of my GUI. I
I have a JTabbedPane with a custom tab component. That component contains a JLabel
I have a JLabel that needs to display some html-formatted text. However, I want
Right, I have a JTabbedPane that has a JPanel that contains a JLabel and
I have a JLabel which has a lot of text on it. Is there
Hi is it possible to have a JLabel text growing to the right instead
Here is the issue I am having. I have a JToolBar that contains 3
I created a array of JPanels which contains a common JLabel class gui {
I have a JLabel that I need to draw off screen with some Transforms
I have a JLabel, a JButton, and a JTextField all arranged horizontally in that

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.