1

I have a JButton with an image icon and I need to position the text. Is there a way to position the text of a JButton in a specific position instead of using CENTER,LEADING, TOP and so on?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3354252
  • 63
  • 1
  • 5
  • Short answer, no. Long answer, depends, but you can use `` to gain some more control, but you'd need to mix the image and text within the html directly... – MadProgrammer Mar 05 '14 at 04:01

3 Answers3

2

I've never tried it on a JButton before, but maybe you can add a JLabel as a component on the button. Then you can use layout managers or Borders to position the label more appropriately.

Here are some example of adding a component to a JLabel with an Icon:

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

public class LabelImageText extends JPanel
{
    public LabelImageText()
    {
        JLabel label1 = new JLabel( new ColorIcon(Color.ORANGE, 100, 100) );
        label1.setText( "Easy Way" );
        label1.setHorizontalTextPosition(JLabel.CENTER);
        label1.setVerticalTextPosition(JLabel.CENTER);
        add( label1 );

        //

        JLabel label2 = new JLabel( new ColorIcon(Color.YELLOW, 200, 150) );
        label2.setLayout( new BoxLayout(label2, BoxLayout.Y_AXIS) );
        add( label2 );

        JLabel text = new JLabel( "More Control" );
        text.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        label2.add( Box.createVerticalGlue() );
        label2.add( text );
        label2.add( Box.createVerticalStrut(10) );

        //

        JLabel label3 = new JLabel( new ColorIcon(Color.GREEN, 200, 150) );
        label3.setLayout( new GridBagLayout() );
        add( label3 );

        JLabel text3 = new JLabel();
        text3.setText("<html><center>Text<br>over<br>Image<center></html>");
        text3.setLocation(20, 20);
        text3.setSize(text3.getPreferredSize());
        label3.add( text3 );

        //

        JLabel label4 = new JLabel( new ColorIcon(Color.CYAN, 200, 150) );
        add( label4 );

        JTextPane textPane = new JTextPane();
        textPane.setText("Add some text that will wrap at your preferred width");
        textPane.setEditable( false );
        textPane.setOpaque(false);
        SimpleAttributeSet center = new SimpleAttributeSet();
        StyleConstants.setAlignment(center, StyleConstants.ALIGN_CENTER);
        StyledDocument doc = textPane.getStyledDocument();
        doc.setParagraphAttributes(0, doc.getLength(), center, false);
        textPane.setBounds(20, 20, 75, 100);
        label4.add( textPane );
    }

    public static class ColorIcon implements Icon
    {
        private Color color;
        private int width;
        private int height;

        public ColorIcon(Color color, int width, int height)
        {
            this.color = color;
            this.width = width;
            this.height = height;
        }

        public int getIconWidth()
        {
            return width;
        }

        public int getIconHeight()
        {
            return height;
        }

        public void paintIcon(Component c, Graphics g, int x, int y)
        {
            g.setColor(color);
            g.fillRect(x, y, width, height);
        }
    }

    private static void createAndShowUI()
    {
        JFrame frame = new JFrame("LabelImageText");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add( new LabelImageText() );
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible( true );
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

I doubt the JTExtPane example will work with a button because the text pane will intercept the mouse events and so you will not be able to click the button when you click on the text. So I would stick with the examples that add a label to the button.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Thanks for that, helped a lot. Another thing, how do I use the same label text? I am doing i18n and every time I switch locales, it adds a new label on top of the previous one created or it doesn't reset the text. – user3354252 Mar 05 '14 at 07:14
  • I probably is not resetting the text. That is something you will need to do manually. I'm not sure what listener you would use. – camickr Mar 05 '14 at 16:13
0

Spanish: Puedes sobreescribir el método setText(String str).

Google Traductor English: You can overwrite the method setText (String str).

private static final String HTML_1 = "<html><body style='margin:0px 0px 0px 0px'>";
private static final String HTML_2 = "</html>";

@Override
public void setText(String txt) {
    if (txt.isEmpty()) {
        txt = "Dispositivo";
    }
    String txt2 = HTML_1 + txt + HTML_2;
    super.setText(txt2);
}
-1

you can use set bounds method to set the postion of the text on the button

JLabel positiongLabel = new JLabel();
positiongLabel.setBounds(100,50,300,30);

for more detail click here

Engineer
  • 1,436
  • 3
  • 18
  • 33
  • @camickr now it is not depends upon SWT – Engineer Mar 05 '14 at 04:56
  • Now you are using AWT components not Swing coponents. Also, it doesn't answer the question. The question is about positioning the text within the button, not on positioning the button within the frame. Also you should not use setBounds() sot positions components. You should be using layout managers. – camickr Mar 05 '14 at 05:01