JTable

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 16
Reputation: ebiz is an unknown quantity at this point 
Solved Threads: 0
ebiz ebiz is offline Offline
Newbie Poster

JTable

 
0
  #1
Nov 28th, 2008
I have a button which opens up the file chooser. When a txt file is selected a Jframe appears with the data in. How do I change this, so the data displays in a JTable?

Here is my current code

package org.project;

import java.util.*;
import java.io.*;
import javax.swing.*;

public class openResults
{

public void readMatch() throws Exception
{
JFrame frame = new JFrame ("Display File");

JTextArea ta = new JTextArea (20,30);
JFileChooser chooser = new JFileChooser(); //Creating the File Chooser

int status = chooser.showOpenDialog (null); //setting blank File Chooser

if(status != JFileChooser.APPROVE_OPTION)
ta.setText("No File Chosen");
else
{
File file = chooser.getSelectedFile(); //Selecting File
Scanner scan = new Scanner(file); //Scanning the file from file chooser

String info = "";
while(scan.hasNext())
info += scan.nextLine() + "\n";
ta.setText(info);
}
frame.getContentPane().add(ta);
frame.pack();
frame.setVisible(true);
}
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz

Re: JTable

 
0
  #2
Nov 28th, 2008
Hello Ebiz
  1. public static void readMatch() throws FileNotFoundException {
  2. JFrame frame = new JFrame("Display File");
  3. JTable table = new JTable();//Creating a new JTable
  4. List<String> columnData = new ArrayList<String>(); //Creating a List to store String data
  5. JFileChooser chooser = new JFileChooser(); //Creating the File Chooser
  6. int status = chooser.showOpenDialog(null); //setting blank File Chooser
  7. if (status != JFileChooser.APPROVE_OPTION) {
  8. ((DefaultTableModel) table.getModel()).addColumn("No File Chosen", (Object[])null);
  9. } else {
  10. File file = chooser.getSelectedFile(); //Selecting File
  11. Scanner scan = new Scanner(file); //Scanning the file from file chooser
  12. while (scan.hasNext()) {
  13. columnData.add(scan.nextLine()); // add String data to List
  14. }
  15. // one touch !
  16. ((DefaultTableModel) table.getModel()).addColumn("DATA", columnData.toArray());
  17. }
  18. JScrollPane jsp = new JScrollPane(table);
  19. frame.getContentPane().add(jsp);
  20. frame.pack();
  21. frame.setVisible(true);
  22. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//remember close the frame
  23. }
<String> You can delete
quuba
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 16
Reputation: ebiz is an unknown quantity at this point 
Solved Threads: 0
ebiz ebiz is offline Offline
Newbie Poster

Re: JTable

 
0
  #3
Nov 28th, 2008
Thank You.

frame.getContentPane().add(jsp);
this comes up with an error...

and so does:
DefaultTableModel
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,275
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 494
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: JTable

 
0
  #4
Nov 29th, 2008
Originally Posted by ebiz View Post
Thank You.

frame.getContentPane().add(jsp);
this comes up with an error...

and so does:
DefaultTableModel
When you get any errors you have to let us know what they are exactly as we cannot see what you getting on your screen.
For DefaultTableModel I bet you forgot to import required library. With your general imports as I seen import javax.swing.*; this would not cover it. So you need to add import javax.swing.table.*; or even better start using full paths like import javax.swing.table.DefaultTableModel;
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 16
Reputation: ebiz is an unknown quantity at this point 
Solved Threads: 0
ebiz ebiz is offline Offline
Newbie Poster

Re: JTable

 
0
  #5
Nov 29th, 2008
Thank You. I have mended these problems, but this creates a default table. I need to get the data from a text file. How would I manage this? I am currently importing a text file through a FileChooser, and displaying this data in a JFrame, so I just need to change this so that there is a table in the frame, and the data is all in the correct cells. Can anyone help? This is the code that I use to select the txt file:

package org.project;

import java.util.*;
import java.io.*;
import javax.swing.*;

public class openResults
{

public static void readMatch() throws FileNotFoundException
{
JFrame frame = new JFrame ("Match Results");

JTextArea ta = new JTextArea (20,30);
JFileChooser chooser = new JFileChooser(); //Creating the File Chooser

int status = chooser.showOpenDialog (null); //setting blank File Chooser

if(status != JFileChooser.APPROVE_OPTION)
ta.setText("No File Chosen");
else
{
File file = chooser.getSelectedFile(); //Selecting File
Scanner scan = new Scanner(file); //Scanning the file from file chooser

String info = "";
while(scan.hasNext())
info +=scan.nextLine() + "\n";
ta.setText(info);
}
frame.getContentPane().add(ta);
frame.pack();
frame.setVisible(true);
}
}
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz

Re: JTable

 
0
  #6
Nov 29th, 2008
Show Your txt file (a few lines).
quuba
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 54
quuba quuba is offline Offline
Posting Whiz

Re: JTable

 
0
  #7
Nov 29th, 2008
I recived not refreshed subject from www.
Last edited by quuba; Nov 29th, 2008 at 5:57 pm. Reason: doubled post
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,275
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 494
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: JTable

 
0
  #8
Nov 29th, 2008
@quuba - ebiz absolutely ignored your solution

Just lets wait what he will come back with.

PS: Nice city Krakow, been there in 1988
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 16
Reputation: ebiz is an unknown quantity at this point 
Solved Threads: 0
ebiz ebiz is offline Offline
Newbie Poster

Re: JTable

 
0
  #9
Nov 29th, 2008
I have changed the code. Just put on here the original.

here is a few lines of the file
westham,0,0,Queens Park Rangers
man u,1,0,Preston North End
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,275
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 494
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is online now Online
Code tags enforcer

Re: JTable

 
0
  #10
Nov 29th, 2008
Please also provide update program or we may just waste time here...
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1799 | Replies: 12
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC