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

  • SEARCH
  • Home
  • 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 6827747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:18:05+00:00 2026-05-26T22:18:05+00:00

I want to set text above and below a JButton ‘s icon. At the

  • 0

I want to set text above and below a JButton‘s icon. At the moment, in order to achieve this, I override the layout manager and use three JLabel instances (i.e. 2 for text and 1 for the icon). But this seems like a dirty solution.

Is there a more direct way of doing this?

Note –
I’m not looking for a multi-line solution, I’m looking for a multi-label solution. Although this article refers to it as a multi-line solution, it actually seems to refer to a multi-label solution.


EXAMPLE

import java.awt.Component;
import java.awt.FlowLayout;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public final class JButtonDemo {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }


    private static void createAndShowGUI(){
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new FlowLayout());
        frame.add(new JMultiLabelButton());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    private static final class JMultiLabelButton extends JButton
    {
        private static final long serialVersionUID = 7650993517602360268L;

        public JMultiLabelButton()
        {
            super();
            setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
            add(new JCenterLabel("Top Label"));
            add(new JCenterLabel(UIManager.getIcon("OptionPane.informationIcon"))); 
            add(new JCenterLabel("Bottom Label"));
        }
    }

    private static final class JCenterLabel extends JLabel
    {
        private static final long serialVersionUID = 5502066664726732298L;

        public JCenterLabel(final String s)
        {
            super(s);
            setAlignmentX(Component.CENTER_ALIGNMENT);
        }

        public JCenterLabel(final Icon i)
        {
            super(i);
            setAlignmentX(Component.CENTER_ALIGNMENT);
        }
    }

}

enter image description here

  • 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-26T22:18:06+00:00Added an answer on May 26, 2026 at 10:18 pm

    There is not way to split the text between the top/bottom of a JButton. This would involve custom painting.

    Since I’m not sure of your exact requirement I’ll just through out a few random ideas:

    1. You can use a JButton with text & icon. There are methods in the API that allow you to controal where text is positioned relative to the icon. Then you would need a second label for the other line of text. Basically the same as you current solution but you only need two labels.

    2. You could use the Text Icon and Compound Icon classes to create 1 Icon out of 3 separate Icons. Then you can just add the icon to a button.

    3. Use a JTextPane. Its supports an insertIcon() method. So you could add a line of text, add the icon and then add the other line of text. You can play with the paragraph attributes of the text pane to align the text horizontally within the space if you don’t want the text left justified. You can also play with the background color to make it look like a label.

    Example using the CompoundIcon:

    import java.awt.*;
    import javax.swing.*;
    
    public final class JButtonDemo {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable(){
                @Override
                public void run() {
                    createAndShowGUI();
                }
            });
        }
    
    
        private static void createAndShowGUI()
        {
            JButton button = new JButton();
    
            CompoundIcon icon = new CompoundIcon(CompoundIcon.Axis.Y_AXIS,
                new TextIcon(button, "Top Label"),
                UIManager.getIcon("OptionPane.informationIcon"),
                new TextIcon(button, "Bottom Label"));
    
            button.setIcon( icon );
            button.setFocusPainted( false );
    
            final JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setLayout(new FlowLayout());
            frame.add( button );
            frame.pack();
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to set the text on a JButton that is size 32x32 but
i have a problem, i want set text of a UILabel or UItextView or
I want to set a text in a label: labelDemnaechst.setText( Demnächst fällig:); On the
I want to set the Text of a TextBlock in my StatusBar before making
I want to set the text direction for some cells in a TextTable so
I want to set a text in a specific label on focus of an
I want to set up text in edit box programatically in other program, which
I want to set some text at a specified offset in my JTextArea .
SHORT DESCRIPTION OF PROBLEM: I want to set the text of a searchbar without
I have a ListActivity and I want to set programmatically the text of a

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.