This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS 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
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

import excel file and show in Jtable with netbeans (with Jbutton)  RSS feed

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

Who can help me.

I am programming java for the first time and now i have to make a school project.
The idee is to import a excel file in java and show this data in a Jtable (via a jbutton in the window)
When you click on the second jbutton there will open a new window and show a bargraph. (with some data from the table)

Can some one help me with the code.

Kind regards, Thomas
 
Rancher
Posts: 3987
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure it's an Ecel file (.xls, or .xlsx) and not a CSV file (.csv)?
I'd be surprised any beginners class would expect you to figure out how to use any of the Excel frameworks.
 
Marshal
Posts: 63837
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch

I wouldn't expect a beginner's course to tell people who to import CSVs either.
 
Dave Tolls
Rancher
Posts: 3987
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did think about that after I posted, it has to be said.


And throwing in Swing as well.
 
Campbell Ritchie
Marshal
Posts: 63837
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, some people would teach quite raw students to use Swing. Not sure why; I'd leave GUIs to more advanced work, but let's not stray too far from the original intent of the thread.
 
Arie Jansen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hello everyone,

Thanks for the reply`s.

The file to import is a .xls file.
I have made already some code to import .xls file in Java and this works.

The problem is to use this data in the Jtable when i click on the button in the screen. (I don't no the code)

I make some screen shots this evening for clarification.
 
Sheriff
Posts: 24295
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Arie Jansen wrote:The problem is to use this data in the Jtable when i click on the button in the screen.



Then the next step is to wrote some code which includes a JTable. Don't say to yourself "I need a JTable which contains data which came from an XLS file" -- a JTable doesn't care where its data comes from. Keep things separate as much as possible. Start with the JTable code and make it so you're adding constant data to the table model. Only when that works should you go on to modify it so that you're adding data from the XLS file to the table model.
 
Arie Jansen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Paul for the advice

I use the JTable in netbeans 8.2 see attachment. On the bottom of the screen you see the data that i have import the data in java from the .xls file.

Can some one help me with the code to fill the Jtable with the data that i have already availeble in java?

Thankx
Schermafbeelding-2019-04-15-om-19.47.20.png
[Thumbnail for Schermafbeelding-2019-04-15-om-19.47.20.png]
 
Arie Jansen
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the java code to import the .xls file

import java.io.File;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author thomas
*/
public class Read {
   public static void main (String[] args) throws Exception{
       File f=new File("/Users/thomas/Desktop/Data1.xls");
               
      Workbook wb=Workbook.getWorkbook(f);
      Sheet s=wb.getSheet(0);
      int row=s.getRows();
      int col=s.getColumns();
      for (int i = 0; i <row; i++) {
          for (int j = 0; j <col; j++) {
              Cell c=s.getCell(j, i);
              System.out.print(c.getContents()+"\t\t");                        
          }
          System.out.println("");
      }
   
   }
 
}

 
Master Rancher
Posts: 3028
21
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so you have the data in the Workbook class. Now you need to use that data to create a DefaultTableModel.

So, in your looping code you need to create a Vector with the data in a Cell to represent a row in the table model.

Once you get a row of data in the Vector you add the Vector to the main Vector, so you have Vector of Vectors to represent the Excel data.

Then you use the Vector of Vectors to create the DefaultTableModel which is used to create the JTable.

Take a look at the code from lines 20-39 found in this posting: https://coderanch.com/t/631989/java/jTable-jdbc-errors

It is similar to what you want. The difference is that it gets the data from a ResultSet. But it shows the concept of creating the Vector of Vectors.
 
For my next feat, I will require a volunteer from the audience! Perhaps this tiny ad?
global solutions you can do in your home or backyard
https://coderanch.com/t/708587/global-solutions-home-backyard
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!