还有,谁能解释下面这些传入参数的含义呢?
javax.swing.JTable 
javax.swing.JTable() 
javax.swing.JTable(int, int) 
javax.swing.JTable(java.lang.Object[][], java.lang.Object[]) 
javax.swing.JTable(java.util.Vector, java.util.Vector) 
javax.swing.JTable(javax.swing.table.TableModel) 
javax.swing.JTable(javax.swing.table.TableModel, javax.swing.table.TableColumnModel) 
javax.swing.JTable(javax.swing.table.TableModel, javax.swing.table.TableColumnModel, javax.swing.ListSelectionModel)

解决方案 »

  1.   

    给你看个源码!
    //ShowAuthTab.java
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import java.util.*;
    import java.sql.*;
    public class ShowAuthTab extends JFrame implements ActionListener
    {
    private Container c;
    public JTable table;
    public JLabel label;
    Connection con=null;
    Statement sm=null;//用来执行SQL语句,并取得执行结果
    ResultSet rs=null;//用来封装接收数据库查询结果的对象
        ResultSetMetaData rsMetaData=null; //提供有关ResultSet对象的栏位和属性的信息
    public ShowAuthTab()
    {
    super("woodfire");
    setSize(800,400);
    c=getContentPane();
    c.setLayout(new FlowLayout());
    label=new JLabel("数据显示:");
    try
    {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    }
    catch(ClassNotFoundException e)
    {
    System.out.println("SQLException:"+e.getMessage());
    }
    try
    {
    con=DriverManager.getConnection("jdbc:odbc:bbb");
                    //Connection con=DriverManager.getConnection("jdbc:odbc:bbb");
    sm=con.createStatement();
                    //Statement sm=con.createStatement();
    }
    catch(SQLException e)
    {
     System.out.println("SQLException:"+e.getMessage());
    }
    show();
        getTable();
    }
    public void getTable()
        {
    String query="select * from vote";
    try
    {
    //sm=con.createStatement();
    rs=sm.executeQuery(query);
    //在表格中显示结果
                displayResultSet(rs);
                }
          catch ( SQLException e )
      {
             System.out.println("无法查询");
      }
    }
    public void displayResultSet( ResultSet rs ) throws SQLException
       {
          //定位到达第一条记录
          boolean moreRecords = rs.next();
          //如果没有记录,则提示一条消息
          if ( ! moreRecords )
      {
             JOptionPane.showMessageDialog( this, "※结果集中无记录※" );
             setTitle( "〓 无记录显示 〓" );
             return;
               }
          Vector columnHeads = new Vector();
          Vector rows = new Vector();
          try
      {
             //获取字段的名称
             ResultSetMetaData rsmd = rs.getMetaData();
             for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
                columnHeads.addElement( rsmd.getColumnName( i ) );
             //获取记录集
             do
     {
                rows.addElement( getNextRow( rs, rsmd ) );
                 } while ( rs.next() );
             //在表格中显示查询结果
            table = new JTable( rows, columnHeads );
            JScrollPane scroller = new JScrollPane( table );
    table= new JTable();
    table.setPreferredScrollableViewportSize(new Dimension(700,10));
    JPanel show =new JPanel();
    show.setLayout(new BorderLayout());
    show.add(scroller,BorderLayout.CENTER);
    show.add(label,BorderLayout.NORTH);
    c.add(show);
    c.validate();
          }
          catch ( SQLException sqlex ) {
           sqlex.printStackTrace();
          }
       }
       private Vector getNextRow( ResultSet rs,
                                  ResultSetMetaData rsmd )
           throws SQLException
       {
          Vector currentRow = new Vector();
          for ( int i = 1; i <= rsmd.getColumnCount(); ++i )
             currentRow.addElement( rs.getString( i ) );
          //返回一条记录
          return currentRow;
       }
       public void actionPerformed(ActionEvent e)
    {
    }
     public void shutDown()
       {
          try {
            //断开数据库连接
            sm.close();
    con.close();
            }
          catch ( SQLException sqlex )
      {
             System.err.println( "Unable to disconnect" );
             sqlex.printStackTrace();
              }
        }
    public static void main(String[] args)
    {
      ShowAuthTab s=new ShowAuthTab();
      s.shutDown();
      s.addWindowListener(new WindowAdapter()
    {
      public void windowClosing(WindowEvent e)
    {
      System.exit(0);
      }
         } );
    }
    }
      

  2.   

    参数的含义去看文档吧,
    给个简单的例子,你看看有帮助么。^_^/* (swing1.1beta3)
     *
     * |-----------------------------------------------------|
     * |        |       Name      |         Language         |
     * |        |-----------------|--------------------------|
     * |  SNo.  |        |        |        |      Others     |
     * |        |   1    |    2   | Native |-----------------|
     * |        |        |        |        |   2    |   3    |  
     * |-----------------------------------------------------|
     * |        |        |        |        |        |        |
     *
     */
    //package jp.gr.java_conf.tame.swing.examples;import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.table.*;import jp.gr.java_conf.tame.swing.table.*;
     
    /**
     * @version 1.0 11/09/98
     */
    public class GroupableHeaderExample extends JFrame {  GroupableHeaderExample() {
        super( "Groupable Header Example" );    DefaultTableModel dm = new DefaultTableModel();
        dm.setDataVector(new Object[][]{
          {"119","foo","bar","ja","ko","zh"},
          {"911","bar","foo","en","fr","pt"}},
        new Object[]{"SNo.","1","2","Native","2","3"});    JTable table = new JTable( dm ) {
          protected JTableHeader createDefaultTableHeader() {
    return new GroupableTableHeader(columnModel);
          }
        };
        TableColumnModel cm = table.getColumnModel();
        ColumnGroup g_name = new ColumnGroup("Name");
        g_name.add(cm.getColumn(1));
        g_name.add(cm.getColumn(2));
        ColumnGroup g_lang = new ColumnGroup("Language");
        g_lang.add(cm.getColumn(3));
        ColumnGroup g_other = new ColumnGroup("Others");
        g_other.add(cm.getColumn(4));
        g_other.add(cm.getColumn(5));
        g_lang.add(g_other);
        GroupableTableHeader header = (GroupableTableHeader)table.getTableHeader();
        header.addColumnGroup(g_name);
        header.addColumnGroup(g_lang);
        JScrollPane scroll = new JScrollPane( table );
        getContentPane().add( scroll );
        setSize( 400, 120 );   
      }  public static void main(String[] args) {
        GroupableHeaderExample frame = new GroupableHeaderExample();
        frame.addWindowListener( new WindowAdapter() {
          public void windowClosing( WindowEvent e ) {
    System.exit(0);
          }
        });
        frame.setVisible(true);
      }
    }
      

  3.   

    javax.swing.JTable();
    javax.swing.JTable(int,int);
    ......
    这些都是JTable对象的构造方法,参数不一样,所构造的JTable也不一样
      

  4.   

    想把jTable的所有的东西搞清楚,那不是一时半会儿的事情,需要亲手不断的实现不同种类的jtable功能;所以,你可以先做一个可以实现jtable显示的例子,一个比较小的那种,然后,你可以不断地在上面更改不同的参数,添加不同的model,主要还是你要不断的学习jdk上面的api。
    不懂再来问吧!
      

  5.   

    好好看看jdkdoc嘛。虽然都是英文,但是坚持下来会有不小的收获的。
      

  6.   

    http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#simple
      

  7.   

    http://www2.gol.com/users/tame/swing/examples/JTableExamples1.html
      

  8.   

    public class JTable
    extends JComponent
    implements TableModelListener, Scrollable, TableColumnModelListener, ListSelectionListener, CellEditorListener, Accessible
    The JTable is used to display and edit regular two-dimensional tables of cells. See How to Use Tables in The Java Tutorial for task-oriented documentation and examples of using JTable. The JTable has many facilities that make it possible to customize its rendering and editing but provides defaults for these features so that simple tables can be set up easily. For example, to set up a table with 10 rows and 10 columns of numbers: 
          TableModel dataModel = new AbstractTableModel() {
              public int getColumnCount() { return 10; }
              public int getRowCount() { return 10;}
              public Object getValueAt(int row, int col) { return new Integer(row*col); }
          };
          JTable table = new JTable(dataModel);
          JScrollPane scrollpane = new JScrollPane(table);
     Note that if you wish to use a JTable in a standalone view (outside of a JScrollPane) and want the header displayed, you can get it using getTableHeader() and display it separately. When designing applications that use the JTable it is worth paying close attention to the data structures that will represent the table's data. The DefaultTableModel is a model implementation that uses a Vector of Vectors of Objects to store the cell values. As well as copying the data from an application into the DefaultTableModel, it is also possible to wrap the data in the methods of the TableModel interface so that the data can be passed to the JTable directly, as in the example above. This often results in more efficient applications because the model is free to choose the internal representation that best suits the data. A good rule of thumb for deciding whether to use the AbstractTableModel or the DefaultTableModel is to use the AbstractTableModel as the base class for creating subclasses and the DefaultTableModel when subclassing is not required. The "TableExample" directory in the demo area of the source distribution gives a number of complete examples of JTable usage, covering how the JTable can be used to provide an editable view of data taken from a database and how to modify the columns in the display to use specialized renderers and editors. The JTable uses integers exclusively to refer to both the rows and the columns of the model that it displays. The JTable simply takes a tabular range of cells and uses getValueAt(int, int) to retrieve the values from the model during painting. By default, columns may be rearranged in the JTable so that the view's columns appear in a different order to the columns in the model. This does not affect the implementation of the model at all: when the columns are reordered, the JTable maintains the new order of the columns internally and converts its column indices before querying the model. So, when writing a TableModel, it is not necessary to listen for column reordering events as the model will be queried in its own coordinate system regardless of what is happening in the view. In the examples area there is a demonstration of a sorting algorithm making use of exactly this technique to interpose yet another coordinate system where the order of the rows is changed, rather than the order of the columns. As for all JComponent classes, you can use InputMap and ActionMap to associate an Action object with a KeyStroke and execute the action under specified conditions. For the keyboard keys used by this component in the standard Look and Feel (L&F) renditions, see the JTable key assignments. Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see XMLEncoder. 
      

  9.   

    http://www2.gol.com/users/tame/swing/examples/SwingExamples.html
    去看看吧■
      

  10.   

    楼上的例子不错!
    我也研究过,我自己写的一个TableModel
    用起来不错!不敢独享!
    呵呵//表模型类
       class MyTableModel extends AbstractTableModel {
              
         private ArrayList columnNames = new ArrayList();  //TableModel中的列名称
         private ArrayList dataRows = new ArrayList();          //以行来组织的数据
              
        public  MyTableModel(ArrayList v){
      if (v == null)
      System.out.println(v.size());
      columnNames =(ArrayList)(v.get(0));
                  
                  for(int j=1;j<v.size();j++){
               ArrayList vRow =new ArrayList();
               vRow = (ArrayList)v.get(j);
               dataRows.add(vRow);
               }
             }                        public int getColumnCount() {
                 if(columnNames == null)
                 System.out.println("kong name");
                  return columnNames.size();
                }

                public int getRowCount() {
                 if (dataRows ==null)
                 System.out.println("kong data");
                  return dataRows.size();
                }

                public String getColumnName(int col) {
                  return columnNames.get(col).toString();
                }

               public Object getValueAt(int row, int col) {
                 ArrayList tmp = new ArrayList();
                 tmp = (ArrayList)dataRows.get(row);
                    return tmp.get(col).toString();
                }

          public Class getColumnClass(int c) {  
                                        //依据该方法的返回值来决定使用何种editor
                  return getValueAt(0, c).getClass();
                }

    public boolean isCellEditable(int row, int col) { //定义可编辑的行
                  if (col < 2) {
                    return false;
                  }
                  else {
                    return true;
                  }
                }  public void setValueAt(Object value, int row, int col) {
        //该方法进行单元表格的修改
                 
                  
                   ArrayList tmp = new ArrayList();
                 tmp = (ArrayList)dataRows.get(row);
                 tmp.set(col,value);
                    fireTableCellUpdated(row, col);
                  }

                 
                
                public void addRow(ArrayList data){
                  int RowNums = dataRows.size();
                  dataRows.add(data);
                  fireTableRowsInserted(RowNums,RowNums);
                }
                
                public void insertRow(int row,ArrayList data){
                  dataRows.add(row,data);
                  fireTableRowsInserted(row,row);
                 
                }
                public void deleteRow(int row){
                  int RowNums = dataRows.size();
                  dataRows.remove(row-1);
                  fireTableRowsDeleted(RowNums,RowNums);             
                 }
              }       //end of MyTableModel
      

  11.   

    帮你顶以下
    我有一个问题 如何把表字段内容转到 一个文本框内(textField)
      

  12.   

    JAVA里的TABLE太复杂了,我一直没去做,因为我觉得做个WEB APPLICATION的话用HTML来实现表格,表格数据用JSP来做不就好了吗?高手指点一下.
      

  13.   

    请看一下《Java图形设计:SWING卷》,本书有一章专门介绍JTable.