Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default JTable Data refuse to Display

    Please Help
    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...

    /*
    * 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.WindowConstan ts.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("--------------------------");
    }
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You already have a thread with the same code and question, please ask all questions there: JTable problem

    Closing this one.

Similar Threads

  1. Replies: 2
    Last Post: 02-23-2011, 06:14 PM
  2. [SOLVED] Cant display data in jTable
    By tpyq in forum NetBeans
    Replies: 2
    Last Post: 12-09-2008, 03:42 PM
  3. JButton to display JTable
    By Nemesis777 in forum New To Java
    Replies: 0
    Last Post: 12-08-2008, 01:16 PM
  4. Display XML in JTable
    By boy22 in forum XML
    Replies: 2
    Last Post: 12-07-2008, 07:03 PM
  5. Replies: 0
    Last Post: 12-04-2008, 06:39 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
  •