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

    Default Table, beginner stuff


    My problem is as follows:

    Java Code:
    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();
    		//adds the listener
    	//the listener
    	public void tableChanged(TableModelEvent e) {
    		//just checks if it goes past this part
    		//Try and catch block
    			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");
    				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());
    						ws.write((String) table.getValueAt(i,j)+",");
    		}catch (Exception error){
    			System.err.println("Error"+ error.getMessage());
    	public static void main(String[] args){
    		//Creates the Book object
    		new Book();
    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
    Rep Power


    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