Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Can a dialog box contain JTable ?  RSS feed

 
Mahajan Bhupendra
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i want a table like structure to display in dialog box
i tried this
JOptionPane.showConfirmDialog(frame,table);
where frame is JFrame and table is object of type JTable
but it's giving me runtime error..
actullay i just want a table to appear with heading and
only one row...
is it possible???
BHupendra Mahajan
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll have to create your dialog window by creating a JDialog window and adding the JTable (and any other widgets you want) to it yourself. JOptionPane is just used for building simple dialogs quickly to show error/confirmation messages and that sort of thing, and it probably doesn't know how to handle tables.

-Nate
 
Mahajan Bhupendra
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nate
i tried the option by u....
but it's still giving me error..
my code is...

Bhupendra Mahajan
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bhupendra,

It is not a problem with the table per se... it is a problem with the data you are sending it. The row Vector needs to be a Vector of Vectors, not just a Vector of Strings. (There has to be a way to show what row the data belongs to!) Replace your dialog with the one below and it will work!


-Nate
 
Mahajan Bhupendra
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Nate..
actullay i solved the problem..
it was the same as u said..
but i forgot to reply back here..
anyway thanks..

------------------
Bhupendra Mahajan
 
Sachendra Pal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bhupendra/Nathan

Thanks for the wonderful example. After displaying table in the Dialog Box, i want to click individual cells and store that value in a text box. Is it possible and how i can achieve it?
Waiting for a positive reply.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

The recommended way to achieve this is a add a selection listener to the table's selection model. Once you get a selection notification, you can figure out the selected cell and process the value.
Recommended reading: https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#selection
 
Sachendra Pal
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Welcome to the Ranch.

The recommended way to achieve this is a add a selection listener to the table's selection model. Once you get a selection notification, you can figure out the selected cell and process the value.
Recommended reading: https://docs.oracle.com/javase/tutorial/uiswing/components/table.html#selection


Can you please help me out with a code example?
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The link I posted already contains code with explanation. Did you go through it?
We do not just hand out code here at the Ranch. Show us your code, tell us where you are stuck and we will help point you in the right direction
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!