0

I have a JFrame with a scrollpane and a JList. For some reason I can't rename these buttons and the originally set text is not there.

private DefaultListModel<JButton> model = new DefaultListModel<>();
private JList<JButton> emailList = new JList<>(model);
private JButton test = new JButton("test");

In constructor:

    JScrollPane scroll = new JScrollPane();
    scroll.getViewport().setView(emailList);
    scroll.setMinimumSize(new Dimension(500, 350));
    add(scroll, BorderLayout.SOUTH);
    model.addElement(test);

The name of this button ends up being

javax.swing.JButton[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.BorderUIResource$CompoundBorderUIResource@1de0aca6,flags=296,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=2,left=14,bottom=2,right=14],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=testar,defaultCapable=true]
  • 2
    `toString()` method does not print the name of the button. To get the name, use `getName()`. That is, only if you used `setName("some name")`. If you only used the constructor, then you need to use `getText()` to get the text that is displayed in the GUI – XtremeBaumer Mar 18 '19 at 15:31
  • I don't want to get the name, I want to set it to something other than that long line. setName() does not work – user10990200 Mar 18 '19 at 15:35
  • As I told you. `toString()` will never change, no matter what you set the text/name to – XtremeBaumer Mar 18 '19 at 15:36
  • `JList` calls `toString`, which returns that annoyingly long name. If you want the name that you gave it, make an adaptor class whose `toString` does what you want. – Silvio Mayolo Mar 18 '19 at 15:36

1 Answers1

0

Display text of JList items are derived by calling toString() on those items. JButton.toString() returns that long text "javax.swing.JButton[,0,0,0x0,invalid...". So, that's why you get that long text on JList items.

One way of solving this problem is by writing your own class extending JButton and overriding toString() in your class. Like in below example.

NOTE:

If you are expecting JButton functionality (e.g. clicking the button) from your list items, then you will have to do more than this. You will have to write a custom cell renderer and use it in your JList.

import javax.swing.*;

public class ButtonList
{
  public static void main(String[] args)
  {
    CustomButton test = new CustomButton("test");
    CustomButton b2 = new CustomButton("button 2");
    CustomButton b3 = new CustomButton("button 3");
    DefaultListModel<CustomButton> model = new DefaultListModel<>();
    model.addElement(test);
    model.addElement(b2);
    model.addElement(b3);

    JList<CustomButton> emailList = new JList<>(model);

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(new JScrollPane(emailList));
    frame.pack();
    frame.setLocation(300, 300);
    frame.setVisible(true);
  }
}

class CustomButton extends JButton
{
  CustomButton(String text)
  {
    super(text);
  }

  @Override
  public String toString()
  {
    return getText();
  }
}

Output:

enter image description here

Prasad Karunagoda
  • 2,048
  • 2
  • 12
  • 16
  • I would argue for [composition](https://stackoverflow.com/questions/49002/prefer-composition-over-inheritance) here. We're not making a *new type* of button; we're making a new way of looking at ordinary everyday buttons. – Silvio Mayolo Mar 18 '19 at 18:18
  • Thank you, I will try it in a bit – user10990200 Mar 18 '19 at 18:42