不知道在哪里放置java代码以防止列混乱 [英] Not sure where to put the java code to prevent column disordering
问题描述
我有以下代码:我想阻止重新排序列标题。我不确定在哪里放置以下代码来执行此操作以及我需要放置的括号(如果有):
I have the following code below: I want to prevent the the column headers from being reordered. I am unsure where to place the following code to do this and also the brackets i need to place (if any):
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 );
}
}
推荐答案
你可以在定义你的表后调用 table.getTableHeader()。setReorderingAllowed(false);
。
you can call table.getTableHeader().setReorderingAllowed(false);
after define your table.
您声明并初始化表格的代码。
in your code you have declared and initialized table.
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
所以你可以调用表.getTableHeader()。setReorderingAllowed(false);
在你的代码之后,你调用setReorderingAllowed的地方是不正确的,因为它在表创建中。 public Class getColumnClass
方法在表格内部创建代码。
so you can call table.getTableHeader().setReorderingAllowed(false);
after that line.in your code the place you have called setReorderingAllowed is not correct because it's inside of table creating .public Class getColumnClass
method is inside of table creating code.
所以完整代码是......
so complete code is...
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);
这篇关于不知道在哪里放置java代码以防止列混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!