Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JTable column with different cell types... how please?

mohsenmadiMay 22 2010 — edited May 24 2010
Hello all -

Is it possible to have a jTable column that is of type Boolean, yet some of its cells are of type String? For example, if you kindly consider the code below, what I would like to accomplish is to have the bottom two cells in the "Skip" column be of type String. If possible, then I could for example display the count of checked boxes in the last cell in the "Skip" column instead of the checkbox currently rendered, even though I didn't explicitly fill it as you see in method loadTable(). In case the solution involves extending the DefaultTableCellRenderer class, I have provided a simple extension that simply justifies the text of columns.

Many thanks in advance.

import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

public class NewJFrame extends javax.swing.JFrame {

   public NewJFrame() {
      initComponents();
      jTable1.setDefaultRenderer(Object.class, new LTRCellRenderer());
      loadTable();
   }

   private void loadTable() {
      DefaultTableModel tm = (DefaultTableModel) jTable1.getModel();

      int len = 7;
      tm.setRowCount(len);

      int sum = 0;
      int freq = 10;
      for (int i = 1; i < len - 1; i++, freq = i * 10) {
         tm.setValueAt(i, i - 1, 0);
         tm.setValueAt(freq, i - 1, 1);
         tm.setValueAt(false, i, 2);
         sum += freq;
      }

      // row before last
      tm.setValueAt("------", len - 2, 0);
      tm.setValueAt("------", len - 2, 1);

      // last row
      tm.setValueAt("Total:", len - 1, 0);
      tm.setValueAt(sum, len - 1, 1);
   }

   class LTRCellRenderer extends DefaultTableCellRenderer {

      @Override
      public Component getTableCellRendererComponent(
              JTable table,
              Object obj,
              boolean isSelected,
              boolean hasFocus,
              int row,
              int column) {

         Component cell = super.getTableCellRendererComponent(
                 table, obj, isSelected, hasFocus, row, column);

         setHorizontalAlignment(LEFT);

         return cell;
      }
   }

   @SuppressWarnings("unchecked")
   private void initComponents() {

      jScrollPane1 = new javax.swing.JScrollPane();
      jTable1 = new javax.swing.JTable();

      setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

      jTable1.setModel(new javax.swing.table.DefaultTableModel(
         new Object [][] {

         },
         new String [] {
            "#", "Frequency", "Skip"
         }
      ) {
         Class[] types = new Class [] {
            java.lang.String.class, java.lang.Integer.class, java.lang.Boolean.class
         };
         boolean[] canEdit = new boolean [] {
            false, false, true
         };

         public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
         }

         public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
         }
      });
      jTable1.getTableHeader().setReorderingAllowed(false);
      jScrollPane1.setViewportView(jTable1);

      javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
      getContentPane().setLayout(layout);
      layout.setHorizontalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGap(0, 213, Short.MAX_VALUE)
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
               .addGap(12, 12, 12)
               .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 191, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
      );
      layout.setVerticalGroup(
         layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
         .addGap(0, 186, Short.MAX_VALUE)
         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
               .addGap(12, 12, 12)
               .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
               .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
      );

      pack();
   }

   public static void main(String args[]) {
      java.awt.EventQueue.invokeLater(new Runnable() {

         public void run() {
            new NewJFrame().setVisible(true);
         }
      });
   }
   private javax.swing.JScrollPane jScrollPane1;
   private javax.swing.JTable jTable1;
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 21 2010
Added on May 22 2010
7 comments
339 views