Results 1 to 2 of 2
  1. #1
    drwk is offline Member
    Join Date
    Jan 2010
    Posts
    3
    Rep Power
    0

    Default 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]
    In need to somehow make table global or somehow accessible so that it can be written to a text file.:confused:

    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 appreciated
    Last edited by Fubarable; 01-24-2010 at 07:30 PM. Reason: Code tags added!

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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

  1. Trying to create a code for queue, complex stuff...
    By Mikey_S in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-28-2009, 09:13 PM
  2. Looking for help on drawing stuff in a jPanel
    By Gatts79 in forum AWT / Swing
    Replies: 3
    Last Post: 08-28-2009, 06:00 PM
  3. Anyone selling SCJA training material? Your OLD stuff?
    By KMN in forum Reviews / Advertising
    Replies: 2
    Last Post: 08-24-2009, 05:58 PM
  4. Simple Stuff 0.1
    By Java Tip in forum Java Software
    Replies: 0
    Last Post: 07-19-2008, 04:27 PM
  5. No Fluff Just Stuff Software Symposium Series 2007,
    By orchid in forum Reviews / Advertising
    Replies: 0
    Last Post: 04-08-2007, 08:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •