943,955 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3775
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 28th, 2008
0

JTable

Expand Post »
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);
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ebiz is offline Offline
16 posts
since Nov 2008
Nov 28th, 2008
0

Re: JTable

Hello Ebiz
java Syntax (Toggle Plain Text)
  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
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Nov 28th, 2008
0

Re: JTable

Thank You.

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

and so does:
DefaultTableModel
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ebiz is offline Offline
16 posts
since Nov 2008
Nov 29th, 2008
0

Re: JTable

Click to Expand / Collapse  Quote originally posted by ebiz ...
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;
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Nov 29th, 2008
0

Re: JTable

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);
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ebiz is offline Offline
16 posts
since Nov 2008
Nov 29th, 2008
0

Re: JTable

Show Your txt file (a few lines).
quuba
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Nov 29th, 2008
0

Re: JTable

I recived not refreshed subject from www.
Last edited by quuba; Nov 29th, 2008 at 5:57 pm. Reason: doubled post
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Nov 29th, 2008
0

Re: JTable

@quuba - ebiz absolutely ignored your solution

Just lets wait what he will come back with.

PS: Nice city Krakow, been there in 1988
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Nov 29th, 2008
0

Re: JTable

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ebiz is offline Offline
16 posts
since Nov 2008
Nov 29th, 2008
0

Re: JTable

Please also provide update program or we may just waste time here...
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: how to make attempts in pin numbers
Next Thread in Java Forum Timeline: Problem reading data from file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC