Results 1 to 4 of 4

Thread: JAvA: how to create a very simple JTable ?

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2003
    Posts
    56

    JAvA: how to create a very simple JTable ?

    i am writing a program that requires the data be outputted in a table style

    so i thought to use the JTable. Ive searched up the web for examples, but all i could find is one that requires you to create a class and then call that class to make the table.

    i dont want that, i just want to create a table simply, which i can load in a 2 Dimensional array. eg 3 columns, and lots of rows.

    anyone?

  2. #2
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Is this a DOS based program? Is the Data going to be outputted to a file or to the user via a GUI? A JTable would be best but you could just use a multidimensional array if you wanted to. Problem is that when using arrays to present data you always end up using tabs '\t' to line up the data correctly when is a pain in the A$$.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2003
    Posts
    56
    im using an applet.
    and the data must be shown in a table.
    so i want to use a jtable but dont know how.

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Ok. So you do want a JTable. Ill let you worry about the Applet part.
    Code:
     
     import java.awt.*; 
     import java.awt.event.*;
     import javax.swing.*; 
     
     class G{
      public static void main(String[] args){
    
      JFrame jf = new JFrame("Inventory"); 
      Container c = jf.getContentPane();
      
      Object[] colnames = {"zooks", "zanks", "dips", "doodles"};
     
      Object[][] data = {
       {"Red", null, new Integer(50), new Integer(23)}, 
       {"Blue", new Integer(25), null , null}, 
       {"Purple", new Integer(74), new Integer(98), null},
       {"Green", new Integer(16), new Integer(72), null}, 
       {"Yellow", new Integer(16), new Integer(72), null},
       {"Black", null, new Integer(54), null},
       {"Brown", null, new Integer(50), new Integer(23)}, 
       {"Teal", new Integer(25), null , null}, 
       {"Aqua", new Integer(74), new Integer(98), null},
       {"Peach", new Integer(16), new Integer(72), null}, 
       {"Mango", new Integer(16), new Integer(72), null},
       {"Orange", null, new Integer(54), null},          
      }; 
    
      jf.addWindowListener(new WindowAdapter(){
       public void windowClosing(WindowEvent we){
        System.exit(0);
       }
      });
    
      JTable jt = new JTable(data, colnames);
      JScrollPane scrollableTable = new JScrollPane(jt); 
      c.add(scrollableTable);
      jf.setSize(500, 150);  
      jf.setVisible(true); 
      
     }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width