Avatar of fattoo
fattoo
Flag for United States of America asked on

add jcombbox to jtable of array list

HI

I have JTable class that takes the array list of object as content
like this
String data [][] = new String [reportList.size()][5];

            //assign the elements in te array list to be displayed in the Jtable
            for (int pos = 0; pos < reportList.size(); pos++){
                  ReportsExample2 obj = reportList.get(pos);
                  data[pos][0] = obj.getTech();//at position 0
                  data[pos][1] = obj.getClient();//at position 1
                  data[pos][2] = obj.getPro();//at position 2
                  data[pos][3] = obj.getPr();//at position 3
                  data[pos][4] = obj.getSt();//at position 4


            }
and then shows table of 5 columns
how could I add jcombobox to specific column and allow the client to choose one of the values in the jcombobox  and store the new value in the array list

I found many examples on the internet but no one use sth like this


Many thanks
Java

Avatar of undefined
Last Comment
zzynx

8/22/2022 - Mon
zzynx

>> I found many examples on the internet but no one use sth like this
Could you rephrase that please.

Baseline:
1. You'll have to write your own TableModel (extending DefaultTableModel)
2. You'll have to write a custom CellEditor (extending DefaultCellEditor) that uses a JComboBox as its editor.
3. The above two points might sound difficult, but in fact they aren't.
You'll just have to make yourself familiar with the concept of table models and table renderers and editors.
How to use tables: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
This article has the sections "Creating a Table Model" and "Using a Combo Box as an Editor".
fattoo

ASKER
I mean all the examples i found create jtable and then create jcombobox with certain value
After that they assign jcombobox to certain column
My problem is that i am afraid if i create a combobox i will lose the actuall content of array list
E.g
The content of specific column before adding jcombobox like this
2
4
5
And after addind jcombobx
2
2
2
And when a customer clicks on 2 the list appears is 2 4 5
So the actual content of the array list wont be visible any more
Any help please
CEHJ

how could I add jcombobox to specific column and allow the client to choose one of the values in the jcombobox  and store the new value in the array list
What values would be in the combo? Ordinarily using a combo in a cell implies a set of values not directly in the table model, but joined in from elsewhere
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
fattoo

ASKER
How could I join them
zzynx

The content of specific column before adding jcombobox like this
2
4
5
And after addind jcombobx
2
2
2
And when a customer clicks on 2 the list appears is 2 4 5

So, that means that the contents of your combobox editor is OK. (since it contains 2, 4, 5)
However, it doesn't display the value that is in the cell. Instead of that it always displays the first value item (2) in its list.

Did you read the article I gave you? (How to use tables: http://docs.oracle.com/javase/tutorial/uiswing/components/table.html)
There you can read that a table has cell renderers and cell editors.
Your editor seems to work.
Your renderer doesn't seem to be ok.
ASKER CERTIFIED SOLUTION
zzynx

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
CEHJ

How could I join them
To answer that specifically, it's necessary to know what your combo box is intended to do and where it gets its model from
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
zzynx

Thanx 4 axxepting.
However, I just wonder: what answer worth an A grade did you expect me to give?