ho già letto/provato questi posti, ma che non ha aiutato:swing JList con testo su più righe e l'altezza dinamica
- Display multiple lines within a Jlist cell
- How to get multiline for a Jlist text?
- Problem displaying components of JList
cosa ho bisogno è a ListCellRenderer
che restituisce un pannello con un'icona a sinistra e un testo di lunghezza dinamica a destra (come in qualsiasi forum: a sinistra un avatar utente, a destra t egli post il testo). I testi sono NOT noto, quindi non posso impostare un'altezza di cella fissa. Inoltre, la lunghezza del testo è diversa dalla cella elenco alla cella elenco. Quindi ogni cella di lista ha bisogno della sua altezza in base alla lunghezza del testo. In realtà un layout davvero comune ... ma non per Swing. L'altezza della cella non si espande in base alla lunghezza del testo.
Ho già letto quasi tutti i post relativi all'altezza delle celle dinamiche e ai testi multilinea in JList
, ma non è stato possibile trovare una soluzione. Così ho deciso di dare un piccolo SSCCE. Per favore, dammi un suggerimento su come ottenere ciò che ho descritto o per favore aggiusta il mio codice se pensi che sia facile.
Grazie
Ecco THS SSCCE:
public class MultiLineList extends JFrame
{
private static final long serialVersionUID = 1L;
public static void main(final String[] args)
{
new MultiLineList();
}
private MultiLineList()
{
setTitle("MultiLineList");
setSize(800, 450);
setResizable(true);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());
final DefaultListModel model = new DefaultListModel();
model.addElement("This is a short text");
model.addElement("This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. This is a long text. ");
model.addElement("This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. This is an even longer text. ");
final JList list = new JList(model);
list.setCellRenderer(new MyCellRenderer());
this.add(list);
this.getContentPane().invalidate();
this.getContentPane().validate();
}
public class MyCellRenderer extends DefaultListCellRenderer
{
@Override
public Component getListCellRendererComponent(final JList list, final Object value, final int index, final boolean isSelected, final boolean hasFocus)
{
final String text = (String) value;
//create panel
final JPanel p = new JPanel();
p.setLayout(new BorderLayout());
//icon
final JPanel IconPanel = new JPanel(new BorderLayout());
final JLabel l = new JLabel("icon"); //<-- this will be an icon instead of a text
IconPanel.add(l, BorderLayout.NORTH);
p.add(IconPanel, BorderLayout.WEST);
//text
final JTextArea ta = new JTextArea();
ta.setText(text);
ta.setLineWrap(true);
ta.setWrapStyleWord(true);
p.add(ta, BorderLayout.CENTER);
return p;
}
}
}
estraneo al problema in esame: a) neve r-ever _create_ components in getXXRendererComponent b) dont _extend_ classes se il nuovo non soddisfa l'is-a requisito – kleopatra
Si noti che un SSCCE dovrebbe includere le importazioni. Non tutti usiamo gli IDE automagic che possono capirli! –
Lo terrò a mente – haferblues