Skip to main content

Working with LWUIT Table

10 replies [Last post]
gt8887b
Offline
Joined: 2010-03-15
Points: 0

Hello,

I am trying to use LWUIT table control and running into two problems.

Firstly, table completely ignores theme settings (TableCell.bgColor, etc.) It renders black text on white background and sometimes, when you select a header it apparently renders white text on white background (or may be it does not draw text at all). I stepped into the code and paint() method for Component seems to get the right style object with all the right values for colors. However, it still ends up drawing black-on-white at the end. Can't figure out how it is happening.

Secondly, I can't seem to figure out how to set column size. My table has two columns and I want to make sure it does not truncate text in headers. There is enough screen real-estate to fit both headers, but since some entries in my first column are long it always makes the first column very large. I saw from looking at the code that I can set a constraint as a percentage of total width, but I cannot find out the width until layout is performed.

Do you have any suggestions?

Thanks.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vprise
Offline
Joined: 2003-11-07
Points: 0

bgColor for cell's won't work without setting the transparency to something other than 0.
You need to use the column constraint to determine the width of the column in percentage. You can also disallow horizontal scrolling and stick the table into a CENTER border layout which will make sure it always fits the screen regardless.

gt8887b
Offline
Joined: 2010-03-15
Points: 0

Thanks vprise.

Unfortunately, it did not help at all. It still ignores style settings and sometimes even paints the selected element white-on-white or may be it just paints a white rectangle on top of selected cell element.

I even tried overwriting the "createCell" method but it did not help at all.

protected Component createCell(Object value, int row, int column, boolean editable) {

Label l = new Label(value.toString());

Style us = l.getUnselectedStyle();
Style se = l.getSelectedStyle();

if(row == -1) {
l.setUIID("TableHeader");
se.setBgColor(0x004576);
us.setBgColor(0x004576);

if (column == 1) {
int w = l.getPreferredW() * 3 / 2;
l.setPreferredW(w);
}
}
else {
l.setUIID("TableCell");
se.setBgColor(0xeeeeee);
us.setBgColor(0xeeeeee);
}

se.setBorder(Border.createLineBorder(1, 0x0AA245));
se.setBgTransparency(200);
us.setBgTransparency(200);

l.setAlignment(Label.CENTER);
l.setFocusable(true);

return l;
}

I am using BoxLayout(BoxLayout.Y_AXIS) so the table does occupy full width of the screen.

vprise
Offline
Joined: 2003-11-07
Points: 0

Styling the table works well in the LWUIT Demo, I suggest you try to compare styling in your application vs. the LWUIT demo.

sudip230784
Offline
Joined: 2010-09-15
Points: 0

hi Can you please help in deciding the width of a particular column.
And also How can i add a icon to the column.
Please help me ASAP.
Looking forward to hear from you.

Thanks,
Sudip

vprise
Offline
Joined: 2003-11-07
Points: 0

Use the table layout constraint value when adding elements to the table layout or override the constraint method for the Table.
To use images you need to override the tables cell creation method.

sudip230784
Offline
Joined: 2010-09-15
Points: 0

Hi vprise,
Thanks for your reply.
I am not using a table layout. Instead I am using a Table class directly.
So how can I change the width of column.

Also If you can help me with some small code snippets for this and also for creating cell
that will be very helpful.
Thanks in advance.

vprise
Offline
Joined: 2003-11-07
Points: 0

Use the addComponent(Object,Component) method and pass the TableLayout.constraint instance which you have modified.

sudip230784
Offline
Joined: 2010-09-15
Points: 0

Hi Thanks for the reply.
I am still confuse with the solution you told.
I am not using table layout, so how can i set the constraints.
In addition to this ,I am having a table with 2 columns , I want to set focus on both the columns , how can i achieve it?
Please help me out.

Thanks in advance.

vprise
Offline
Joined: 2003-11-07
Points: 0

Sorry I missed the word "NOT" ;-)

Override createCellConstraint and modify the constraint returned.

Override createCell() to mark a cell differently based on its status, you would need to attach a focus listener to all cells and essentially set their UIID to something different when a row is selected.

gt8887b
Offline
Joined: 2010-03-15
Points: 0

If it is any help, here is my code:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import com.sun.lwuit.Command;
import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.events.DataChangedListener;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.table.Table;
import com.sun.lwuit.table.TableModel;
import com.sun.lwuit.util.Resources;
import java.io.InputStream;
import javax.microedition.midlet.*;

/**
 * @author AG
 */
public class LwuitTable extends MIDlet implements ActionListener {

    private static class ReportTable implements TableModel {

        String [][] mTable = new String[][] {
            new String[] {"Dispatch Job 1 (1188 Bridle CT)", "3.5"},
            new String[] {"Dispatch Job 2 (3301 Main St)", "7.2"},
            new String[] {"Sanyo Test (unspecified)", "1.6"},
            new String[] {"Service Test 1", "5.9"}};


        public int getRowCount() {
            return mTable.length;
        }

        public int getColumnCount() {
            return 2;
        }

        public String getColumnName(int i) {
            if (i == 0) {
                return "Job";
            }
            else {
                return "Hours";
            }
        }

        public boolean isCellEditable(int row, int column) {
            return false;
        }

        public Object getValueAt(int row, int column) {
            return mTable[row][column];
        }

        public void setValueAt(int row, int column, Object o) {
        }

        public void addDataChangeListener(DataChangedListener d) {
        }

        public void removeDataChangeListener(DataChangedListener d) {
        }
    };

    Command mExitCmd = new Command("Exit");

    ReportTable mTable;

    public void startApp() {
        //init the LWUIT Display
        Display.init(this);
        // Setting the application theme is discussed
        // later in the theme chapter and the resources chapter
        try {
            InputStream is = LwuitTable.class.getResourceAsStream("/pointTrace.res");

            Resources r = Resources.open(is);
            UIManager.getInstance().setThemeProps(r.getTheme(
            r.getThemeResourceNames()[0])
            );
        } catch (java.io.IOException e) {
        }

        mTable = new ReportTable();

        Form form = new Form();

        Table tbl = new Table(mTable);

        form.addComponent(tbl);

        form.addCommand(mExitCmd);

        form.addCommandListener(this);

        form.show();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
    }

    public void actionPerformed(ActionEvent evt) {
        if (evt.getSource() == mExitCmd) {
            notifyDestroyed();
        }
    }
}