2013-11-22 59 views
1

自從yeseterday以來,我專注於自己的問題。我想在文本字段中輸入一個動態的JTable行。我的表是從DB動態生成的,並在scrollPane中進行傳輸。更清楚的是,我必須在我的swing應用程序中顯示很多表格,所以我使用了JTabbedPane。 這裏是我的應用程序的代碼:在文本字段中輸入一個動態的JTable行

import java.awt.BorderLayout; 

公共類測試擴展的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

我想在一個文本字段篩選動態JTable中一行輸入。

來自Swing教程Sorting and Filtering的部分包含一個工作示例。

不要忘記查看Swing教程其他部分的目錄。