其实很容易,给你个例子看看。/*
 * <applet code = TJTable2 width = 350 height = 100>
 * </applet>
 */import javax.swing.*;
import java.awt.*;
import java.util.*;public class TJTable2 extends JApplet {
    // Create an array of names to be displayed in the table
    // header.
    String[] columnNames = {"Name", "Size (Bytes)", "Date", "Directory"};    // Create an array of row data (each row is an array) to be
    // displayed in the rows of the table.
    Object[][] rowData = {
        {"AUTOEXEC.BAT", "149", "09-11-98", new Boolean(false)},
        {"REAL", "DIR", "12-11-97", new Boolean(true)},
        {"WINDOWS", "DIR", "03-24-97", new Boolean(true)},
        {"COMMAND.COM", "92879", "07-11-97", new Boolean(false)}};    public void init() {
        // 1. Create a table by using the row and column data.
        JTable table = new JTable(rowData, columnNames);        // 2. Add the table column header and the
        // table to the content pane of the applet.
        getContentPane().add(table.getTableHeader(), BorderLayout.NORTH);
        getContentPane().add(table);
    }
}

解决方案 »

  1.   

    Read a data file into a JTable
    The first line of the data file contains the column names. Fields are separated by the "|" character. [customers.dat]
    Id|Name|City|Phone
    102|Beth Reiser|New York|(212)5558725
    111|Dylan Ricci|Syracuse|(315)5554486
    116|Brian Gugliuzza|Mamaroneck|(914)5553817
    120|Gertrude Stein|Elmsford|(914)5553476
    131|Daljit Sinnot|Bohemia|(516)5559811 
    First we need a TableModel to define the data structure to used by the JTable. [DataFileTableModel.java]
    import javax.swing.*;
    import javax.swing.table.*;
    import javax.swing.event.*;
    import java.io.*;
    import java.util.*;public class DataFileTableModel extends AbstractTableModel {
      protected Vector data;
      protected Vector columnNames ;  
      protected String datafile;
      
      public DataFileTableModel(String f){
        datafile = f;
        initVectors();  
        }  public void initVectors() {
        String aLine ;
        data = new Vector();
        columnNames = new Vector();
        try {
          FileInputStream fin =  new FileInputStream(datafile);
          BufferedReader br = new BufferedReader(new InputStreamReader(fin));
          // extract column names
          StringTokenizer st1 = 
             new StringTokenizer(br.readLine(), "|");
            while(st1.hasMoreTokens())
              columnNames.addElement(st1.nextToken());
          // extract data
          while ((aLine = br.readLine()) != null) {  
            StringTokenizer st2 = 
             new StringTokenizer(aLine, "|");
            while(st2.hasMoreTokens())
              data.addElement(st2.nextToken());
            }
          br.close();  
          }
        catch (Exception e) {
          e.printStackTrace();
          }
      }  public int getRowCount() {
        return data.size() / getColumnCount();
        }  public int getColumnCount(){
        return columnNames.size();
        }  public String getColumnName(int columnIndex) {
        String colName = "";    if (columnIndex <= getColumnCount())
           colName = (String)columnNames.elementAt(columnIndex);    return colName;
        }
        
      public Class getColumnClass(int columnIndex){
        return String.class;
        }
        
      public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
        }
        
      public Object getValueAt(int rowIndex, int columnIndex) {
        return (String)data"404.php3" tppabs="http://www.rgagnon.com/javadetails/.elementAt(" (rowIndex * getColumnCount()) + columnIndex);
        }
        
      public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        return;
        }
    }
     
    [DataFileTable.java]
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.io.*;
    import java.util.*;public class DataFileTable extends JPanel {
      public DataFileTable(String dataFilePath) {
        JTable table;
        DataFileTableModel model;
        Font f;    f = new Font("SanSerif",Font.PLAIN,24);
        setFont(f);
        setLayout(new BorderLayout());    model = new DataFileTableModel(dataFilePath);    table = new JTable();
        table.setModel(model);
        table.createDefaultColumnsFromModel();    JScrollPane scrollpane = new JScrollPane(table);
        add(scrollpane);
        } public Dimension getPreferredSize(){
        return new Dimension(400, 300);
        }
        
     public static void main(String s[]) {
        JFrame frame = new JFrame("Data File Table");
        DataFileTable panel;
            
        panel = new DataFileTable("customers.dat");    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        frame.setForeground(Color.black);
        frame.setBackground(Color.lightGray);
        frame.getContentPane().add(panel,"Center");
            
        frame.setSize(panel.getPreferredSize());
        frame.setVisible(true);
        frame.addWindowListener(new WindowCloser());
        }
     }class WindowCloser extends WindowAdapter {
     public void windowClosing(WindowEvent e) {
       Window win = e.getWindow();
       win.setVisible(false);
       System.exit(0);
        }
    }
     
      

  2.   

    问题是在container内实现是没有可以的,但在panel中就不行了,请问如何解决