JTable fill data with JComboBox

偶尔善良 提交于 2019-11-29 18:01:06
String sql="SELECT * FROM "+tesztvalt+""; 

PreparedStatement pst = conn.prepareStatement(sql);
ResultSet rs = pst.executeQuery();

while (rs.next()) 
{
    tabla.setModel(model);
}

You have a couple of problems:

  1. You are using the PreparedStatement incorrectly. Your code may work (I'm not sure), but it does not take advantage of the features of the PreparedStatement.

  2. The code that read the data from the ResultSet makes no sense because, well you aren't even reading any data from the ResultSet.

To use a PreparedStatement the code is something like:

String sql = "Select * from ?";
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setString( 1, tesztvalt );
stmt.executeQuery();

Now the PreparedStatement will build the SQL query with the proper delimiters so you don't need to worry about that.

If you want to read all the data from a specific table, then check out the TableFromDatabaseExample.java code found in Table From Database. It shows how to build the query as well as how to access the column names and data.

This is a slightly different way, but it is simple. When you select an item from the combo box, the table changes. It uses a table model:

//ComboTableTest.java
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class ComboTableTest extends JFrame implements ActionListener {
    private static final long serialVersionUID = -3898736145211708745L;

    private JComboBox<String> combo;
    private MyTableModel tableModel;

    //constructor
    public ComboTableTest() {
        //set up frame
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(600,175);
        setLayout(new FlowLayout());

        //create combo box
        combo = new JComboBox<String>(new String[]{"one", "two", "three"});
        combo.addActionListener(this);
        add(combo);

        //create table
        tableModel = new MyTableModel();
        final JTable table = new JTable(tableModel);
        table.setPreferredScrollableViewportSize(new Dimension(500, 70));
        table.setFillsViewportHeight(true);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == combo) {
            tableModel.setValueAt(0,0,(String)combo.getSelectedItem());
            tableModel.setValueAt(1,1,(String)combo.getSelectedItem());
            tableModel.setValueAt(2,2,(String)combo.getSelectedItem());
        }

    }


    /*** MAIN ***/
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ComboTableTest frame = new ComboTableTest();
                    frame.setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

class MyTableModel extends AbstractTableModel {
    private static final long serialVersionUID = -1359557492205432915L;

    private String[] columnNames = {"Col 1","Col 2","Col 3"};
    private Object[][] data = new Object[][]{{"1","2","3"},{"4","5","6"},{"7","8","9"}};

    public void setValueAt(int row, int col, String s) {
        data[row][col] = s;
        fireTableDataChanged(); //EDITED
    }

    public int getColumnCount() {
        return columnNames.length;
    }

    public int getRowCount() {
        return data.length;
    }

    public String getColumnName(int col) {
        return columnNames[col];
    }

    public Object getValueAt(int row, int col) {
        return data[row][col];
    }

    public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
}
Robert3452

I think the best and most common approach is to have all the data in the JTable and use a filter which is based on the combobox selection.

In this question someone has done this based on a Text box. Changing for a combo would be simple.

How can I filter rows in a JTable?

Or for a tutorial look at http://docs.oracle.com/javase/tutorial/uiswing/components/table.html and select "Sorting and Filtering"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!