使用文本字段中的输入过滤动态JTable行

时间:2013-11-22 13:41:35

标签: swing

从那天起,我专注于我的问题。我想在文本字段中使用输入过滤动态JTable行。我的表是从DB动态生成的,并在scrollPane中进行了处理。为了更清楚,我必须在我的swing应用程序中显示许多表,所以我使用了JTabbedPane。 这是我的申请代码:

    import java.awt.BorderLayout;

public class test扩展了JFrame {

private JPanel contentPane;
ArrayList<String> elements=new ArrayList<String>();
ArrayList<String> elementsIn=new ArrayList<String>();

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                test frame = new test();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 * @throws SQLException 
 */
public test() throws SQLException {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 1200, 600);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);
    elements.add("product 1");
    elements.add("product 2");
    elements.add("product 3");
    elementsIn.add("in 1");
    elementsIn.add("in 2");
    elementsIn.add("in 3");
    JTabbedPane paneTop=new JTabbedPane();
    paneTop.setBounds(31, 52, 1140, 510);
    Connection con=ConnectDataBaseGeneric("jdbc:jtds:sybase://xxx/xxx","xxx","xxx" ,"net.sourceforge.jtds.jdbc.Driver");
    ResultSet rs= GetResultFromQuery(con,"select * from TCLDTRAMAS");

    //on repace le curseur avant la première ligne


    DefaultTableModel model = buildTableModel(rs);

    con.close();

    for(int i=0;i<elements.size();i++){
        System.out.println("je suis dans la phase I "+ i );
        JPanel panelTop=new JPanel();
        panelTop.setBounds(40,60,1130,500);
        JTabbedPane paneIn=new JTabbedPane();
        paneIn.setBounds(50,100,1125,450);

        for(int y=0;y<elementsIn.size();y++){
            JScrollPane scroll=createJScrollPane(model);
            JPanel panel=createJPanelIn(scroll);
            paneIn.add(elementsIn.get(y),panel);
        }
        panelTop.add(paneIn);
        paneTop.add(elements.get(i),panelTop);
    }

    contentPane.add(paneTop);   


}
public static Connection ConnectDataBaseGeneric(String url,String login, String mdp,String driver){

    try {
        Class.forName(driver);
    }
    catch(ClassNotFoundException ex) {
        System.out.println("Error: unable to load driver class!");
    }
    try {   
        return DriverManager.getConnection(url, login, mdp);
    } catch (SQLException e) {
        e.printStackTrace();
        return null;
    }

}

public static ResultSet GetResultFromQuery(Connection conn,String query,String ...args){
    Statement stmt ;
    //String newQuery=TransformQuery(query,args);
    try {
        stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY,ResultSet.HOLD_CURSORS_OVER_COMMIT);
        //stmt = conn.createStatement();
        return stmt.executeQuery(query);
    } catch (SQLException e) {
        //traitement de l'exception
        e.printStackTrace();
        return null;
    }
}
public static DefaultTableModel buildTableModel(ResultSet rs) throws SQLException {

    ResultSetMetaData metaData = rs.getMetaData();

    // names of columns
    Vector<String> columnNames = new Vector<String>();
    int columnCount = metaData.getColumnCount();
    for (int column = 1; column <= columnCount; column++) {
        columnNames.add(metaData.getColumnName(column));
    }
    // data of the table
    Vector<Vector<Object>> data = new Vector<Vector<Object>>();
    while (rs.next()) {
        Vector<Object> vector = new Vector<Object>();
        for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {
            vector.add(rs.getObject(columnIndex));
        }
        data.add(vector);
    }

    return new DefaultTableModel(data, columnNames);

}
public static JScrollPane createJScrollPane(DefaultTableModel defaultTableModel){

    JScrollPane scrollPane_dataTable = new JScrollPane();

    scrollPane_dataTable.setBounds(63,130,1108,432);
    scrollPane_dataTable.setViewportBorder(null);

    JXTable datatable = new JXTable(defaultTableModel);
    scrollPane_dataTable.setViewportView(datatable);


    return scrollPane_dataTable;
}

public static JPanel createJPanelIn(JScrollPane jScrollPane){

    JPanel panel= new JPanel();
    panel.setBounds(55,120,1115,440);
    panel.add(jScrollPane);

    return panel;
}

}

1 个答案:

答案 0 :(得分:1)

  

我想在文本字段中使用输入过滤动态JTable行。

Sorting and Filtering上的Swing教程中的部分包含一个可以执行此操作的工作示例。

不要忘记查看Swing教程其他部分的目录。