CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 1999
    Location
    usa
    Posts
    33

    populating jtable by using vector .

    below is my code.
    how do i implement adding rows dynamically to the jtable using vector. ( i know it is vector.addElement something... But what is the actual code ? )

    Thanks


    import java.util.Vector;
    import java.sql.*;
    import OraDB;
    import javax.swing.*;
    import javax.swing.table.AbstractTableModel;
    //import javax.swing.event.TableModelEvent;
    import java.awt.*;
    import java.awt.event.*;

    public class demotable extends JPanel {
    JPanel subpanel = new JPanel();
    JLabel lbTitle = new JLabel("Registration Of Foreign Worker By Job Type and Sex");
    JLabel lbTop = new JLabel("Statistics taken from 01/07/1999 to 31/07/1999 ");
    JLabel lbLine = new JLabel("Fomema Info Centre Report Printed as of 31/10/1999");
    Vector mdata = new Vector();

    public demotable() {

    String columns[] = {"Employee ID", "First Name",
    "Last Name", "Department"};
    String rows[][] = {
    {"0181", "Bill", "Cat", "Political Candidate"},
    {"0915", "Opus", "Penguin", "Lost and Found"},
    {"9923", "Berke", "Breathed", "Editor"}
    };

    mdata.addElement
    JTable table = new JTable (rows, columns);

    table.getColumnModel().getColumn(1).setPreferredWidth(120);
    table.getColumnModel().getColumn(1).setPreferredWidth(80);
    table.setCellSelectionEnabled(false);
    table.setRowSelectionAllowed(true);
    table.setColumnSelectionAllowed(false);
    table.getTableHeader().setReorderingAllowed(false);
    table.setPreferredScrollableViewportSize(new Dimension(500, 70));
    JScrollPane scrollPane = new JScrollPane(table);

    subpanel.setLayout(new BoxLayout(subpanel,BoxLayout.Y_AXIS));
    subpanel.add(lbTitle);
    subpanel.add(Box.createVerticalStrut(5));
    subpanel.add(lbTop);
    subpanel.add(Box.createVerticalStrut(5));
    subpanel.add(lbLine);
    subpanel.add(Box.createVerticalStrut(5));

    setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    add(subpanel);
    add(scrollPane);

    } // TableDemo ends here


    public static void main(String[] args) {
    JFrame f = new JFrame();
    demotable tab = new demotable();
    f.getContentPane().add(tab);

    f.addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    System.exit(0);
    }
    });
    f.pack();
    f.setVisible(true);

    } // main ends here

    } // TableDemo ends here




    http://members.xoom.com/lookads

  2. #2
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: populating jtable by using vector .


    >
    how do i implement adding rows dynamically to the jtable using vector. ( i know it is vector.addElement something... But what is the actual code ? )



    ((DefaultTableModel)table.getModel()).addRow( v ); // Here v is a vector object.

    // If you like to insert a Row in between two rows , try like this..
    ((DefaultTableModel)table.getModel()).insertRow( nRowIndex , v ); // One row will be inserted after nRowIndex'th row.





    Poochi..


  3. #3
    Join Date
    Oct 1999
    Location
    usa
    Posts
    33

    Re: populating jtable by using vector .

    tableLabels.addElement("Emp Code");
    tableData.addElement("0181");

    JTable table = new JTable (tableData, tableLabels );


    C:\JavaClass>java demotable
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
    at java.util.Vector.elementAt(Vector.java, Compiled Code)
    at javax.swing.table.DefaultTableColumnModel.getColumn(DefaultTableColum
    nModel.java:261)
    at demotable.<init>(demotable.java:34)
    at demotable.main(demotable.java:83)


    how do i add a vector that contains 3 fields to the jtable ?

    i tried using the line below but it gives me some error.

    tableData.addElement("0181","Bill","Cat");



    C:\JavaClass>javac demotable.java
    demotable.java:29: Wrong number of arguments in method.
    tableData.addElement("0181","Bill","Cat");

    so, what's wrong with this vector ?


    http://members.xoom.com/lookads

  4. #4
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: populating jtable by using vector .


    1. Your tableData hast to be vector of vectors.
    2. If you have 2 columns in your JTable , your inner vector has to have atleast 2 objects in it.


    import javax.swing.*;
    import java.util.*;

    public class TableEx{
    public static void main( String[] str ){
    JFrame frame = new JFrame();
    Vector columnNames = new Vector();
    columnNames.addElement( "Column1" );
    columnNames.addElement( "Column2" );

    Vector rowValues = new Vector();
    Vector dataVector = new Vector();
    dataVector.addElement( "Value1" );
    dataVector.addElement( "Value2" );
    rowValues.addElement( dataVector );
    JTable table = new JTable( rowValues , columnNames );
    JScrollPane pane = new JScrollPane(table);
    frame.getContentPane().add( pane );
    frame.setSize( 300,300 );
    frame.setVisible( true );
    }
    }





  5. #5
    Join Date
    Oct 1999
    Location
    usa
    Posts
    33

    how to add another row ?

    import javax.swing.*;
    import java.util.*;

    public class TableEx{
    public static void main( String[] str ){
    JFrame frame = new JFrame();
    Vector columnNames = new Vector();
    columnNames.addElement( "Column1" );
    columnNames.addElement( "Column2" );
    Vector rowValues = new Vector();
    Vector dataVector = new Vector();
    dataVector.addElement( "Value1" );
    dataVector.addElement( "Value2" );
    rowValues.addElement( dataVector );
    JTable table = new JTable( rowValues , columnNames );
    JScrollPane pane = new JScrollPane(table);
    frame.getContentPane().add( pane );
    frame.setSize( 300,300 );
    frame.setVisible( true );
    }

    }




    how to add the second row to the above vector ?

    do i have to create a new vector everytime i want to add another row to rowValues vector ?

    Thanks again.


    http://members.xoom.com/lookads

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

Featured