为JTable使用矢量[英] Using vector for JTable

本文是小编为大家收集整理的关于为JTable使用矢量的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

现在我只使用JList,但是如果数据显示在JTable中,则看起来更好. 这是GUI类:

public class Table extends JFrame {

private JPanel contentPane;
DefaultTableModel tableModel = new DefaultTableModel();
private JTable table;
private Controller controller = new Controller();
private static Vector <Vector<String>> data = new Vector <Vector<String>>();


/**
 * Launch the application.
 */


public Table() throws SQLException {

    Vector <String> columnNames = new Vector <String>();
    ResultSet rs2 = controller.getPerson();
    while (rs2.next()){
    columnNames.add(rs2.getString(1));
    }
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

            //add tableModel into table
    table = new JTable(tableModel);

    contentPane.add(table, BorderLayout.CENTER);
            //Create JButton
    JButton showBtn = new JButton("Show");
    showBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
        ResultSet rs = controller.getPerson();
        while (rs.next()){
        Vector <String> vstring = new Vector <String>();
            vstring.add(rs.getString(1));
            data.add(vstring);

                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
    contentPane.add(showBtn, BorderLayout.SOUTH);
}}

单击按钮时,数据应在JTable中显示.我真的不知道如何通过数据库中的列和行循环,然后将此信息添加到JTable中.

推荐答案

JDBCAdapter,它扩展了 AbstractTableModel AbstractTableModel ,是研究的一个很好的例子.它可以在samples/demo/jfc/TableExample中找到,在 em> Java SE开发套件7U45演示和样品下载 .

其他推荐答案

//全局声明

private Vector<Vector<String>> data; //used for data from database
private Vector<String> header; //used to store data header

//显示信息到jtable

    data = get();

    //create header for the table
    header = new Vector<String>();
    header.add("Column1"); 
    header.add("Column2");
    ...
    model=new DefaultTableModel(data,header);
    table = new JTable(model);

这将帮助您从数据库获取数据

get(){
Vector<Vector<String>> doublevector = new Vector<Vector<String>>();

Connection conn = dbConnection();//Your Database connection code
PreparedStatement pre1 = conn.prepareStatement("select * from Table");

ResultSet rs1 = pre1.executeQuery();
while(rs1.next())
{
Vector<String> singlevector = new Vector<String>();
singlevector.add(rs1.getString(1)); 
singlevector.add(rs1.getString(2)); 
....
doublevector.add(singlevector);

}

return doublevector;
}

本文地址:https://www.itbaoku.cn/post/597359.html

问题描述

Right now I just use JList, but it looks much better if the data is shown in a JTable. This is the GUI class:

public class Table extends JFrame {

private JPanel contentPane;
DefaultTableModel tableModel = new DefaultTableModel();
private JTable table;
private Controller controller = new Controller();
private static Vector <Vector<String>> data = new Vector <Vector<String>>();


/**
 * Launch the application.
 */


public Table() throws SQLException {

    Vector <String> columnNames = new Vector <String>();
    ResultSet rs2 = controller.getPerson();
    while (rs2.next()){
    columnNames.add(rs2.getString(1));
    }
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

            //add tableModel into table
    table = new JTable(tableModel);

    contentPane.add(table, BorderLayout.CENTER);
            //Create JButton
    JButton showBtn = new JButton("Show");
    showBtn.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            try {
        ResultSet rs = controller.getPerson();
        while (rs.next()){
        Vector <String> vstring = new Vector <String>();
            vstring.add(rs.getString(1));
            data.add(vstring);

                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });
    contentPane.add(showBtn, BorderLayout.SOUTH);
}}

When the button is clicked the data should be shown in the JTable. I don't really know how to loop through the columns and rows in the database and then add this info to the JTable.

推荐答案

JDBCAdapter, which extends AbstractTableModel, is a good example for study. It may be found in samples/demo/jfc/TableExample, found among the Java SE Development Kit 7u45 Demos and Samples Downloads.

其他推荐答案

//Global Declaration

private Vector<Vector<String>> data; //used for data from database
private Vector<String> header; //used to store data header

//Display info to JTable

    data = get();

    //create header for the table
    header = new Vector<String>();
    header.add("Column1"); 
    header.add("Column2");
    ...
    model=new DefaultTableModel(data,header);
    table = new JTable(model);

This will help you to get data from database

get(){
Vector<Vector<String>> doublevector = new Vector<Vector<String>>();

Connection conn = dbConnection();//Your Database connection code
PreparedStatement pre1 = conn.prepareStatement("select * from Table");

ResultSet rs1 = pre1.executeQuery();
while(rs1.next())
{
Vector<String> singlevector = new Vector<String>();
singlevector.add(rs1.getString(1)); 
singlevector.add(rs1.getString(2)); 
....
doublevector.add(singlevector);

}

return doublevector;
}