Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make one item in a JList bold?

Tags:

java

swing

jlist

I'm making enhancements to a Swing app (never done Swing programming before), and need to be able to make a single text item in a JList bold. I've seen a few posts where they said to just put "<html><b>" and "</b></html>" around the string. Are you serious, that seems like such a hack. It's also possible in the future that we'll want to change the background color of an item in the JList - would that also be possible with HTML tags?

Another suggestion I've seen is to call the setCellRenderer() method on the JList with your own object that implements the ListCellRenderer interface. But I'm not sure that can do what we want. It seems the ListCellRenderer has a getListCellRendererComponent() method where you set how an item will be displayed depending on whether it is selected or has the focus. But we want to have one item in the JList be bold depending on what our business logic determines, and it may be an item that is neither selected nor has the focus. I haven't seen any good examples of this ListCellRenderer, so I'm not sure if it's the approach we want.

like image 336
pacoverflow Avatar asked Jun 03 '11 07:06

pacoverflow


People also ask

How do I select an item in a JList?

Add an ActionListener to the button and override the actionPerformed method. Now every time the user presses the button this method will fire up. Call getSelectedIndex to get the index of the selected item in the JList . Call getSelectedValue method to get the value of the selected item in the JList .

How to add text to JList in Java?

String[] ar = {"one", "two", "three"}; JList list = new JList(ar); However, JList has no method to add or delete items once it is initialized. Instead, if you need to do this, you must use the ListModel class with a JList. Think of the ListModel as the object which holds the items and the JList as the displayer.

Which method of a JList returns if no item in the list is selected?

getAccessibleSelectionCount. Returns the number of items currently selected. If no items are selected, the return value will be 0.


1 Answers

The ListCellRenderer component also gets the object to be displayed and thus you can format based on whatever logic you have to. You can find the introduction to custom rendering here and an example of a renderer here (it sets the background based on dnd location but the idea is the same for other logic as well).

The following code gives you an idea of how it may be implemented.

class MyCellRenderer extends DefaultListCellRenderer {

    public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

        if ("TEST".equals(value)) {// <= put your logic here
            c.setFont(c.getFont().deriveFont(Font.BOLD));
        } else {
            c.setFont(c.getFont().deriveFont(Font.PLAIN));
        }
        return c;
    }
}
like image 156
Howard Avatar answered Oct 06 '22 07:10

Howard