我有以下代碼如下:我想防止列頭被重新排序。我不確定該將下面的代碼來做到這一點,還括號我需要的地方(如果有的話):不知道在哪裏把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);
}
}
不要忘了相關標記添加到你的問題。你沒有爲這個Java question.that就是爲什麼你沒有得到快速的回答中加入Java代碼。 – 2014-09-23 13:22:04