Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swing JButton: Icon above Text

How can I create a JButton in Swing with the icon above the text?

like image 319
codegy Avatar asked Dec 10 '08 01:12

codegy


People also ask

What does JButton icon mean?

The JButton class is used to create a labeled button that has platform independent implementation. The application result in some action when the button is pushed. It inherits AbstractButton class.

How will you assign the string and icon both to the JButton?

To add icon to a button, use the Icon class, which will allow you to add an image to the button. Icon icon = new ImageIcon("E:\editicon. PNG"); JButton button7 = new JButton(icon);

How do I remove an icon from a JButton?

Simple, Set the icon to null . It doesn't have an icon, so it removes it. That will work perfectly.

How do I add text to a JButton?

By default, we can create a JButton with a text and also can change the text of a JButton by input some text in the text field and click on the button, it will call the actionPerformed() method of ActionListener interface and set an updated text in a button by calling setText(textField.


1 Answers

just do this:

      button.setVerticalTextPosition(SwingConstants.BOTTOM);     button.setHorizontalTextPosition(SwingConstants.CENTER);  
like image 107
berlindev Avatar answered Oct 05 '22 23:10

berlindev