How to set text above and below the JButton icon?

I want to set the text above and below a JButton . At the moment, for this I redefine the layout manager and use three instances of JLabel (i.e. 2 for text and 1 for icon). But this seems like a dirty decision.

Is there a more direct way to do this?

Note.
I am not looking for a multi-line solution, I am looking for a multi-label solution. Although this article refers to it as a multi-linear solution, it appears 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

+3
source share
3 answers

Cannot split text between top / bottom of JButton. This will include regular painting.

Since I'm not sure about your specific requirement, I will just consider a few random ideas:

  • You can use JButton with text and icon. There are methods in the API that let you determine where the text is relative to the icon. Then you need a second label for another line of text. This is basically the same as the current solution, but you only need two labels.

  • You can use the Text Icon and Component Icon classes to create 1 icon from 3 separate icons. Then you can simply add an icon to the button.

  • Use JTextPane. It supports the insertIcon () method. So you can add a line of text, add an icon, and then add another line of text. You can play with the paragraph attributes of the text panel to align the text horizontally inside the space if you do not want the text to remain justified. You can also play with the background color to make it look like a shortcut.

CompoundIcon example:

 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); } } 
+5
source

you have two four options

1) use JLabel + Icon + Html (<= Html 3.2)

2) use XxxButtonUI and override all the necessary methods from the API

3) JLayeredPane with transparency ???, another layout with transparency, like JLabel or JComponent for JButton,

4) there are many graphical SWs that can prepare Background as * .jpg for the icon, then it is very simple to change something according to the event, action or actual setting of JButton

Wrong way looking for JLabel + Whatever it is instead of JButton, I think this is a half-sized solution

+2
source
-1
source

Source: https://habr.com/ru/post/1381546/


All Articles