| | |
JTable
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2008
Posts: 16
Reputation:
Solved Threads: 0
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);
}
}
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);
}
}
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 54
Hello Ebiz
<String> You can delete
quuba
java Syntax (Toggle Plain Text)
public static void readMatch() throws FileNotFoundException { JFrame frame = new JFrame("Display File"); JTable table = new JTable();//Creating a new JTable List<String> columnData = new ArrayList<String>(); //Creating a List to store String data JFileChooser chooser = new JFileChooser(); //Creating the File Chooser int status = chooser.showOpenDialog(null); //setting blank File Chooser if (status != JFileChooser.APPROVE_OPTION) { ((DefaultTableModel) table.getModel()).addColumn("No File Chosen", (Object[])null); } else { File file = chooser.getSelectedFile(); //Selecting File Scanner scan = new Scanner(file); //Scanning the file from file chooser while (scan.hasNext()) { columnData.add(scan.nextLine()); // add String data to List } // one touch ! ((DefaultTableModel) table.getModel()).addColumn("DATA", columnData.toArray()); } JScrollPane jsp = new JScrollPane(table); frame.getContentPane().add(jsp); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//remember close the frame }
quuba
•
•
•
•
Thank You.
frame.getContentPane().add(jsp);
this comes up with an error...
and so does:
DefaultTableModel
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Nov 2008
Posts: 16
Reputation:
Solved Threads: 0
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);
}
}
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);
}
}
@quuba - ebiz absolutely ignored your solution
Just lets wait what he will come back with.
PS: Nice city Krakow, been there in 1988
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- Problem changing contents of JTable when button is pressed... (Java)
- deleting rows from a JTable (Java)
- How to make JTable cell not Editable (Java)
- JTable Reloading/Refreshing/Updating/Whatever. (Java)
- Adding and deleting a column in JTable (Java)
- JTable problem (Java)
- Getting horizontal scrollbars in a JTable (Java)
- JTable not calling getValueAt - any ideas? (Java)
- Saving and opeing a JTable (Java)
Other Threads in the Java Forum
- Previous Thread: how to make attempts in pin numbers
- Next Thread: Problem reading data from file
Views: 1789 | Replies: 12
| Thread Tools | Search this Thread |
Tag cloud for Java
actuate android api apple applet application arguments array arrays automation balls binary bluetooth business c++ chat class classes client code codesnippet collections component database defaultmethod doctype dragging draw ebook eclipse error event exception file fractal froglogic game givemetehcodez graphics gui helpwithhomework hql html ide image input integer intersect invokingapacheantprogrammatically j2me java javaprojects jmf jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie number numbers object oracle parameter php print problem program programming project recursion scanner screen server set size sms socket sort sql string sun swing swt tcp test threads time transfer tree udp windows






