Results 1 to 7 of 7
  1. #1
    Bill is offline Member
    Join Date
    Mar 2008
    Posts
    8
    Rep Power
    0

    Default Create different instance of a tablemodel

    Hi all,

    I need some help!

    I got a class MyClassModel who extends AbstractTablemodel. From a JPanel, i got a JTable that i want to take the model of this class. How can i instanciate this class from my JTable

    Something like that :
    Java Code:
    JTable table = new Jtable(new MyClassModel()), but this not work.
    I also want to pass to MyClassModel a different data (an array or something else) to build my table

    thank you for your help

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

    Default

    Java Code:
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class Test {
        public static void main(String[] args) {
            Test test = new Test();
            JTable table = new JTable(test.getModel());
            JOptionPane.showMessageDialog(null,
                    new JScrollPane(table), "",
                    JOptionPane.PLAIN_MESSAGE);
        }
    
        private AbstractTableModel getModel() {
            return new AbstractTableModel() {
                // You must specify at least these 3 methods.
                // See comments in AbstractTableModel api.
                public int getRowCount() { return 10; }
    
                public int getColumnCount() { return 4; }
    
                public Object getValueAt(int row, int col) {
                    return String.valueOf((row+1)) + (col+1);
                }
            };
        }
    }

  3. #3
    Bill is offline Member
    Join Date
    Mar 2008
    Posts
    8
    Rep Power
    0

    Default

    Hi,

    This is an example of my class:

    Java Code:
    public class MyTableModel extends AbstractTableModel{
    
                public MyTableModel(){}
    
                public int getRowCount() { return 10; }
    
                public int getColumnCount() { return 4; }
    
                public Object getValueAt(int row, int col) {
                    return String.valueOf((row+1)) + (col+1);
                }
    }
    and in my panel:

    Java Code:
    //in this table, i want to call a new instance of MyTableModel
    //to get the model
    JTable table = new JTable();
    I hope this clear

    I don't know to do this ! ! :confused:

    thx a lot

  4. #4
    Bill is offline Member
    Join Date
    Mar 2008
    Posts
    8
    Rep Power
    0

    Default

    The fact is.......when i loop more than once in
    Java Code:
    for(int i=0;i<2;i++){
    JTable table = new JTable(/*pass my new instance here*/);
    }
    than the table take only the last loop to create my model in my table

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

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class Test {
        public static void main(String[] args) {
            Test test = new Test();
            JPanel panel = new JPanel(new GridLayout(1,0,5,5));
            for(int i = 0; i < 3; i++) {
                JTable table = new JTable(test.getModel());
                Dimension d = table.getPreferredSize();
                d.height = 175;
                table.setPreferredScrollableViewportSize(d);
                panel.add(new JScrollPane(table));
            }
            JOptionPane.showMessageDialog(null,
                    new JScrollPane(panel), "",
                    JOptionPane.PLAIN_MESSAGE);
        }
    
        private AbstractTableModel getModel() {
            return new AbstractTableModel() {
                // You must specify at least these 3 methods.
                // See comments in AbstractTableModel api.
                public int getRowCount() { return 10; }
    
                public int getColumnCount() { return 4; }
    
                public Object getValueAt(int row, int col) {
                    return String.valueOf((row+1)) + (col+1);
                }
            };
        }
    }

  6. #6
    Bill is offline Member
    Join Date
    Mar 2008
    Posts
    8
    Rep Power
    0

    Default

    hi,

    its not exactly that i want to do. My AbstractTableModel extends my class not a method return an AbstractTableModel, and my panel is in another class

    Java Code:
    Public class MyTableModel extends AbstractTablemodel{
    
    }
    and my panel
    Java Code:
    public class MyPanel extends JPanel{
    
    }
    my loop is in this panel class and i want to instantiate the model of MyTableModel from a JTable in MyPanel.

    Thanks a lot for your help

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

    Default

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.table.*;
    
    public class TryIt {
        public static void main(String[] args) {
            JOptionPane.showMessageDialog(null,
                    new JScrollPane(new MyPanel()), "",
                    JOptionPane.PLAIN_MESSAGE);
        }
    }
    
    class MyPanel extends JPanel {
        public MyPanel() {
            super(new GridLayout(1,0,5,5));
            for(int i = 0; i < 3; i++) {
                JTable table = new JTable(new MyTableModel());
                Dimension d = table.getPreferredSize();
                d.height = 175;
                table.setPreferredScrollableViewportSize(d);
                add(new JScrollPane(table));
            }
        }
    }
    
    class MyTableModel extends AbstractTableModel {
        public int getRowCount() { return 10; }
    
        public int getColumnCount() { return 4; }
    
        public Object getValueAt(int row, int col) {
            return String.valueOf((row+1)) + (col+1);
        }
    }

Similar Threads

  1. Instantiation using an instance factory method
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-29-2008, 12:35 PM
  2. passing an array into an instance
    By lockmac in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 09:35 AM
  3. New Instance for SWT
    By srinivasa_v in forum SWT / JFace
    Replies: 1
    Last Post: 08-08-2007, 01:02 AM
  4. problems with Instance Data
    By paty in forum New To Java
    Replies: 2
    Last Post: 08-02-2007, 05:45 PM
  5. Instance variable
    By Jack in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 04:00 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
  •