不知道在哪里放置java代码以防止列混乱

时间:2014-09-22 20:36:46

标签: 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 );


    }


}

1 个答案:

答案 0 :(得分: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

因此您可以在该行之后调用table.getTableHeader().setReorderingAllowed(false);。在您的代码中,您调用setReorderingAllowed的地方不正确,因为它在表创建中。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);