How to manipulate the source code of the GUI builder
(making Check box column)
-----------------------------------------------------------------------------------------
edit
-----------------------------------------------------------------------------------------
I found a better way to make a JTable then the method I used in this post.
I could just code a table directly and drag and drop it on JFrame form.
So, I guess the infomation on this post is useless?
=====================================================
In this post, I will make a table from this
to this
by manipulating the source code, not by the GUI builder
First, lets take a look at the source code of the original table.
public class jtbl_test extends javax.swing.JPanel { /** * Creates new form jtbl_test */ public jtbl_test() { 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") //private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); setLayout(new java.awt.BorderLayout()); 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); add(jScrollPane1, java.awt.BorderLayout.CENTER); }// // Variables declaration - do not modify private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable jTable1; // End of variables declaration }
here, you cannot modify the initComponents() part directly.
Also, it uses .setModel method in the not modifiable area with
the table data and header in it.
So to make it easy to code, I've changed the code as follows
First, you need to set up the model to get rid of this part.
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" } ));
at the very last part of the source code where you can modify, add these
// Variables declaration - do not modify private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable jTable1; // End of variables declaration /* this part defines the data and header of the table */ private Object[][] data = { {"Singed", 2, false}, {"Ashe", 3, true}, {"Riven", 3, false}, {"Vladimir", 2, true} }; private String[] columnNames = {"Name", "Level", "Vegetarian"}; /* this part initializes the model of the table */ private DefaultTableModel model = new DefaultTableModel(data, columnNames) { @Override public Class getColumnClass(int column) { return getValueAt(0, column).getClass(); } };
Now, we have the DefaultTableModel model to replace the
jTable1.setModel(... part.
to do that, go back to design tab, go to navigator tab, and right click the jTable1.
From there, you choose Table contents ...
At the Table Model tab, choose "Custom Code" and put model in the blank
Now, lets see the result.
To see the result, we need a JFrame to display.
Go create JFrame Form as below.
Now, you have a blank JFrame form.
It is time to add the table on it.
Left Click and drag&drop the table on the JFrame.
Then you see the following
Exactly the same as before, right?
The final source code of the table is here
package jtbl_ex; import javax.swing.table.DefaultTableModel; /** * * @author Lee */ public class jtbl_test extends javax.swing.JPanel { /** * Creates new form jtbl_test */ public jtbl_test() { 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") //private void initComponents() { jScrollPane1 = new javax.swing.JScrollPane(); jTable1 = new javax.swing.JTable(); setLayout(new java.awt.BorderLayout()); jTable1.setModel(model); jScrollPane1.setViewportView(jTable1); add(jScrollPane1, java.awt.BorderLayout.CENTER); }// // Variables declaration - do not modify private javax.swing.JScrollPane jScrollPane1; private javax.swing.JTable jTable1; // End of variables declaration /* this part defines the data and header of the table */ private Object[][] data = { {"Singed", 2, false}, {"Ashe", 3, true}, {"Riven", 3, false}, {"Vladimir", 2, true} }; private String[] columnNames = {"Name", "Level", "Vegetarian"}; /* this part initializes the model of the table */ private DefaultTableModel model = new DefaultTableModel(data, columnNames) { @Override public Class getColumnClass(int column) { return getValueAt(0, column).getClass(); } }; }
I like this version much more than the last post one,
because it is easy to read, and easy to modify
Please let me know if there is better way to do this.
So with this and the Oracle How to JTable tutorial,
I could easily create some more interesting tables.
http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
end of JTable study notes
No comments:
Post a Comment