Avatar of fattoo
fattooFlag 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
Avatar of zzynx
zzynx
Flag of Belgium image

>> 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".
Avatar of fattoo
fattoo
Flag of United States of America image

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
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of fattoo
fattoo
Flag of United States of America image

ASKER

How could I join them
Avatar of zzynx
zzynx
Flag of Belgium image

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
Avatar of zzynx
zzynx
Flag of Belgium image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of zzynx
zzynx
Flag of Belgium image

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

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo