How to do an action when a LWUIT List item clicked - java-me

I have a LWUIT application that has a list which involving some items.
The list itself has been added to a Combobox .
1/ How I change the colour of an item of list when I focus on it?
final com.sun.lwuit.List mylist = new com.sun.lwuit.List();
mylist.addItem("one");
mylist.addItem("two");
mylist.addItem("three");
mylist.addItem("four");
final com.sun.lwuit.ComboBox combo = new com.sun.lwuit.ComboBox (mylist.getModel());
final com.sun.lwuit.Form ff = new com.sun.lwuit.Form();
ff.addComponent(combo);
2/ I want to do an action when I click ( or double click ) on an item ,
ActionListener interface didn't make that for me , can someone guide me?
mylist.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ev)
{
System.out.println("java");
}
}
);

You Should set a renderer to ComboBox and can use both of setRenderer and setListCellRenderer but setListCellRenderer is
deprecated than use setRenderer:
combo.setRenderer(new ListCellRenderer() {
public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
Label l = new Label(String.valueOf(value));
l.getStyle().setBgColor(0xffaa00);
return l;
}
public Component getListFocusComponent(List list) {
Label l = new Label(String.valueOf(list.getSelectedItem()));
l.getStyle().setBgColor(0x00ff00);
return l;
}
});
this working well.

To change the colour of a ComboBox you should modify the ComboBoxFocusstyle from the ResourceEditor.
If you are adding the list to the ComboBox, I think that you should put the ActionListener to the ComboBox not to the List as you are doing. Try this facts.

You can work with ListCellRenderer. Its helpful tool ,
look here for example
You can implement getListCellRendererComponent(..)- this function return the compenents that display on screen and responsible on UI.
If you work with ListCellRenderer you can use actionLisiner like this:
mylist.setRenderer(getListCellRenderer());
ActionListener chooseItemActionListener = new ActionListener() {
public void actionPerformed(ActionEvent evt) {
doAction(getSelected());
}
};
mylist.addActionListener(chooseItemActionListener);

Related

LWUIT Custom expandable component in list item

In my project I use lwuit List(main), with a custom renderer.
I`m following the purpose of implementing the expandable list item, that expands/collapses another List (wrapped).
Initially, I'd created Container with Button and List (without collapse/expand behaviour), but when I used it in main list, I faced problem with inability to select Items in wrapped list.
So, I will happy if you helps me with two problems:
Can I fix it using standard lwuit tools?
How can I hide my wrapped list by clicking HeaderBar (visibility just hides the content, but leaves a big gap)?
Images for clarity (ListItem - item of main List, which does not display on image):
private void fillForm() {
mF = new Form();
fillList1();
fillList2();
fillList();
mF.show();
}
private void fillList() {
mList = new CList();
mList.setRenderer(new CRenderer());
mList.addItem(c1);
mList.addItem(c2);
mF.addComponent(c1);
mF.addComponent(c2);
}
private void fillList1() {
c1 = new Container();
b1 = new Button();
b1.getUnselectedStyle().setBorder(Border.createLineBorder(2, 0x000000));
b1.addActionListener(this);
mList1 = new List();
mList1.setName("l1");
mList1.setRenderer(new DefaultListCellRenderer());
mList1.addItem("one");
mList1.addItem("two");
mList1.addItem("three");
mList1.addItem("four");
mList1.addItem("five");
c1.addComponent(b1);
c1.addComponent(mList1);
}
private void fillList2() {
c2 = new Container();
b2 = new Button();
b2.getUnselectedStyle().setBorder(Border.createLineBorder(2, 0x000000));
b2.addActionListener(this);
mList2 = new List();
mList2.setName("l2");
mList2.setRenderer(new DefaultListCellRenderer());
mList2.addItem("путин");
mList2.addItem("ест");
mList2.addItem("детей");
mList2.addItem("больше чем");
mList2.addItem("любит родину");
c2.addComponent(b2);
c2.addComponent(mList2);
}
private class CRenderer extends DefaultListCellRenderer {
public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
return (Container)value; //To change body of generated methods, choose Tools | Templates.
}
}
Can this component solve your issue?
PopupChoiceGroup

GXT 3.x EditorGrid: choose cell editor type on a cell by cell basis

Is there anyway to define the editor type on a cell by cell basis in GXT 3.0?
I need to create a transposed table; the column become the row and the row is the column. That being the case, a column (from a normal table point of view) will have various editor type, whereby a row will have identical editor type.
I am trying to use following approach - It seems to be working fine, and allow to open up editors based on data type but when i click out; it doesn't close/hide editor.
I would really appreciate if someone can please point me in right direction.
final GridInlineEditing<MyModel> editing = new GridInlineEditing<MyModel>(mygrid){
#SuppressWarnings("unchecked")
#Override public <O> Field<O> getEditor(ColumnConfig<MyModel, ?> columnConfig) {
if(valueColumnName.equals(columnConfig.getHeader().asString())) {
MyModel myModel = tree.getSelectionModel().getSelectedItem();
if(MyModelType.STRING.equals(myModel.getMyModelType())) {
TextField textField = new TextField();
textField.setAllowBlank(Boolean.FALSE);
return (Field<O>) textField;
}
else {
TextArea textField = new TextArea();
textField.setAllowBlank(Boolean.FALSE);
return (Field<O>) textField;
}
}
return super.getEditor(columnConfig);
}
};
editing.setClicksToEdit(ClicksToEdit.TWO);
PS:
This is similar to question below; but answer is specific to post GXT 3.0. I am new to stackoverflow and it seems recommendation was to create new question instead of adding new post to old thread.
GXT EditorGrid: choose cell editor type on a cell by cell basis
After playing around all day; my colleague(Praveen) and I figured it out. So instead of trying to override GridInlineEditing's getEditor() method override startEditing() method. Also, you will need converters if you have data like Date, List etc. Below is sample code; hope this help others.
final GridInlineEditing<MyModel> editing = new GridInlineEditing<MyModel>(tree){
#Override public void startEditing(GridCell cell) {
MyModel myModel= tree.getSelectionModel().getSelectedItem();
if(MyModelType.TEXT.equals(myModel.getContextVariableType())) {
TextArea textField = new TextArea();
textField.setAllowBlank(Boolean.FALSE);
super.addEditor(valueColumn, textField);
}
else if(MyModelType.BOOLEAN.equals(myModel.getContextVariableType())) {
SimpleComboBox<String> simpleComboBox = new SimpleComboBox<String>(new StringLabelProvider<String>());
simpleComboBox.setTriggerAction(TriggerAction.ALL);
simpleComboBox.add("YES");
simpleComboBox.add("NO");
super.addEditor(valueColumn, simpleComboBox);
}
else if(MyModel.INTEGER.equals(myModel.getContextVariableType())) {
SpinnerField<Integer> spinnerField = new SpinnerField<Integer>(new IntegerPropertyEditor());
spinnerField.setIncrement(1);
Converter<String, Integer> converter = new Converter<String, Integer>(){
#Override public String convertFieldValue(Integer object) {
String value = "";
if(object != null) {
value = object.toString();
}
return value;
}
#Override public Integer convertModelValue(String object) {
Integer value = 0;
if(object != null && object.trim().length() > 0) {
value = Integer.parseInt(object);
}
return value;
}
};
super.addEditor(valueColumn, converter, (Field)spinnerField);
}
else {
TextField textField = new TextField();
textField.setAllowBlank(Boolean.FALSE);
super.addEditor(valueColumn, textField);
}
super.startEditing(cell);
}
};
editing.setClicksToEdit(ClicksToEdit.TWO);
I think the reason you are not seeing the fields not closing is because you are not actually adding them to the GridInlineEditing class.
In the parts where you have the following return statements;
return (Field<O>) textField;
Those textfields are never added to the grid.
I would try substituting the following code for your first two return statement;
super.addEditor(columnConfig, (Field<O>) textField;
This adds the editor to some maps used by AbstractGridEditing. Specifically, the AbstractGridEditing.removeEditor(GridCell, Field<?>) method, which is used in GridInlineEditing.doCompleteEditing() and GridInlineEditing.cancelEditing() needs the field to be in the map so it can be detached from its parent.

How to remove the Title-bar of a LWUIT Form?

In the actionPerformed of a Button I want to remove the Title-bar of the actual LWUIT Form. How to achieve that? And how to redisplay it again after a certain action has been complete?
Use below code for hide/show the title of the Form in the Button action event,
final Form form = new Form("Sample");
form.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
final Container titleContainer = form.getTitleArea();
titleContainer.setVisible(false);
Button b = new Button("button");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
if (!titleContainer.isVisible()) {
titleContainer.setVisible(true);
} else {
titleContainer.setVisible(false);
}
form.revalidate();
}
});
form.addComponent(b);
form.show();
You could also just do
form.getTitleArea().setVisible(false);

How to add a new item into ObjectListView?

I tried the demo code in demo project but I can't add new item successfully.
It just add new new NULL group and NULL item.
Please give me an simple example code to add new item (text and image).
Thank you!
Oh sorry! I forgot it. This is the first time I participate in this site.
I use C#. And the code is:
objectListView1.BeginUpdate();
objectListView1.AddObject(new string [] {"Hello","dfdsF" });
objectListView1.EndUpdate();
and
objectListView1.BeginUpdate();
OLVListItem item = new OLVListItem(new string [] {"Hello","dfdsF" });
objectListView1.Items.Add(item);
objectListView1.EndUpdate();
It's so different form ListView and EXListView which I can define a text or a image when creating new item. But in ObjectListView, I don't understand OBJECT?
I get ObjectListView anh it's demo code form here http://nchc.dl.sourceforge.net/project/objectlistview/objectlistview/v2.5/ObjectListViewFull-2.5.0.zip
I will show you what to do to add items. Try to create a class, then make getters and setters for the properties you want to show on your ObjectListView.
SetObjects method takes a List<T>:
public Form1()
{
InitializeComponent();
this.objectListView1.SetObjects(haha.GET());
}
Now this is my class, I called it haha, I've two properties in it (Name and Detail):
class haha
{
string name;
string detail;
public haha(string name , string detail)
{
this.name = name;
this.detail = detail;
}
public string Name
{
get { return name; }
set { name = value; }
}
public string Detail
{
get { return detail; }
set { detail = value; }
}
static internal List<haha> GET()
{
haha item = new haha("zeko", "dunno");
haha xx = new haha("sheshe", "dunno");
haha ww = new haha("murhaf", "dunno");
haha qq = new haha("soz", "dunno");
haha ee = new haha("HELLO", "dunno");
List<haha> x = new List<haha>();
x.Add(item);
x.Add(xx);
x.Add(ww);
x.Add(qq);
x.Add(ee);
return x;
}
}
Now
change ShowGroups in ObjectListView to false
then add the columns that you want; I've added two columns, one for Name and one for Detail
and as in the picture when you add a column, see the AspectName and write exactly the same name of its property that you want to show from your class
Here's the result:
If you want to use AddObject(), which takes an object, I'd write this:
private void button1_Click(object sender, EventArgs e)
{
haha newObject = new haha("memo","zezo");
objectListView1.AddObject(newObject);
}
Happy coding :)
The best thing is to use an entity class. Then make a list of items and add this list to your ObjectListView.
myObjectListView.SetObjects(myListofEntityItems);
But before you do that, you have to setup the columns in your designer. Just add a column, and in the field AspectName enter the exact name of the attribute of your entity item.

How to ticker List elements when they are Commands?

If I create a List based on an array of Commands, and the text of some Commands are not entirely shown in the List, although the List preferredWidth is set to the Form preferredWidth, how to ticker them ?
Thank you very much
Add the below class in your midlet class or create a new class file for that:
class TickerRenderer extends DefaultListCellRenderer {
private DefaultListCellRenderer selectedRenderer = new DefaultListCellRenderer(false);
private List parentList;
public TickerRenderer() {
super(false);
}
public boolean animate() {
if (parentList != null && parentList.getComponentForm() != null) {
if (selectedRenderer.isTickerRunning()) {
if (selectedRenderer.animate()) {
parentList.repaint();
}
}
}
return super.animate();
}
public Component getListCellRendererComponent(List list, Object value, int index, boolean isSelected) {
if (isSelected) {
selectedRenderer.getListCellRendererComponent(list, value, index, isSelected);
// sometimes the list asks for a dummy selected value for size calculations and this might
// break the tickering state
if (index == list.getSelectedIndex()) {
if (selectedRenderer.shouldTickerStart()) {
if (!selectedRenderer.isTickerRunning()) {
parentList = list;
list.getComponentForm().registerAnimated(this);
selectedRenderer.startTicker(UIManager.getInstance().getLookAndFeel().getTickerSpeed(), true);
}
} else {
if (selectedRenderer.isTickerRunning()) {
selectedRenderer.stopTicker();
}
}
}
return selectedRenderer;
} else {
return super.getListCellRendererComponent(list, value, index, isSelected);
}
}
}
Use it like this:
List cmdList = new List(cmds);
cmdList.setListCellRenderer(new TickerRenderer());
Try this code, it will show list in dialog box on clicking "Show list" command and will also enable ticker initially. Below is the code which shows how to use the above mentioned class to see ticker in list when list is contained in dialog.
Don't forget to make your list final so that it can be used in inner classes.
form.addCommand(new Command("Show list") { // add command in form and override its actionPerformed method
public void actionPerformed(ActionEvent evt) {
Dialog d = new Dialog() { // create an instance of dialog and make it an inner class so that you can override onShow() method and set focus on list when dialog gets initialized and also can set its index to ur preferred one (here it's 0)
protected void onShow() { // overriding of onShow() method
list.requestFocus(); // set focus on list
list.setSelectedIndex(0); // set selected index to 0
}
};
d.addComponent(list); // add list in dialog
d.show(); // show dialog
}
});
This code shows my list in dialog and starts ticker initially. If it doesn't help, post your code, i will try to see it.

Resources