Skip to main content

AutoCompleteDecorator problem

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
7 replies [Last post]
lcristin
Offline
Joined: 2009-03-19

Hello,

I have a little problem with the use of auto complete decorator of swingX.

I found out a while ago that the renderer in my combo was only taken into account for the "possible values" displayed when you click on the combo, but not for the auto-completion, or for the selected value. (The selected value displayed in the combo is the value without processing of renderer, auto-completion works with values ​​without renderer's treatment too)

So I did some research and tried to associate a custom ObjectToStringConverter. For that, I outsourced the processing done in my renderer, and I am calling this code in the method getPreferredStringForItem of the ObjectToStringConverter.

This has solved part of the problem, the value selected appears properly now, but that does not solve the problem of auto-completion, the only texts accepted is still the value without renderer's treatment.

Note that I use an old swingX lib (swingx-2006_10_08.jar), perhaps this is a known bug that has since been resolved, but I have not found information about it. (And since this is a huge project, I am not at liberty to modify a library)

A part of the code:

------------------------------------------------------------------------------------------------------------------------------------------------------------

comboValue = new JComboBox(((MSearchComboValue)model).getComboModelValue());

comboValue.setRenderer(createRendererValue());

comboValue.setEnabled(false);

comboValue.setPreferredSize(new Dimension(0, comboValue.getPreferredSize().height));

AutoCompleteDecorator.decorate(comboValue, new ObjectToStringConverter() {

public String[] getPossibleStringsForItem(Object arg0) {

String preferred = getPreferredStringForItem(arg0);

return preferred == null ? new String[0] : new String[] { preferred };

}

public String getPreferredStringForItem(Object item) {

String result = null;

try {

result = VSearchComboValue.this.getText(item); // Traitement fait dans le renderer externalisé

} catch(Exception ex) {

ex.printStackTrace();

}

return result;

}

});

------------------------------------------------------------------------------------------------------------------------------------------------------------

I found that auto-completion is using the getPreferredStringsForItem method, but the value passed as parameter (arg0) is null if it does not correspond to one of the items in the combo model without renderer's treatment.

If anyone has an idea on how to solve the problem, it would be greatly helpful to me.

Thank you!

Laurent.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
kschaefe
Offline
Joined: 2006-06-08

Can you provide a small runnable demonstration?

Karl

lcristin
Offline
Joined: 2009-03-19

Hmm i've tried to reproduce the problem in a runnable and it is working juste fine :s

Sorry for the disturbance, seems that it's coming from my code. (even if i don't see what i've done wrong for now, i'll look more into it)

kschaefe
Offline
Joined: 2006-06-08

That's why we always ask for a small, runnable demo. ;) If you do find something, just let us know.

Karl

lcristin
Offline
Joined: 2009-03-19

Ok so i've found out what is causing the bug... It's the subtance look & feel:

CODE:

SubstanceLookAndFeel.setCurrentTheme("org.jvnet.substance.theme.SubstanceBrownTheme");

UIManager.setLookAndFeel(new SubstanceLookAndFeel());

--------------------------------------------------------------------------------

If i comment the lign "setLookAndFeel" it's working normally, if i let the lign the auto-completion doesn't work on combo with renderer.

kschaefe
Offline
Joined: 2006-06-08

Can you post a small runnable demo? I am using Substance at work and can test it. I want to figure out if we need to make a change or Substance does.

Karl

lcristin
Offline
Joined: 2009-03-19

package com.demo;

import java.awt.BorderLayout;

import java.awt.Component;

import java.awt.Dimension;

import javax.swing.DefaultComboBoxModel;

import javax.swing.DefaultListCellRenderer;

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JList;

import javax.swing.JPanel;

import javax.swing.UIManager;

import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

import org.jdesktop.swingx.autocomplete.ObjectToStringConverter;

import org.jvnet.substance.SubstanceLookAndFeel;

public class DemoCombo {

public static int VALUE_TEST1 = 1;

public static int VALUE_TEST2 = 2;

public static int VALUE_TEST3 = 3;

private JPanel ui;

public String getStringAssociated(Object value) {

String result = " ";

if(value instanceof Integer) {

if(value.equals(VALUE_TEST1)) {

result = "Value test 1";

} else if(value.equals(VALUE_TEST2)) {

result = "Value 2";

} else if(value.equals(VALUE_TEST3)) {

result = "Test 3";

}

}

return result;

}

private void createUI() {

ui = new JPanel(new BorderLayout());

ui.setPreferredSize(new Dimension(100, 30));

final DefaultComboBoxModel model1 = new DefaultComboBoxModel();

model1.addElement(1);

model1.addElement(2);

model1.addElement(3);

JComboBox combo = new JComboBox(model1);

AutoCompleteDecorator.decorate(combo, new ObjectToStringConverter() {

public String getPreferredStringForItem(Object arg0) {

return getStringAssociated(arg0);

}

public String[] getPossibleStringsForItem(Object arg0) {

String preferred = getPreferredStringForItem(arg0);

return preferred == null ? new String[0] : new String[] { preferred };

}

});

combo.setRenderer(new DefaultListCellRenderer() {

public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus){

Component comp = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

String txt = getStringAssociated(value);

setText(txt);

return comp;

}

});

ui.add(combo, BorderLayout.CENTER);

}

public JPanel getUI() {

if(ui == null) {

createUI();

}

return ui;

}

public static void main(String[] args) {

try {

UIManager.setLookAndFeel(new SubstanceLookAndFeel());

JFrame frame = new JFrame();

frame.getContentPane().add(new DemoCombo().getUI());

frame.setSize(200, 80);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

} catch(Exception ex) {

ex.printStackTrace();

}

}

}

------------------------------------------------------------------------------------------------------------------------

Try to comment / decomment the setLookAndFeel lign. Substance.jar from 2007-02-06, it maybe the version.

kschaefe
Offline
Joined: 2006-06-08

I don't have any problems using your code with the latest versions of SwingX and Substance. If there was a bug on either end, it's no longer present.

Karl