How to create a table with rows and colums


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: How to create a table with rows and colums

  1. #1
    Join Date
    Feb 2005
    Posts
    24

    Red face How to create a table with rows and colums

    Hi everyone

    I want to create a table with 3 rows and 5 colums how can I go about doing this if anyone can show me an example in detail I would be very greatefull...
    Thank you

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Here is the -how-to-boil-an-egg example

    Code:
    import javax.swing.*;
    import java.awt.*;
    import com.borland.dbswing.*;
    import javax.swing.table.*;
    import java.util.EventObject;
    import javax.swing.event.CellEditorListener;
    
    /**
     *
     * @author sjalle
     * @version 1.0
     */
    class ATableClass extends JTable {
      public Class getColumnClass(int column) {
        try {
          if (column == 0) {
            return Class.forName("java.lang.Boolean");
          } else if (column == 1) {
            return Class.forName("java.lang.Integer");
          }
    
          return Class.forName("java.lang.Object");
        }
        catch (ClassNotFoundException ex) {
          ex.printStackTrace();
          return null;
        }
      }
    }
    public class TableExample1 extends JFrame {
      JScrollPane jScrollPane1 = new JScrollPane();
      ATableClass tableOne = new ATableClass();
      DefaultTableModel model  = new DefaultTableModel();
      JCheckBox cbx=new JCheckBox();
      String [] sArray =new String [] {
        "lalala","lala"
      };
    
      Integer [] intArray = new Integer[] {
          new Integer(2),
          new Integer(77),
          new Integer(34),
          new Integer(5)
      };
      Boolean [] boolArray=new Boolean[] {
          new Boolean(true),
          new Boolean(false),
          new Boolean(true),
          new Boolean(false),
          new Boolean(true),
          new Boolean(false)
      };
    
      public TableExample1() {
        try {
          jbInit();
    
          tableOne.setModel(model);
          model.addColumn("CbxColumn", boolArray);
          model.addColumn("IDs", intArray);
          model.addColumn("Names", sArray);
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }
      public static void main(String[] args) {
        TableExample1 tex = new TableExample1();
        tex.setBounds(10,10,300,200);
        tex.setVisible(true);
      }
      private void jbInit() throws Exception {
        this.getContentPane().add(jScrollPane1, BorderLayout.CENTER);
        jScrollPane1.getViewport().add(tableOne, null);
      }
    }
    eschew obfuscation

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center