Results 1 to 2 of 2
Thread: JTable problem
- 08-07-2011, 12:46 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 3
- Rep Power
- 0
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 01:00 PM. Reason: Added code tags, [code]...[/code]
- 08-07-2011, 12:59 PM #2
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
-
Problem with jTable
By pink123 in forum AWT / SwingReplies: 2Last Post: 03-22-2011, 12:55 AM -
JTable problem
By Gesqy in forum NetBeansReplies: 0Last Post: 07-17-2010, 07:28 AM -
JTable problem
By sandeepsai39 in forum New To JavaReplies: 2Last Post: 06-27-2009, 06:15 AM -
JTable problem
By robbertds1 in forum AWT / SwingReplies: 1Last Post: 05-20-2008, 06:34 PM -
Jtable duplicates through Hashtable (JTable condition problem) my assignment plz help
By salmanpirzada1 in forum Advanced JavaReplies: 2Last Post: 05-15-2008, 10:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks