You should post compilable code only, and your code is a bit confusing. You shouldn't pass text into the renderer's constructor as this one constructor will be used for the single renderer that renders all items in the list (unless you want all to use the same code). You shouldn't ignore the Object parameter that is passed into your getListCellRendererComponent method, for this is the data that the renderer item displays.
For example.
Up vote 1 down vote favorite share g+ share fb share tw.
I am using a JList, and I'm trying to use JTextAreas (that implement ListCellRenderer) for the cells. It isn't working. The cells simply display the ListCellRenderer.toString() instead of the actual JTextArea.
Could someone help? Thanks. DefaultListModel listModel = new DefaultListModel(); JList list = new JList(listModel); add(list); class ButtonListener implements ActionListener() { public void actionPerformed(ActionEvent e){ listModel.clear(); for (String s : stringArray) { listModel.
AddElement(new Listm(s)); } } } class Listm extends JTextArea implements ListCellRenderer { protected Listm(String text) { setText(text); //Outputting the text element displays the desired String } public Component getListCellRendererComponent(JList list, Object object, int number, boolean bool, boolean bool2) { setPreferredSize(new Dimension(x, y)); return this; } } } java swing jlist jtextarea link|improve this question asked Jul 23 '11 at 18:07farm ostrich4911317 90% accept rate.
1 For better help sooner, post an SSCCE. – Andrew Thompson Jul 23 '11 at 18:20 Since I don't know where the problem resides, I was more verbose. – farm ostrich Jul 23 '11 at 18:31 but regardless of verbosity, your code makes little sense.
Please post compilable code. – Hovercraft Full Of Eels Jul 23 '11 at 18:32 I can agree with that. – farm ostrich Jul 23 '11 at 18:34 "Since I don't know where the problem resides, I was more verbose.
" Either you did not read, or did not understand, the document on the SSCCE. Which is it? – Andrew Thompson Jul 23 '11 at 18:59.
You should post compilable code only, and your code is a bit confusing. You shouldn't pass text into the renderer's constructor as this one constructor will be used for the single renderer that renders all items in the list (unless you want all to use the same code). You shouldn't ignore the Object parameter that is passed into your getListCellRendererComponent method, for this is the data that the renderer item displays.
For example: import java.awt. *; import java.awt.event. *; import javax.swing.
*; public class MyGui extends JPanel { public static final String DATA = {"One\n1", "Two\n2", "Three\n3"}; private DefaultListModel listModel = new DefaultListModel(); private JList list = new JList(listModel); public MyGui() { list. SetCellRenderer(new Listm(3, 30)); add(new JScrollPane(list)); for (String datum : DATA) { listModel. AddElement(datum); } } private class Listm extends JTextArea implements ListCellRenderer { protected Listm(int rows, int cols) { super(rows, cols); setBorder(BorderFactory.
CreateLineBorder(Color. Blue)); } public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { setText(value.toString()); if (cellHasFocus) { setBackground(FOCUSED_COLOR); } else if (isSelected) { setBackground(SELECTED_COLOR); } else { setBackground(null); } return this; } } private static void createAndShowUI() { JFrame frame = new JFrame("MyGui"); frame.getContentPane(). Add(new MyGui()); frame.
SetDefaultCloseOperation(JFrame. EXIT_ON_CLOSE); frame.pack(); frame. SetLocationRelativeTo(null); frame.
SetVisible(true); } public static void main(String args) { java.awt.EventQueue. InvokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }.
Very good. I don't remember which Qs, but you've helped me out before with some solid answers. Thank you.
– farm ostrich Jul 23 '11 at 18:44 @farm: you're welcome! – Hovercraft Full Of Eels Jul 23 '11 at 18:49.
Verify that you are invoking setCellRenderer(), which sets "the delegate that is used to paint each cell in the list.
You should use renderer. I do not have a code right now but it is pretty simple. The default renderer of JList calls toString() and displays the result.
Not your down-voter; I think you're right about applying the renderer. Although the use of toString() is an implementation detail, it is a good clue as to where things went awry. The use of toString() does not apply to values of type Icon, BTW.
– trashgod Jul 23 '11 at 18:46 toString() does not apply to a custom cell renderer unless the code specifies that it will use this. Otherwise the renderer can choose to use any property of the containing object that the coder so chooses (or none at all if he so chooses). – Hovercraft Full Of Eels Jul 23 '11 at 18:52.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.