Link to home
Start Free TrialLog in
Avatar of rohitdivas
rohitdivas

asked on

Add radio-button in JTable

Hi All,

I have added a JTable in a Panel. I have written below piece of code for it. I have two queries:

1) Using the below set of code, the application doesnot shows me the header part, columnNames, although it is displaying the data properly. I suppose i am missing some point here. Please comment!

JTable jAddInstanceTable ;
    Object[] columnNames = {
        "Select",
        " Name",
        "String Value",
        "String Password",
        " Comments"};
    Object[][] data = {
        {
        "radio-button", "iName", "value", "password", "comments"}
        , {
        "radio-button0", "iName0", "value0", "password0", "comments0"}
        , {
        "r1", "iName1", "value1", "password1", "comments1"}
    };

    jAddInstanceTable = new JTable(data,columnNames);
    JTableHeader th = new JTableHeader();
    th.setName("InstanceVector");
    jAddInstanceTable.setTableHeader(th);

2) I need to add radio button inside one of the row. and at one time only one radio button should be selectable. Any comments how to proceed for the same?

Thanks,
rdh
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

1. You need to add the table to a JScrollPane

2. Please say more about how the radio buttons are to be distributed
1) Remove
>>  jAddInstanceTable.setTableHeader(th);
>> 1. You need to add the table to a JScrollPane
And that too
Avatar of rohitdivas
rohitdivas

ASKER

Regarding radiobutton, i need to display a radio button in first column of every row. And at one time user can select only one radio button
I am not getting the desired result even after deleting following lines from the code:
 JTableHeader th = new JTableHeader();
    th.setName("InstanceVector");
    jAddInstanceTable.setTableHeader(th);

rdh
>> I am not getting the desired result
Did you
>> add the table to a JScrollPane
?
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Adding to a scroll pane as i suggested is necessary

>>and at one time user can select only one radio button

That's going to take a special table model, where the values 'cut across' rows. You'll need a custom renderer to show the radio buttons
Thank you
rohitdivas can you tell me why the answer i gave you was ignored please?
Hi CEHJ,

Actually i was looking for a complete solutions for both of my queries. You had answered my first query right. But zzynx gave me complete answer.

But thanks a lot CEHJ for helping. Also, I am not ignoring your answer at all.

Regards,
RDH
>>Actually i was looking for a complete solutions for both of my queries. You had answered my first query right.

Then i should have been credited for it.
For me query a) had 10% weightage and query b) had 90% weightage.
So i.e 5 points for anwering ur query. Rather giving u 5 points , its better to give all 50 points to zzynx.

But, still lots of thanks!!

Do not worry CEHJ, next time, i shall definately consider this situation while assigning points.
RDH
Blacklist++
rohitdivas, what about this proposal:
>> For me query a) had 10% weightage and query b) had 90% weightage.
Then you should have given the question a number of points that's in accordance of this weightage.
Since 20 points is the minimum to give in case of a split (10% = 20 points => 100% = 200 points)

You could
1) ask to reopen this question
2) once reopened, increment the points to 200
3) re-accept and split the points into 20/180
But frankly telling, i do not wish to assign any more points on this query. What about proposal:
you transfer 5 points to CEHJ.
What do you say, CEHJ?
rdh
>>Then you should have given the question a number of points that's in accordance of this weightage
Moreover if you look at the qureries, one can easily guess abt the weightage, it doesnot require any explicit mention.
rdh
Forget it zzynx thanks, i simply shan't answer this person's questions again. I hope if you do, you don't happen to answer the 'wrong' part of the question correctly ;-)
It's your wish CEHJ, no nody is forcing you to answer.

I liked zzynx quick, accurate and complete responses that is why I assigned him all the points.

I donot have any personal disliking for you, its just that i found zzynx more deserving.

rdh
>> I hope if you do, you don't happen to answer the 'wrong' part of the question correctly
As a matter of fact, rohitdivas, in the future you can avoid this kind of problems by following this EE rule:
Don't ask more than one question per 'thread'.
Sure zzynx, anyways thanks for providing excellent, quality and timely answers
rohit
You're welcome