-1

我正在使用模型,我可以刷新表格的内容吗?但我将它添加到 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();
}
}
4

1 回答 1

3

使用布局的荣誉,但不要忘记pack()封闭Window。还,

  • setPreferredScrollableViewportSize()有点随意,所以你也可以做某事Dimension的整数倍。

  • Swing GUI 对象应该事件分派线程上构建和操作。

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

图片

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();
            }
        });
    }
}
于 2013-03-19T00:55:58.303 回答