2014-09-22 110 views
2

我有以下代碼如下:我想防止列頭被重新排序。我不確定該將下面的代碼來做到這一點,還括號我需要的地方(如果有的話):不知道在哪裏把java代碼,以防止列無序

table.getTableHeader().setReorderingAllowed(false); 

我的代碼如下

public class JavaApplication2 extends javax.swing.JFrame{ 

    String driverName = "net.sourceforge.jtds.jdbc.Driver"; 
    String serverName = "xx"; 
    String serverPort = "xx"; 
    String database = serverName + ":" + serverPort; 
    String url = "jdbc:jtds:sqlserver:/" + database; 
    String username = "xx"; 
    String password = "xx"; 

    public JavaApplication2() throws SQLException { 
      ArrayList columnNames = new ArrayList(); 
      ArrayList data = new ArrayList(); 


    try { 

     Class.forName(driverName); 
     Connection connection = DriverManager.getConnection(url, username, password); 

     // Create and execute an SQL statement that returns some data. 
     String SQL = "xx"; 
     Statement stmt = connection.createStatement(); 
     ResultSet rs = stmt.executeQuery(SQL); 
     ResultSetMetaData rsmetadata = rs.getMetaData(); 

     int columns = rsmetadata.getColumnCount(); 


     // Get column names 
      for (int i = 1; i <= columns; i++) 
      { 
      boolean add; 
      add = columnNames.add(rsmetadata.getColumnName(i)); 
      } 

      // Get row data 
      while (rs.next()) 
      { 
       ArrayList row; 
      row = new ArrayList(columns); 

       for (int i = 1; i <= columns; i++) 
       { 
        boolean add; 
        add = row.add(rs.getObject(i)); 
       } 

      boolean add; 
      add = data.add(row); 
      } 
     } 
     catch (SQLException e) 
     { 
      System.out.println(e.getMessage()); 
     } catch (ClassNotFoundException ex) { 
      Logger.getLogger(JavaApplication2.class.getName()).log(Level.SEVERE, null, ex); 
     } 


     Vector columnNamesVector = new Vector(); 
     Vector dataVector = new Vector(); 

     for (int i = 0; i < data.size(); i++) 
     { 
      ArrayList subArray = (ArrayList)data.get(i); 
      Vector subVector = new Vector(); 
      for (int j = 0; j < subArray.size(); j++) 
      { 
       boolean add; 
       add = subVector.add(subArray.get(j)); 
      } 
       boolean add; 
       add = dataVector.add(subVector); 
     } 

    for (int i = 0; i < columnNames.size(); i++){ 
     boolean add ; 
     add = columnNamesVector.add(columnNames.get(i)); 
    } 

     // Create table with database data  
     JTable table; 
      table = new JTable(dataVector, columnNamesVector) 

       //table.getTableHeader().setReorderingAllowed(false) 
        //table.tableHeader.reOrderingAllowed(false); 
      { 



     public Class getColumnClass(int column) 
     { 
      for (int row = 0; row < getRowCount(); row++) 
      { 
       Object o = getValueAt(row, column); 

       if (o != null) 
       { 
        return o.getClass(); 
       } 
      } 

      return Object.class; 
     } 
    }; 


     JScrollPane scrollPane = new JScrollPane(table); 
     getContentPane().add(scrollPane); 

     JPanel buttonPanel = new JPanel(); 
     getContentPane().add(buttonPanel, BorderLayout.SOUTH); 


    } 


} 
+0

不要忘了相關標記添加到你的問題。你沒有爲這個Java question.that就是爲什麼你沒有得到快速的回答中加入Java代碼。 – 2014-09-23 13:22:04

回答

1

你可以打電話table.getTableHeader().setReorderingAllowed(false);定義表後。

在你的代碼中你已經聲明和初始化表。

JTable table; 
    // start of creating table 
    table = new JTable(dataVector, columnNamesVector){ 

     public Class getColumnClass(int column) 
     { 
     for (int row = 0; row < getRowCount(); row++) 
     { 
      Object o = getValueAt(row, column); 

      if (o != null) 
      { 
       return o.getClass(); 
      } 
     } 

     return Object.class; 
     } 
    }; 
    //end of creating table 

,所以你可以調用之後line.in你的代碼中呼籲setReorderingAllowed的地方是不正確的table.getTableHeader().setReorderingAllowed(false);,因爲它是表創建的內部。 public Class getColumnClass方法是在表創建代碼中。

所以完整的代碼是...

JTable table; 

    table = new JTable(dataVector, columnNamesVector){ 

     public Class getColumnClass(int column) 
     { 
     for (int row = 0; row < getRowCount(); row++) 
     { 
      Object o = getValueAt(row, column); 

      if (o != null) 
      { 
       return o.getClass(); 
      } 
     } 

     return Object.class; 
     } 
    }; 

//call setReorderingAllowed here 

table.getTableHeader().setReorderingAllowed(false); 
+0

謝謝你的解釋,我有很多方括號的麻煩,我覺得我永遠不知道把東西放在哪裏。我現在只是測試它,它完美的作品。回覆晚了非常抱歉。 :d – Ingram 2014-09-24 09:01:29