Results 1 to 9 of 9
  1. #1
    darrell22 is offline Member
    Join Date
    Feb 2010
    Posts
    13
    Rep Power
    0

    Default How to assign values to object?

    Sorry for such a simple question.

    Could someone please help me with simple assignment of values to a previously initialized object?


    See the method useModel ()

    The idea is, assign the values to the temporary object, data
    Then plunk it into this statement:
    model.addRow ( data );


    Simple enough?



    I've been putzing with the syntax for multiple hours, over days, now.

    With and without

    [0]
    ;
    ,


    Netbeans keeps giving me: Illegal start of expression


    data is declared as an array of Object, although, in this case,
    it does not need to be an array.


    What is the correct syntax please?



    Thanks a lot!


    --------


    Java Code:
    //--
    import java.awt.BorderLayout;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.util.Date;
    
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    
    
    public class SimpleDefaultTableModel2 extends JFrame {
        
        private  JScrollPane scrollPane;
       
        DefaultTableModel model ; 
        
        Object[] data = {
                new String ( "My String" ) , 
                new Date()  ,
                new Integer(123),
        };
    
        // constructor
        public SimpleDefaultTableModel2() 
        {
            model = new   DefaultTableModel (   ) ;
    
            setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
    
            model.addColumn( "Column1"  );
            model.addColumn( "Column2"  );
            model.addColumn( "Column3"  );
    
            model.addRow  (data   );
    
            System.out.println ("data[0].toString() is: "  + data[0].toString() );
            System.out.println ("data[0].toString() is: "  + data[1].toString() );
            System.out.println ("data[0].toString() is: "  + data[2].toString() );
            
                    
            // I can modify data, cell by cell
            model.setValueAt(123.456, 0, 2)        ;
            model.setValueAt (   "New String" , 0  , 0   );
            
            System.out.println ("data[0].toString() is: "  + model.getValueAt(  0, 2   )   );   
    
            
            JTable table = new JTable ( model  );
            getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
        
            pack();
            
       }  // end constructor SimpleDefaultTableModel2
    
        
        public void useModel ()
        {
            
            Date dateTemp = new Date()  ;
            
            data[0] = {
                 "My String"  ; 
                 dateTemp  ;
                456 ;
            };
    
          //  model.addRow(data);
            
        }  // end useModel ()
        
        
        
      public static void main(String arg[]) {
    
        SimpleDefaultTableModel2 test   =  new SimpleDefaultTableModel2() ;
        test.setVisible(true);
        test.pack();
        
      }  // end main
    
    }  // end class SimpleDefaultTableModel
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    //--

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: How to assign values to object?

    Netbeans keeps giving me: Illegal start of expression
    It's helpful to post the entire compiler message as it has useful information like the exact line and character position it is talking about.

    In your case the assignment

    Java Code:
    data[0] = {
        "My String"  ; 
        dateTemp  ;
        456 ;
    };
    The first thing to realise is that the thing you have defined as data is an array of three instances of Object (a string, a date and an int which will be converted and stored as an instance of Integer). To store the data you want in that array you assign each of the three objects to their corresponding slot in the array:

    Java Code:
    data[0] = "My String";
    data[1] = dateTemp;
    data[2] = 123;
    ---

    Is there some reason why you assign values to the slots in the data array to begin with? If not, you might as well remove those assignments which will improve the clarity of the code.

    Java Code:
    import java.util.Date;
    
    public class Test {
        public static void main(String[] args) {
            /*
            Object[] data = {
                new String ( "My String" ) , 
                new Date(),
                new Integer(123),
            };
            */
            Object[] data = new Object[3];
            System.out.println(data.length);
    
            Date dateTemp = new Date()  ;
            data[0] = "My String";
            data[1] = dateTemp;
            data[2] = 123;
    
            System.out.print("Data elements are: ");
            System.out.println(data[0] + ", " + data[1] + ", " + data[2]);
        }
    }
    [Edit] Continuing this thought a little, why have an instance variable, data at all? Better to declare it when and where you use it:

    Java Code:
    Object[] toAdd = {"My String", dateTemp, 123};
    model.addRow(toAdd);
    Or just

    Java Code:
    model.addRow(new Object[] {"My String", dateTemp, 123});
    Last edited by pbrockway2; 04-25-2015 at 11:26 PM.

  3. #3
    darrell22 is offline Member
    Join Date
    Feb 2010
    Posts
    13
    Rep Power
    0

    Default Re: How to assign values to object?

    Hello,

    Thanks for the help!

    I wanted a single temp variable to be defined once,
    then used, over and over.
    Not create a new () object everytime.

    -----

    You got the syntax right. This worked perfectly:

    data[0] = "useModel";
    data[1] = dateTemp;
    data[2] = 456.68;


    -----

    However, from my experiments, it seems that in DefaultTableModel,
    everything gets changed to a string!

    I wanted each column to be rigorously enforced as their appropriate datatype.
    String, Date, Integer.
    Just like a regular datbase table.
    That way, if you try to add an incorrect datatype, the error is trapped.


    I've added some more code to show how:
    - the data is not a date
    - an Integer, is not Integer


    Not quite useless, but most disappointing. :(


    Java Code:
    // ---
    
    
    import java.awt.BorderLayout;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.util.Date;
    
    import javax.swing.JFrame;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    
    
    public class SimpleDefaultTableModel2 extends JFrame {
        
        private  JScrollPane scrollPane;
       
        DefaultTableModel model ; 
        
        Object[] data = {
                new String ( "My String" ) , 
                new Date()  ,
                new Integer ( 123 ),
        };
    
    //     Integer intTemp = new Integer(123.456);
        
    //    error: no suitable constructor found for Integer(double)
    //     Integer intTemp = new Integer(123.456);
    //    constructor Integer.Integer(String) is not applicable
    //      (actual argument double cannot be converted to String by method invocation conversion)
    
          
        // constructor
        // assign values in the constructor
        public SimpleDefaultTableModel2() 
        {
            model = new   DefaultTableModel (   ) ;
    
            setDefaultCloseOperation ( JFrame.EXIT_ON_CLOSE );
    
            model.addColumn( "String"  );
            model.addColumn( "Date"  );
            model.addColumn( "Integer Object"  );
    
            model.addRow  (data   );
    
            System.out.println ("data[0].toString() is: "  + data[0].toString() );
            System.out.println ("data[1].toString() is: "  + data[1].toString() );
            System.out.println ("data[2].toString() is: "  + data[2].toString() );
            
                    
            // I can modify data, cell by cell
            model.setValueAt (   "New String" , 0  , 0   );
            model.setValueAt ( 123.456, 0, 2)        ;
            // should give an error, but does not. 
            
            model.setValueAt ( "String, but should be Integer", 0, 2)        ;
            // should give an error, but does not. 
            
            
            System.out.println ("model.getValueAt( 0, 2 ) is: "  + 
                    model.getValueAt(  0, 2   )   );   
    
            
            JTable table = new JTable ( model  );
            getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
        
            pack();
            
       }  // end constructor SimpleDefaultTableModel2
    
        
        public void useModel ()
        {
            
            Date dateTemp = new Date()  ;
            
            data[0] = "useModel";
            data[1] = dateTemp;
            data[1] = "A string, should be date";
            data[2] = 456.68;
        
            model.addRow (data );
            
        }  // end useModel ()
        
        
        
      public static void main(String arg[]) {
    
        SimpleDefaultTableModel2 test   =  new SimpleDefaultTableModel2() ;
        test.useModel();
        
        test.setVisible(true);
        test.pack();
        
        System.out.println (  test.model.getDataVector() );
        
      }  // end main
    
    }  // end class SimpleDefaultTableModel
    
    
    
    
    // ---

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: How to assign values to object?

    Have you checked out the Java tutorials linked in my signature. They provide some good examples or using tables.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: How to assign values to object?

    Quote Originally Posted by darrell22 View Post
    I wanted each column to be rigorously enforced as their appropriate datatype.
    String, Date, Integer.
    Just like a regular datbase table.
    That way, if you try to add an incorrect datatype, the error is trapped.
    For that, you need to override getColumnClass(...) appropriately.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    darrell22 is offline Member
    Join Date
    Feb 2010
    Posts
    13
    Rep Power
    0

    Default Re: How to assign values to object?

    Yes, I've been looking at many webpages over the past week or more.
    Tutorials. Docs/Reference pages.
    Including the official docs.

    I've downloaded at least 12 programs off the web and studied them.

    It sure is slow going working in isolation as I do.

    Thanks for all your support when I've got stuck.



    Quote Originally Posted by jim829 View Post
    Have you checked out the Java tutorials linked in my signature. They provide some good examples or using tables.

    Regards,
    Jim

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: How to assign values to object?

    Did you find the "How to use tables" bit of Oracle's Tutorial? It includes a custom TableModel and illustrates associating a class with a column.

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: How to assign values to object?

    https://docs.oracle.com/javase/tutor...nts/table.html

    (google: "java swing how to use tables").
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: How to assign values to object?

    Deleted an irrelevant response copy/pasted form the net. anthonyemuobo, if you have anything relevant and original to say, feel free to add another post.

    Any repeat of irrelevant material found on multiple sites may however result in a ban.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 7
    Last Post: 02-04-2013, 06:27 PM
  2. How to assign letter values?
    By jsaavedra in forum New To Java
    Replies: 1
    Last Post: 11-14-2011, 05:55 PM
  3. to assign values dynamically
    By su5 in forum New To Java
    Replies: 1
    Last Post: 04-28-2011, 08:00 PM
  4. Iterators - can you assign new values to them?
    By DerekRaimann in forum New To Java
    Replies: 2
    Last Post: 12-09-2010, 07:11 PM
  5. Array Assign Values from a Textfile
    By fawadafr in forum Java Applets
    Replies: 6
    Last Post: 11-30-2008, 12:10 AM

Posting Permissions

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