Results 1 to 2 of 2
Thread: Table, beginner stuff
- 01-23-2010, 07:22 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 3
- Rep Power
- 0
Table, beginner stuff
Hi!
My problem is as follows:
Java Code:import java.io.BufferedWriter; import java.io.FileWriter; import javax.swing.*; import javax.swing.event.TableModelEvent; import javax.swing.event.TableModelListener; import javax.swing.table.*; [B] public class Book implements TableModelListener{ public Book( ){ String [] columnNames = {"first name","last name","sport","# of years","vegetarian"}; Object [][] data = { {"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)}, {"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, {"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, {"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)}, {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)} }; //Creates the GUI JTable table = new JTable (data, columnNames); JScrollPane pane = new JScrollPane(table); JFrame frame = new JFrame(); frame.add(pane); frame.setVisible(true); frame.setSize(500,500); //adds the listener table.getModel().addTableModelListener(this); } //the listener public void tableChanged(TableModelEvent e) { //just checks if it goes past this part System.out.println("Check"); //Try and catch block try{ FileWriter fs = new FileWriter("C:\\hej10.txt"); BufferedWriter ws = new BufferedWriter(fs); for(int i = 0; i<=2; i++){ //Amount of rows if (i >= 1){ System.out.println("den går igenom"); ws.write("\r\n"); } for(int j = 0; j<=3; j++){ //amount of columns if(j == 3){ //The problem with table not being found ws.write((String) table.getValueAt(i,j).toString()); } else{ ws.write((String) table.getValueAt(i,j)+","); }} } ws.close(); }catch (Exception error){ System.err.println("Error"+ error.getMessage()); } } public static void main(String[] args){ //Creates the Book object new Book(); } } [/B]
and yes the array part is taken from Javas tutorial JTable, and yes I'm to lazy to write the array myself :D
any help is appreciatedLast edited by Fubarable; 01-24-2010 at 08:30 PM. Reason: Code tags added!
- 01-24-2010, 08:19 PM #2
somehow make table global or somehow accessible
Make it a member variable in class scope:
Java Code:public class BookRx implements TableModelListener{ JTable table; // member variable public BookRx( ){ ... //Creates the GUI // table is a local variable when declared here // JTable table = new JTable (data, columnNames); // instantiate the member variable table = new JTable (data, columnNames); ...
Similar Threads
-
Trying to create a code for queue, complex stuff...
By Mikey_S in forum Threads and SynchronizationReplies: 3Last Post: 09-28-2009, 10:13 PM -
Looking for help on drawing stuff in a jPanel
By Gatts79 in forum AWT / SwingReplies: 3Last Post: 08-28-2009, 07:00 PM -
Anyone selling SCJA training material? Your OLD stuff?
By KMN in forum Reviews / AdvertisingReplies: 2Last Post: 08-24-2009, 06:58 PM -
Simple Stuff 0.1
By Java Tip in forum Java SoftwareReplies: 0Last Post: 07-19-2008, 05:27 PM -
No Fluff Just Stuff Software Symposium Series 2007,
By orchid in forum Reviews / AdvertisingReplies: 0Last Post: 04-08-2007, 09:13 PM
Bookmarks