How to align Jbutton text to the left of the icon?

Asked

Viewed 698 times

0

I need to align the text of JButton the left of the icon as I do?

I tried to use the method

 button.setHorizontalAlignment(SwingConstants.LEFT);

but this method does not align the text the way I want it.

It’s getting like the first but I want it to look like the second:

inserir a descrição da imagem aqui

Here is a generic code to create the button:

  public void genericMethod(){
          JFrame frame = new JFrame("JFrame Example");
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        JButton button = new JButton();
        button.setText("GenericButton");
        button.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/nextnext.png")));
        button.setHorizontalAlignment(SwingConstants.RIGHT);
        panel.add(button);
        frame.add(panel);
        frame.setSize(300, 300);

        frame.setVisible(true);
    }
    public static void main(String s[]) {
     new NewClass().genericMethod();

    }

The return of the above code is:

inserir a descrição da imagem aqui

  • The answer you already gave in the question, is just the button.setHorizontalAlignment(SwingConstants.LEFT); You’re not even using that in the code.

  • with this method the icon continues left even if I change the parameter to RIGHT.

1 answer

3


Try the following:

jButton.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);          
jButton.setHorizontalAlignment(SwingConstants.RIGHT);                
jButton.setHorizontalTextPosition(SwingConstants.LEFT);

Source: Changing Icon Position On Jbutton


I did some tests here and it only took the line below to make the arrow to the right of the text. Do the test yourself, replacing the path of the two buttons of the code below and pointing to your icon:

import java.awt.FlowLayout;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class JButtonAlinhaTextIconTest {

    public void genericMethod() {

        JFrame frame = new JFrame("JFrame Example");
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout());
        JButton button1 = new JButton();
        JButton button2 = new JButton();

        button1.setText("button1");
        button2.setText("button2");

        try {
            button1.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/res/arrow-right-black.png"))));
            button2.setIcon(new ImageIcon(ImageIO.read(getClass().getResource("/res/arrow-right-black.png"))));
        } catch (IOException e) {
            e.printStackTrace();
        }


        button1.setHorizontalTextPosition(SwingConstants.LEFT);
        panel.add(button1);
        panel.add(button2);
        frame.add(panel);
        frame.setSize(300, 300);

        frame.setVisible(true);
    }

    public static void main(String s[]) {

        SwingUtilities.invokeLater(() -> new JButtonAlinhaTextIconTest().genericMethod());
    }
}

Only the button1 was applied to the configuration and only it aligned the icon to the left. See the result I got:

inserir a descrição da imagem aqui

  • Look, my code is the same as the question but it’s leaving the icon on the left anyway.

  • Worked perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.