Results 1 to 2 of 2
Thread: JTable Data refuse to Display
- 08-07-2011, 01:04 PM #1
Member
- Join Date
- Aug 2011
- Posts
- 3
- Rep Power
- 0
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("--------------------------");
}
}
- 08-07-2011, 01:06 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You already have a thread with the same code and question, please ask all questions there: JTable problem
Closing this one.
Similar Threads
-
JFrame invisible refuse to paint windows content tho everything else runs?
By Addez in forum New To JavaReplies: 2Last Post: 02-23-2011, 05:14 PM -
[SOLVED] Cant display data in jTable
By tpyq in forum NetBeansReplies: 2Last Post: 12-09-2008, 02:42 PM -
JButton to display JTable
By Nemesis777 in forum New To JavaReplies: 0Last Post: 12-08-2008, 12:16 PM -
Display XML in JTable
By boy22 in forum XMLReplies: 2Last Post: 12-07-2008, 06:03 PM -
[SOLVED] How to load data from the database and display in jTable?
By tpyq in forum NetBeansReplies: 0Last Post: 12-04-2008, 05:39 PM


LinkBack URL
About LinkBacks

Bookmarks