ListCellRenderer not Firing Events on Child Components - java

The following ListCellRenderer does not receive click events on the nested ComboBoxes. Do I need to enable something?
class FilterCellRenderer implements ListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
Filter filter = (Filter)value;
JPanel filterPanel = new JPanel();
FlowLayout layout = new FlowLayout();
layout.setAlignment(FlowLayout.LEFT);
filterPanel.setLayout(layout);
filterPanel.add(new JLabel(filter.getLabel()));
final List<Object> options = filter.getOptions();
if (options.size() > 1) {
JComboBox optionCombo = new JComboBox(new AbstractComboBoxModel() {
public int getSize() {
return options.size();
}
public Object getElementAt(int index) {
return options.get(index);
}
});
optionCombo.setSelectedItem(filter.getValue());
filterPanel.add(optionCombo);
}
if (isSelected) {
filterPanel.setBackground(list.getSelectionBackground());
filterPanel.setForeground(list.getSelectionForeground());
}
return filterPanel;
}
}

Renderer components in swing work like "rubber stamps" -they are just used to render/paint a value and are not added to the parent container in the usual way (just think how a single component could be added in multiple places!).
It sounds like you may want an editor rather than a renderer (an editor is a fully-fledged component, added in one place at any given time). Failing that you will have to install the MouseListener on the JList instead.

Since I didn't need to select rows, I ended up just dynamically adding and elements to a JPanel with a custom layout. Allowed for full component behaviour without having to hack a table.

It's a little bit tricky this. I believe you need to replace the JList with a single column JTable. Then set a table cell editor as well as renderer. IIRC, there might be a problem losing the first click (which gets used to select that cell edited).
Also it's a very good idea to reuse the components between each call to getCellRendererComponent. The components are used as a stamp and then discarded. Performance will suck massively if they are recreated each time.

Related

How to update tree node size with custom model?

I am using custom model in my JTree. When the label of some node changes, I am passing TreeModelEvent to the listeners in the following way:
protected void fireNodeChanged(TreePath path) {
TreeModelEvent evt = new TreeModelEvent(this, path);
for(TreeModelListener listener : listeners) {
listener.treeNodesChanged(evt);
}
}
where path is the path to changed node, including itself.
This causes the title change, but the size of it does not:
Swing truncates the title and adds ellipsis (pointed with red arrow).
How to fix this? Note that I have custom model and can't call nodeChanged, I should emulate it.
Without a snippet there are several guesses to make, but this situation seems familiar to me.
Try adding a renderer to your JTree that performs an updateUI() on the rendering component before returning it:
tree.setCellRenderer(new DefaultTreeRenderer() {
public Component getTreeCellRendererComponent(JTree tree, Object value,
boolean selected, boolean expanded, boolean leaf, int row,
boolean hasFocus) {
Component c = super.getTreeCellRendererComponent(tree, value,
selected, expanded, leaf, row, hasFocus);
// You can do more changes here
((JComponent) c).updateUI();
return c;
}});
About the updating, if "emulating" means implementing it and does what's supposed to do, that's correct. As long as it implements TreeModel and the methods do what's supposed, JTree should do the rest.
The truncation might be related to the fact that trees and tables do not lay their "components" out but use renderers instead to paint them in faked containers.
And, by the way, you can have a look at EventListenerList for a proper collection of event listeners.
Have you implemented addTreeModelListener(TreeModelListener l) in your custom TreeModel implementation? You'll want to call treeNodesChanged(TreeModelEvent e) on any registered listeners when your nodes are modified. Take a look at the source code for DefaultTreeModel for an example (the notification propagates up through the node's parents). Or, just have your custom model extend DefaultTreeModel and save yourself some time re-implementing all the listener stuff.

SwingX JXTable: use ColorHighlighter to color rows based on a "row object"

I'm using JXTable and I know how to do this based on DefaultRenderers for JTable, but I want to know how to do it in a way that's JXTable-friendly based on HighlighterPipeline.
I have a list of objects displayed in a table, and each row represents one object. I would like to color the rows displaying objects of a certain type a different color.
It looks like I should be using ColorHighlighter. But I can't find examples for this, other than the simple highlighters like "color every other row" or some such thing.
I need the row number since there's no such thing as a "row object" in the JTable/TableModel paradigm, but if I can do that, I can easily test a predicate and return true/false to tell the highlighter to kick in or not.
Can someone help me figure out the right direction to get this to work?
never mind, I figured it out. It was just hard to figure out the way to use ComponentAdapter propertly.
JXTable table = ...
final List<Item> itemList = ...
final HighlightPredicate myPredicate = new HighlightPredicate() {
#Override
public boolean isHighlighted(
Component renderer,
ComponentAdapter adapter) {
Item item = itemList.get(adapter.row);
return testItem(item);
}
public boolean testItem(Item item) { ... }
}
ColorHighlighter highlighter = new ColorHighlighter(
myPredicate,
Color.RED, // background color
null); // no change in foreground color
table.addHighlighter(highlighter);

JLists with a set of objects

i am making a twitter client (desktop application) in Java, i am using twitter4j API also. i have managed to do the search for tweets and i get back results and i show them in a Jlist.
what i want is that i want to show tweets nicely in the list, not only as a text .. show the image of the user, the tweet, tweeted by ... etc all this information .. in addition attach additional data like star rating .. how can i add that to a JList ? can the Jlist hold different objects .. Jpanels for example ..
Instead I suggest you put a set of JPanels inside a JScrollPane.
A JList's renderer must be a JComponent, so you can use any Swing object, including JPanels.
You can also use HTML in a JLabel if it is easier to do so than using a JPanel.
To use a custom renderer, you do something like this..
myList.setCellRenderer(new CustomRenderer());
and then create a renderer like this
public class CustomRenderer extends DefaultListCellRenderer {
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus) {
JPanel panel = new JPanel();
// set up the panel for your exact display requirements.
return(panel);
}
}
Suggest using a JTable, which has several columns, instead of a JList.
Also suggest using GlazedLists, which makes it really easy to display lists with fields in a JTable, so that they update automatically when the underlying list changes.
Here's an example of some code I wrote recently which displays something similar:
private void bindEmailTargetTable(NotificationModel newModel) {
JTable table = getUI(UIKey.EMAIL_TARGET_TABLE);
EventList<EmailTarget> displayList = newModel.getEmailTargets();
TableFormat<EmailTarget> tf = new TableFormat<EmailTarget>()
{
#Override public int getColumnCount() {
return 4;
}
private final String[] columns = { "address", "description", "msg left", "msg limit" };
#Override public String getColumnName(int col) {
return this.columns[col];
}
#Override public Object getColumnValue(EmailTarget item, int col) {
switch (col)
{
case 0:
return item.getAddress();
case 1:
return item.getDescription();
case 2:
return item.getRemainingMessages();
case 3:
return item.getMessageLimit();
default:
return "";
}
}
};
EventTableModel<EmailTarget> etm = new EventTableModel<EmailTarget>(displayList, tf);
table.setModel(etm);
}
That's 33 lines of code to take a JTable and make it automatically update itself to display 4 fields of each EmailTarget in an EventList<EmailTarget>.
For non-text field contents, you just need a custom TableCellRenderer.
AS Jason suggested its better to go for jtable instead of JLIst. In fact you can use any free Java based table classes that have extended functionality over JTables. JIDE is one such library but its commercial. you can search and find a lot..

Fast replacement for JComboBox / BasicComboBoxUI?

