This week's book giveaway is in the Reactive Progamming forum.
We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line!
See this thread for details.
Win a copy of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams this week in the Reactive Progamming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

How to Display a JTable with the Data

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 65792
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't see how either of those two lines can throw a null Exception.
 
Sege Stephen
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 3073
24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Bartender
Posts: 11445
18
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Nidhii Sharma
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!