我可以为DefaultTableModel定义大小吗?

时间:2013-03-18 18:43:23

标签: java swing jtable defaulttablemodel

我使用的是模型,我可以刷新表的内容吗?但我把它添加到JPanel,现在我的第二个JPanel,它按下按钮,非常深入。我使用GridLayout来控制列的大小(preferredSize())。一开始我有http://www.funkyimg.com/u2/3264/877/343679Untitled.jpg,然后我必须手动将其设置为http://www.funkyimg.com/u2/3264/878/776480Untitled2.jpg源代码我使用上一篇文章,略有修改:

public class Arsti3 {
JFrame main = new JFrame("Arst");
JPanel tP = new JPanel(new GridLayout(1,0));
JPanel bP = new JPanel();
JButton one = new JButton("Test");
JButton two = new JButton("Two");
JTable table = new JTable();
DefaultTableModel model;
Vector columnNames = new Vector();
Vector data = new Vector();

Arsti3() {
    main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    main.setSize(840,400);
    try {
        reloadData();
        model = new DefaultTableModel(data,columnNames);
        table.setModel(model);

        //table.setRowHeight(24);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        TableColumn col;
        for(int i=0; i<table.getColumnCount(); i++) {
            col = table.getColumnModel().getColumn(i);
            col.setPreferredWidth(100);
            col.setMaxWidth(500);
        }
        //table.setPreferredScrollableViewportSize(new Dimension(500,500));
        //model.fireTableDataChanged();
        tP.add(new JScrollPane(table));
        bP.add(one);
        bP.add(two);
        main.add(tP,BorderLayout.NORTH);
        main.add(bP,BorderLayout.SOUTH);
    } catch(Exception e) {
        e.printStackTrace();
    }
    main.setVisible(true);

}

private void reloadData() throws ClassNotFoundException, SQLException {
    columnNames.clear();
    data.clear();
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    String Base = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)}; DBQ=SL.mdb";
    Connection con = DriverManager.getConnection(Base,"","");
    Statement st = con.createStatement();
    ResultSet res = st.executeQuery("SELECT * FROM Arsti");
    ResultSetMetaData rsmd = res.getMetaData();
    int column = rsmd.getColumnCount();         
    columnNames.addElement("ID");
    columnNames.addElement("Vards");
    columnNames.addElement("Uzvards");
    columnNames.addElement("Dzimums");
    columnNames.addElement("Personas kods");
    columnNames.addElement("Telefona numurs");
    columnNames.addElement("Nodalas ID");
    columnNames.addElement("Amata ID");
    while(res.next()) {
        Vector row = new Vector(column);
        for(int i=1; i<=column; i++) {
            row.addElement(res.getObject(i));
        }
        data.addElement(row);
    }
}

public static void main(String[] args) {
    new Arsti3();
}
}

1 个答案:

答案 0 :(得分:3)

赞赏使用layout,但不要忘记pack()封闭的Window。此外,

  • setPreferredScrollableViewportSize()有点武断,所以你也可以让Dimension成为某事的整数倍。

  • 应在event dispatch thread构建和操作Swing GUI对象。

  • 使用model.setRowCount(0) 清除您的模型,而不是每次都替换它。

image

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

public class Arsti3 {

    private static final int WIDE = 100;
    private JFrame main = new JFrame("Arst");
    private JPanel tP = new JPanel(new GridLayout());
    private JPanel bP = new JPanel();
    private JButton one = new JButton("One");
    private JButton two = new JButton("Two");
    private Vector<String> columnNames = new Vector<String>();
    private Vector<Object> data = new Vector<Object>();
    private DefaultTableModel model = new DefaultTableModel(data, columnNames);
    private JTable table = new JTable();

    Arsti3() {
        reloadData();
        table.setModel(model);
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        for (int i = 0; i < table.getColumnCount(); i++) {
            TableColumn col = table.getColumnModel().getColumn(i);
            col.setPreferredWidth(WIDE);
            col.setMaxWidth(WIDE * 5);
        }
        table.setPreferredScrollableViewportSize(new Dimension(
            table.getColumnCount() * WIDE, table.getRowHeight() * 16));
        tP.add(new JScrollPane(table));
        bP.add(one);
        bP.add(two);
        main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        main.add(tP, BorderLayout.CENTER);
        main.add(bP, BorderLayout.SOUTH);
        main.pack();
        main.setVisible(true);

    }

    private void reloadData() {
        // could be factored out
        columnNames.clear();
        columnNames.addElement("ID");
        columnNames.addElement("Vards");
        columnNames.addElement("Uzvards");
        columnNames.addElement("Dzimums");
        columnNames.addElement("Personas kods");
        columnNames.addElement("Telefona numurs");
        columnNames.addElement("Nodalas ID");
        columnNames.addElement("Amata ID");
        model.setRowCount(0); // clear rows
        // add rows here
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new Arsti3();
            }
        });
    }
}