The simplest thing is to change another component that supports word wrap so that it acts like a button. I made a simple class that manipulates JTextArea to act like a Button.
public class MultiLineButton extends JTextArea implements MouseListener { private static final long serialVersionUID = 1L; private Color defaultColor; private Color highlight, lightHighlight; private BtnState state; private List<ActionListener> actionListeners; public MultiLineButton(String text, Color defaultColor) { this.setEditable(false); this.setText(text); this.setLineWrap(true); this.setWrapStyleWord(true); this.addMouseListener(this); this.setBorder(new EmptyBorder(5, 10, 5, 10)); state = BtnState.NORMAL; this.defaultColor = defaultColor; this.setBackground(defaultColor); highlight = new Color(122, 138, 153); lightHighlight = new Color(184, 207, 229);
BtnState is just an enumeration with the constants NORMAL, HOVERED, CLICKED
Most of the code is only used to make JTextArea look like JButton, and it works very well. One of the drawbacks is that you lose the ability to change it using ButtonModels, but for most applications this will be enough.
Alexander Daum Jan 19 '17 at 20:36 2017-01-19 20:36
source share