Reputation: 13
I am completely new to Java and have been set an assignment where we must create a book library system. The table must have 7 columns. however I can only currently get 2 and then the code stops working. in the example below I have tried to get 3 columns although it then also fails. Can anybody spot errors or anything I have missed out? When the table is run instead of data it shows [Ljava.lang.Object;@3dda9c8b
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Mark
*/
public class BookDatabase_frame extends javax.swing.JFrame {
Object [][][] data = null;
String [] columnNames = new String [3];
/**
* Creates new form BookDatabase_frame
*/
public BookDatabase_frame() {
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();
jButton1 = new javax.swing.JButton();
jButton2 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(jTable1);
getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 80, 375, 275));
jButton1.setText("Initiate table");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 40, -1, -1));
jButton2.setText("Add row");
getContentPane().add(jButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 40, -1, -1));
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
columnNames = new String [] {"Name", "Country","age"};
data = new Object [1][2][3];
data [0][0][0]= JOptionPane.showInputDialog("Enter your Name");
data [0][1][0] = JOptionPane.showInputDialog("Enter your country");
data [0][0][2] = JOptionPane.showInputDialog("Enter your age");
jTable1.setModel(new DefaultTableModel(data, columnNames));
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(BookDatabase_frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(BookDatabase_frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(BookDatabase_frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(BookDatabase_frame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new BookDatabase_frame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}
Upvotes: 1
Views: 202
Reputation: 209004
"The table must have 7 columns. however I can only currently get 2 and then the code stops working.
If you want 7 columns, then have the column names array have 7 Strings.
For the rows, you only need single dimension arrays, not a double or a triple. And add each row separately in the actionPerformed
to the model. Just initialize the model with 0 rows and only the column names.
String colNames = {"1", "2", "3", "4", "5", "6", "7"};
DefaultTableModel model = new DefaultTableModel(colNames, 0); <-- 0 rows
....
public void actionPerformed(ActionEvent e){
// get 7 fields of data
Object[] row = {data1, data2, data3, data4, data5, data6, data7};
model.addRow(row);
}
I'm assuming you don't want all the data initially in the JTable, and that you want to be able to add new data every time the button is pressed. So what you need to do is initialize the model and table outside the actionPerformed
, and every time the button is pressed, get all 7 pieces of data, put them into an array, the add that array to the model, which will automatically add another row to the table
From Netbeans GUI Builder
In you actionPerformed
just do something like this
public void actionPerformed(ActionEvent e){
// get 7 fields of data
Object[] row = {data1, data2, data3, data4, data5, data6, data7};
((DefaultTableModel)jTable1.getModel()).addRow(row);
}
Upvotes: 1