Adding a table to a container

167 views
Skip to first unread message

Marko Vidalis

unread,
Sep 13, 2013, 4:11:29 PM9/13/13
to codenameone...@googlegroups.com
Hi,

I need to add a table to my GUI but i have no idea how to successfully do so. I attempted to do this by using a container and the TableLayout, however i found it complex and i had problems with reading the data from the tableLayout container.
Is it possible to do so using the DefaultTableModel and then setting the model with a 2D array for the data?  how would i actually place a table on the UI? because i haven't seen a table component which i can select and place on the form. 

Thanks in advance,

Marko



Shai Almog

unread,
Sep 14, 2013, 12:31:54 AM9/14/13
to codenameone...@googlegroups.com
There is a section of "Additional Components" right below the components. Its clickable and you should find Table and quite a few additions there.

Marko Vidalis

unread,
Sep 14, 2013, 1:47:47 AM9/14/13
to codenameone...@googlegroups.com
I think i'm going crazy, I searched under the additional components section so many times and i never saw it :/ i found it now though, thank you very much :)

Marko Vidalis

unread,
Sep 14, 2013, 8:50:56 AM9/14/13
to codenameone...@googlegroups.com
I'd like to ask, how can i place a ComboBox in one of the cells of every row in the table?. I'm going to have a button which adds a new row to the table, and everytime a new row is added i'd like one of the cells to have a ComboBox in it. I have the corresponding code in a Java program:

 public void addCmb() {

        String sql = "SELECT * from stockinfo ORDER BY Parts";
        int size = 0;

        try {
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            List<String> list = new ArrayList<>();
            while (rs.next()) {
                list.add(rs.getString("Parts"));
            }
            int count = list.size();
            String[] items = list.toArray(new String[list.size()]);
            JComboBox<String> jcb = new JComboBox<>(items);
            TableColumn tc = tblContents.getColumnModel().getColumn(1);
            TableCellEditor tce = new DefaultCellEditor(jcb);
            tc.setCellEditor(tce);
        } catch (SQLException e) {
        }
    }

I've looked through the API and i can't seem to find corresponding methods for TableCellEditor, TableColumn, TableCellEditor and DefaultCellEditor. Could anyone help out with imports and stuff please?
Thanks in advance,

Marko

Shai Almog

unread,
Sep 14, 2013, 11:09:49 AM9/14/13
to codenameone...@googlegroups.com
If you are using a table you will need to inherit it and override the create table cell method. If you are using TableLayout just drag a combo box into place.

Marko Vidalis

unread,
Sep 14, 2013, 11:12:45 AM9/14/13
to codenameone...@googlegroups.com
I am using a table. Can i make use of the code pasted above for this table though? I'm not too sure of what you mean by 'inherit the table' :/
Thank you very much:)

Shai Almog

unread,
Sep 15, 2013, 12:42:41 AM9/15/13
to codenameone...@googlegroups.com
That is Swing code and has absolutely nothing to do with Codename One so it is completely useless.
In order to do that you will need to add the table through code and not via the GUI builder. You will then need to inherit the table class and override at least one method e.g.:
https://groups.google.com/forum/#!searchin/codenameone-discussions/table$20createCell/codenameone-discussions/diKjTX8kDIM/00KEKEAaINEJ

Marko Vidalis

unread,
Sep 15, 2013, 9:14:06 AM9/15/13
to codenameone...@googlegroups.com
Hi Shai,

Thank you very much for all your help. I'd like to ask one more question with regards to the table. I've added it through code however i cannot edit its text while on the gui, the whole idea is for the user to enter text into the table and then the app reads that information in from the table. 
how can i add that functionality?

Shai Almog

unread,
Sep 15, 2013, 2:33:41 PM9/15/13
to codenameone...@googlegroups.com
The model will need to define the given cell as editable in order for that cell to be editable.

Shai Almog

unread,
Sep 15, 2013, 2:33:57 PM9/15/13
to codenameone...@googlegroups.com
See DefaultTableModel.

Marko Vidalis

unread,
Sep 15, 2013, 4:54:23 PM9/15/13
to codenameone...@googlegroups.com
Hi Shai,

I'm sorry about all the questions, i just keep stumbling upon new problems.
I've included a text field into which the user will enter a string of data, and after pressing a button the information is read in from the text field and added to the 2D array, and then it is displayed in the table. After reading the value in, i declare a new DefaultTableModel for the table with the new array of values.
This works just fine, however in order for the value to actually be displayed, i have to actually click on something like a component on the interface or just scroll up or down, basically it requires some sort of interaction with the UI in order for the added value to appear in the table. 
Is there some way in which the added value can appear as soon as the button is pressed without any further interaction with the UI?

On Sunday, September 15, 2013 8:33:57 PM UTC+2, Shai Almog wrote:
See DefaultTableModel.

Marko Vidalis

unread,
Sep 15, 2013, 5:04:31 PM9/15/13
to codenameone...@googlegroups.com
oh and i'm also having a slight problem with how the table is displayed. I use the addComponent() method to add the table to a containeron the UI, which is in BoxLayoutY layout. However, the table does not stretch itself to fit to the entire container, about a third of it to the right of the container is blank. I have tried using tableLayout and gridLayout with one row and one column, however i have the same result. Is there some sort of method i could use to adjust the table's width?

Thank you so much for all your help :)
Marko

Shai Almog

unread,
Sep 16, 2013, 1:24:22 AM9/16/13
to codenameone...@googlegroups.com
No problem, we are here to help. Although if you ask questions that aren't really related I suggest opening a separate thread for convenience of people searching through the forum.

For the second issue use ((TableLayout)table.getLayout()).setGrowHorizontally(true);

Did you just modify the array of the table model or did you use setValueAt()? You need to use setValueAt() in order for the table to detect the refresh.
Reply all
Reply to author
Forward
0 new messages