Ahamed Zahid
Ahamed Zahid

Reputation: 31

Changing Icon Position On Jbutton

I have been developing a java project on netbeans. I want to know how to move the icon on JButton to left or right corner when text on center. Can you please help me ?

https://i.sstatic.net/PiLhq.jpg

Upvotes: 1

Views: 3531

Answers (2)

ziLk
ziLk

Reputation: 3200

I made a demo test for you.

public class Test3 {
        private static final int WIDTH = 300;

        public Test3() {
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(WIDTH, 500);
            frame.setResizable(false);
            frame.setLayout(new BorderLayout());

            JPanel panel = (JPanel) frame.getContentPane();
            panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));

            //my icons
            Icon iconOne = UIManager.getIcon("OptionPane.informationIcon");
            Icon iconTwo = UIManager.getIcon("OptionPane.questionIcon");
            Icon iconThree = UIManager.getIcon("OptionPane.errorIcon");
            Icon iconFour = UIManager.getIcon("OptionPane.warningIcon");

            JButton jButton1 = createButton(iconOne, "Button 1", true) ;
            JButton jButton2 = createButton(iconTwo, "Button 2 sadasddfgdgdfgd", true) ;
            JButton jButton3 = createButton(iconThree, "Button 3 sad asd a", true) ;
            JButton jButton4 = createButton(iconFour, "Button 4 sadasd asdfrfere", true) ;
            JButton jButton5 = createButton(iconTwo, "Button 5 sad", false) ;
            JButton jButton6 = createButton(iconFour, "Button 6 sadrfere", false) ;
            JButton jButton7 = createButton(iconThree, "Button 7 sadarfere", false) ;
            JButton jButton8 = createButton(iconFour, "Button 8 sadasd asdfrfere", false) ;

            panel.add(jButton1);
            panel.add(jButton2);
            panel.add(jButton3);
            panel.add(jButton4);
            panel.add(jButton5);
            panel.add(jButton6);
            panel.add(jButton7);
            panel.add(jButton8);

            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

        public static void main(String[] args) {
            new Test3();
        }

        private JButton createButton(Icon p_jIcon, String p_strButtonText, boolean p_bIsIconLeftSide){
            int nButtonHeight = 60;
            int nGap = 40;

            JButton jButton = new JButton();
            jButton.setIcon(p_jIcon);
            jButton.setIconTextGap(nGap);

            if(p_bIsIconLeftSide){
                jButton.setHorizontalAlignment(SwingConstants.LEFT);
                jButton.setHorizontalTextPosition(SwingConstants.RIGHT);
                jButton.setText(p_strButtonText);
            }else{
                //if you want to set icon position to right side of the button
                jButton.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
                jButton.setHorizontalAlignment(SwingConstants.RIGHT);
                jButton.setHorizontalTextPosition(SwingConstants.LEFT);
                jButton.setText("<html><div align=left width=200px>" + p_strButtonText + "</div></html>");
            }

            Dimension jSize = new Dimension(WIDTH, nButtonHeight);
            jButton.setPreferredSize(jSize);
            jButton.setMaximumSize(jSize);
            jButton.setMinimumSize(jSize);
            return jButton;
        }
    }

demo screenshot

Upvotes: 5

camickr
camickr

Reputation: 324128

It is not supported in the base JDK. There is no such thing as centering the text only. Both the text and Icon are painted together, either left, center, or right aligned.

However, you can use the Component Border class.

This is a custom Border that allows you to add a component to the Border. So you can create a JLabel containing your Icon and use the label as the ComponentBorder

Then you can just set the text of your button and now the text will centered within the bounds of the button and its Border.

Basic code:

JButton button = new JButton("Centered Text");
JLabel label = new JLabel( new ImageIcon("...") );

ComponentBorder cb = new ComponentBorder( label );
cb.setEdge( ComponentBorder.Edge.LEFT );
cb.install( button );

add(button, BorderLayout.NORTH);

Upvotes: 2

Related Questions