keertip wrote:Could you post the code for the list and cell renderer? That would help to reproduce the problem at our end and see what we could do to help.
Here is a test case. I was wrong in my last post: the test works OK if I replace the toString()-result in JUnit with correct value that is visible in JList. Is there a ways to make this behavior a default one in code generation?
- Code: Select all
import java.awt.Component;
import java.util.Locale;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
public class JListRendererDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("JList Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
DataItem[] items = {
new DataItem("EN 1", "DE 1"),
new DataItem("EN 2", "DE 2"),
new DataItem("EN 3", "DE 3"),
new DataItem("EN 4", "DE 4")
};
JList list = new JList(items);
list.setName("JList1");
list.setCellRenderer(new JListCellRenderer(Locale.ENGLISH));
//list.setCellRenderer(new JListCellRenderer(Locale.GERMAN));
frame.add(new JScrollPane(list));
frame.pack();
frame.setVisible(true);
}
/**
* Renderer for JList
*/
private static class JListCellRenderer extends DefaultListCellRenderer {
private Locale userLocale;
public JListCellRenderer(Locale userLocale) {
this.userLocale = userLocale;
}
@Override
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
JLabel renderer = (JLabel) super.getListCellRendererComponent(list,
value,
index,
isSelected,
cellHasFocus);
if(value != null) {
DataItem dataItem = (DataItem) value;
if(userLocale.equals(Locale.ENGLISH)) {
renderer.setText(dataItem.getNameEN());
}
else if(userLocale.equals(Locale.GERMAN)) {
renderer.setText(dataItem.getNameDE());
}
}
return renderer;
}
}
/**
* Item to show in JList
*/
private static class DataItem {
private String nameEN;
private String nameDE;
public DataItem(String nameEN, String nameDE) {
this.nameEN = nameEN;
this.nameDE = nameDE;
}
public String getNameEN() {
return nameEN;
}
public String getNameDE() {
return nameDE;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("DataItem [nameEN=");
builder.append(nameEN);
builder.append(", nameDE=");
builder.append(nameDE);
builder.append("]");
return builder.toString();
}
}
}
Original WindowTester Pro generated JUnit test with toString() values:
- Code: Select all
import com.windowtester.runtime.swing.locator.JListLocator;
import com.windowtester.runtime.swing.locator.NamedWidgetLocator;
import com.windowtester.runtime.swing.UITestCaseSwing;
import com.windowtester.runtime.IUIContext;
import com.windowtester.runtime.swing.condition.WindowDisposedCondition;
public class JListRendererDemoTest1 extends UITestCaseSwing {
/**
* Create an Instance
*/
public JListRendererDemoTest1() {
super(JListRendererDemo.class);
}
/**
* Main test method.
*/
public void testJListRendererDemoTest1() throws Exception {
IUIContext ui = getUI();
ui.click(new JListLocator("DataItem [nameEN=EN 1, nameDE=DE 1]",
new NamedWidgetLocator("JList1")));
ui.click(new JListLocator("DataItem [nameEN=EN 2, nameDE=DE 2]",
new NamedWidgetLocator("JList1")));
ui.click(new JListLocator("DataItem [nameEN=EN 3, nameDE=DE 3]",
new NamedWidgetLocator("JList1")));
ui.click(new JListLocator("DataItem [nameEN=EN 4, nameDE=DE 4]",
new NamedWidgetLocator("JList1")));
ui.wait(new WindowDisposedCondition("JList Test"));
}
}
Modified JUnit test with visible JList values:
- Code: Select all
import com.windowtester.runtime.swing.locator.JListLocator;
import com.windowtester.runtime.swing.locator.NamedWidgetLocator;
import com.windowtester.runtime.swing.UITestCaseSwing;
import com.windowtester.runtime.IUIContext;
import com.windowtester.runtime.swing.condition.WindowDisposedCondition;
public class JListRendererDemoTest2 extends UITestCaseSwing {
/**
* Create an Instance
*/
public JListRendererDemoTest2() {
super(JListRendererDemo.class);
}
/**
* Main test method.
*/
public void testJListRendererDemoTest2() throws Exception {
IUIContext ui = getUI();
ui.click(new JListLocator("EN 1",
new NamedWidgetLocator("JList1")));
ui.click(new JListLocator("EN 2",
new NamedWidgetLocator("JList1")));
ui.click(new JListLocator("EN 3",
new NamedWidgetLocator("JList1")));
ui.click(new JListLocator("EN 4",
new NamedWidgetLocator("JList1")));
ui.wait(new WindowDisposedCondition("JList Test"));
}
}