aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes How to Display a JTable with the Data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to Display a JTable with the Data " Watch "How to Display a JTable with the Data " New topic
Author

How to Display a JTable with the Data

Sege Stephen
Ranch Hand

Joined: Oct 06, 2007
Posts: 51
Hello Everyone,

I have a JTable inside my frame, I also have Field to get a file to be loaded. This data in the in the TableModel is coming from the excel sheet. I have a text field to find this excel sheet, when I browse and select the file, I click the load button to load this data. I am reading the data into my Table model and then setting the Column name and the Object [][] data in the MyTableModel class..

the thing is that if I hardcode the values for the Column name, and the data, when i create JTable with

JTable table = new JTable(new MyTableModel());

I get the table display with the data.

Problem is when I use the values I am getting from the excel sheet, I get the a null point exception because I haven't loaded the file.

so I figure out that my i need to create an instance of MyTableModel in the



however, the problem is that once the UI is already rendered, the table is not updating.

he is my myTableModel class



In summary, my question is how do I display my table after the loading file, and I also need it to get the table contents after I click load.
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
Where are you getting the NullPointerException?

I don't see how you're loading the information into your model. You should be able to create your JTable and the model at construction time, then populate the model with data whenever you load a file.
Sege Stephen
Ranch Hand

Joined: Oct 06, 2007
Posts: 51
Kevin Workman wrote:Where are you getting the NullPointerException?

I don't see how you're loading the information into your model. You should be able to create your JTable and the model at construction time, then populate the model with data whenever you load a file.


Hi Kelvin, I am getting Null Pointer exception if I try to create the JTable with an instance of MyTableModel before loading the file

so, if I do



it throws the null pointer. My intention is to create and load the table after the data has been loaded.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38726
    
  23
I can't see how either of those two lines can throw a null Exception.
Sege Stephen
Ranch Hand

Joined: Oct 06, 2007
Posts: 51
One other thing

if I have a List of List;

like

[[UK, 1, 10.13.64.1, A, CACHE], [UK, 1, 10.13.64.2, B, CACHE], [UK, 1, 10.13.64.3, C, CACHE]]

how do I convert to it Object [][] data
Sege Stephen
Ranch Hand

Joined: Oct 06, 2007
Posts: 51
Campbell Ritchie wrote:I can't see how either of those two lines can throw a null Exception.


Ritchie, line 04 throws an exception because there is no data in the MyTableModel. So that means I have to run load data first before creating an instance of MyTableModel, this then lead me to my problem, the table is not getting updated with the data.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
however, the problem is that once the UI is already rendered, the table is not updating.


You should only ever create the table once and add it to the frame once.

Then when you change the data your code will simple be:



The data will automatically be updated in the table.

Also, based on the code you posted I doubt you need to create a custom TableModel. Just use the DefaultTableModel.
Kevin Workman
Ranch Hand

Joined: Sep 28, 2010
Posts: 151
Sege Stephen wrote:One other thing

if I have a List of List;

like

[[UK, 1, 10.13.64.1, A, CACHE], [UK, 1, 10.13.64.2, B, CACHE], [UK, 1, 10.13.64.3, C, CACHE]]

how do I convert to it Object [][] data


That's a separate issue and might deserve its own thread in the Beginning Java forum, but how would you go about converting a single List into a single array?
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10329
    
    8

Sege Stephen wrote:One other thing
if I have a List of List;
like
[[UK, 1, 10.13.64.1, A, CACHE], [UK, 1, 10.13.64.2, B, CACHE], [UK, 1, 10.13.64.3, C, CACHE]]
how do I convert to it Object [][] data


You need not convert to Object[][] or any of the types specified in the model constructor. You can always subclass and build your model based on the data structure you have.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Nidhii Sharma
Greenhorn

Joined: Dec 04, 2012
Posts: 4
Hi. I am having the similar problem. Do you have the solution for this problem?. please post the solution, it would be a great help thanks
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4540
    
    5

Nidhi.mtech Sharma wrote:Hi. I am having the similar problem. Do you have the solution for this problem?. please post the solution, it would be a great help thanks


The solution to the problem discussed here *is* already posted in the thread. Maybe you have a different problem, in which case you need to describe it and supply adequate details.


luck, db
There are no new questions, but there may be new answers.
Nidhii Sharma
Greenhorn

Joined: Dec 04, 2012
Posts: 4
Darryl Burke wrote:
Nidhi.mtech Sharma wrote:Hi. I am having the similar problem. Do you have the solution for this problem?. please post the solution, it would be a great help thanks


The solution to the problem discussed here *is* already posted in the thread. Maybe you have a different problem, in which case you need to describe it and supply adequate details.

@Darry1 Burke, I am also using the same method to display the file data in a JTable, but the problem is I am getting the null pointer exception as posted above. what should i do to call Mytablemodel which extends AbstractTableModel in Jtable so that nullpointerexception is removed. there is no solution for this problem here Question asked is same as my problem. please help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to Display a JTable with the Data