Results 1 to 2 of 2

Thread: JTable problem

  1. #1
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default JTable problem

    I also have problem using JTable
    The columnName and Object refuse to display on my JTable... Please help... Below is my Code. I'm using Netbean IDE.. I created an abstractClass that i'm calling to my JTable to display on JFrame..

    Below is my Code...

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    /*
     * TableDemo.java
     *
     * Created on 07-Aug-2011, 09:20:21
     */
    
    package table.demo;
    
    /**
     *
     * @author ImoPoly
     */
    public class TableDemo extends javax.swing.JFrame {
    
        /** Creates new form TableDemo */
        public TableDemo() {
            initComponents();
        }
    
        /** This method is called from within the constructor to
         * initialize the form.
         * WARNING: Do NOT modify this code. The content of this method is
         * always regenerated by the Form Editor.
         */
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jScrollPane1 = new javax.swing.JScrollPane();
            jTable1 = new javax.swing.JTable(new MyTableModel());
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    
            jTable1.setModel(new javax.swing.table.DefaultTableModel(
    
            ));
            jTable1.setFillsViewportHeight(true);
            jScrollPane1.setViewportView(jTable1);
    
            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 475, Short.MAX_VALUE)
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(311, Short.MAX_VALUE))
            );
    
            pack();
        }// </editor-fold>
    
        /**
        * @param args the command line arguments
        */
        public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new TableDemo().setVisible(true);
                }
            });
        }
    
        // Variables declaration - do not modify
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTable jTable1;
        // End of variables declaration
    
    }
    
    
    package table.demo;
    import javax.swing.table.AbstractTableModel;
    
    class MyTableModel extends AbstractTableModel {
            private String[] columnNames = {"First Name",
                                            "Last Name",
                                            "Sport",
                                            "# of Years",
                                            "Vegetarian"};
            private Object[][] data = {
    	    {"Kathy", "Smith",
    	     "Snowboarding", new Integer(5), new Boolean(false)},
    	    {"John", "Doe",
    	     "Rowing", new Integer(3), new Boolean(true)},
    	    {"Sue", "Black",
    	     "Knitting", new Integer(2), new Boolean(false)},
    	    {"Jane", "White",
    	     "Speed reading", new Integer(20), new Boolean(true)},
    	    {"Joe", "Brown",
    	     "Pool", new Integer(10), new Boolean(false)}
            };
            private boolean DEBUG;
    
            public int getColumnCount() {
                return columnNames.length;
            }
    
            public int getRowCount() {
                return data.length;
            }
    
            @Override
            public String getColumnName(int col) {
                return columnNames[col];
            }
    
            public Object getValueAt(int row, int col) {
                return data[row][col];
            }
    
            /*
             * JTable uses this method to determine the default renderer/
             * editor for each cell.  If we didn't implement this method,
             * then the last column would contain text ("true"/"false"),
             * rather than a check box.
             */
            @Override
            public Class getColumnClass(int c) {
                return getValueAt(0, c).getClass();
            }
    
            /*
             * Don't need to implement this method unless your table's
             * editable.
             */
            @Override
            public boolean isCellEditable(int row, int col) {
                //Note that the data/cell address is constant,
                //no matter where the cell appears onscreen.
                if (col < 2) {
                    return false;
                } else {
                    return true;
                }
            }
    
            /*
             * Don't need to implement this method unless your table's
             * data can change.
             */
            @Override
            public void setValueAt(Object value, int row, int col) {
                if (DEBUG) {
                    System.out.println("Setting value at " + row + "," + col
                                       + " to " + value
                                       + " (an instance of "
                                       + value.getClass() + ")");
                }
    
                data[row][col] = value;
                fireTableCellUpdated(row, col);
    
                if (DEBUG) {
                    System.out.println("New value of data:");
                    printDebugData();
                }
            }
    
            private void printDebugData() {
                int numRows = getRowCount();
                int numCols = getColumnCount();
    
                for (int i=0; i < numRows; i++) {
                    System.out.print("    row " + i + ":");
                    for (int j=0; j < numCols; j++) {
                        System.out.print("  " + data[i][j]);
                    }
                    System.out.println();
                }
                System.out.println("--------------------------");
            }
        }
    Last edited by sunde887; 08-07-2011 at 02:00 PM. Reason: Added code tags, [code]...[/code]

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default

    ebiwari@tenece.com, don't hijack a running discussion with an unrelated question. Get your own thread -- they're free.

    Also, go through this page then edit your post and format the code correctly.

    db

Similar Threads

  1. Problem with jTable
    By pink123 in forum AWT / Swing
    Replies: 2
    Last Post: 03-22-2011, 01:55 AM
  2. JTable problem
    By Gesqy in forum NetBeans
    Replies: 0
    Last Post: 07-17-2010, 08:28 AM
  3. JTable problem
    By sandeepsai39 in forum New To Java
    Replies: 2
    Last Post: 06-27-2009, 07:15 AM
  4. JTable problem
    By robbertds1 in forum AWT / Swing
    Replies: 1
    Last Post: 05-20-2008, 07:34 PM
  5. Replies: 2
    Last Post: 05-15-2008, 11:15 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
  •