I've got a JComboBox that potentially can have thousands of items. They're sorted, and there's find-as-you-type, so in principle it's not completely unusable.
In practice, it's pretty unusable with just a couple of hundred items. I managed to improve the initial display performance using setPrototypeDisplayValue(), but BasicListUI still insists on configuring the list cell renderer for every item in the box (see BasicListUI.updateLayoutState()).
This, or something like it, is apparently a known issue to Sun; it has been for going on eight years now, so I'm not holding my breath.
Short of implementing my own UI, has anyone got a workaround?
JList might be a better choice, as it uses a fly-weight approach to rendering and appears to support find-as-you-type.
If you use JComboBox, add entries to the model before the component itself starts listening. This SortedComboBoxModel uses a simple insertion sort that is acceptable for a few thousand entries:
class SortedComboBoxModel extends DefaultComboBoxModel {
/** Add elements by inserting in lexical order. */
#Override
public void addElement(Object element) {
this.insertElementAt(element, 0);
}
/** Insert in lexical order by name; ignore index. */
#Override
public void insertElementAt(Object element, int index) {
String name = element.toString();
for (index = 0; index < this.getSize(); index++) {
String s = getElementAt(index).toString();
if (s.compareTo(name) > 0) {
break;
}
}
super.insertElementAt(element, index);
}
}
Here's the hack that I came up with. The drawbacks are:
if you want to maintain the look and feel, you have to separately subclass each BasicComboBoxUI extension you care about
you have to use reflection to load your UI classes, since (for instance) a subclass of WindowsComboBoxUI won't load on Linux
it won't work with L&Fs (e.g. MacOS?) that don't extend BasicComboBoxUI
it makes assumptions about the ListCellRenderer that may not always be warranted
I'm still open to cleaner solutions.
class FastBasicComboBoxUI extends BasicComboBoxUI {
#Override
public void installUI(JComponent c) {
super.installUI(c);
Object prototypeValue = this.comboBox.getPrototypeDisplayValue();
if (prototypeValue != null) {
ListCellRenderer renderer = comboBox.getRenderer();
Component rendererComponent = renderer
.getListCellRendererComponent(this.listBox,
prototypeValue, 0, false, false);
if (rendererComponent instanceof JLabel) {
// Preferred size of the renderer itself is (-1,-1) at this point,
// so we need this hack
Dimension prototypeSize = new JLabel(((JLabel) rendererComponent)
.getText()).getPreferredSize();
this.listBox.setFixedCellHeight(prototypeSize.height);
this.listBox.setFixedCellWidth(prototypeSize.width);
}
}
}
}
I'm still open to cleaner solutions.
Later
Turns out this only solved some of the problems. Initial display of a combo box with a large number of items could still be really slow. I had to make sure the popup list box immediately gets a fixed cell size, by moving the code into the ComboPopup itself, as follows. Note that, as above, this depends on the prototype value.
#Override
protected ComboPopup createPopup() {
return new BasicComboPopup(comboBox) {
#Override
protected JList createList() {
JList list = super.createList();
Object prototypeValue = comboBox.getPrototypeDisplayValue();
if (prototypeValue != null) {
ListCellRenderer renderer = comboBox.getRenderer();
Component rendererComponent = renderer
.getListCellRendererComponent(list, prototypeValue, 0, false, false);
if (rendererComponent instanceof JLabel) {
// Preferred size of the renderer itself is (-1,-1) at this point,
// so we need this hack
Dimension prototypeSize = new JLabel(((JLabel) rendererComponent)
.getText()).getPreferredSize();
list.setFixedCellHeight(prototypeSize.height);
list.setFixedCellWidth(prototypeSize.width);
}
}
return list;
}
};
}

Java Swing: How to bind a JLabel's text to a column in the selected row of a JTable?

I am using Netbeans and am trying to find a way for the IDE to auto-generate the code for me. I remember binding a JLabel's text to a column in the selected row of the JTable before, but in that case, the JTable's values were from an entity manager, and it was very easy. I was wondering if there is a way to do it even if the JTable is not tied to a database.
Also, how else could one do it? I was thinking of implementing a ListSelectionListener, and whenever an event got generated, just update the text of the label.
I think your second solution is best way to do it, something like this:
public class LabelSyncer implements ListSelectionListener {
private JLabel toSync;
private int columnIndex;
public LabelSyncer(JLabel toSync, int columnIndex) {
}
public void valueChanged(ListSelectionEvent e) {
JTable table = (JTable) e.getSource();
int row = table.getSelectedRow();
toSync.setText(table.getModel().getValueAt(row, columnIndex).toString());
}
}
and then
table.getSelectionModel().addListSelectionListener(new LabelSyncer(label, columnIndex));
Something like this. Probably a more generic solution, but this should work.

Categories

Resources