JTable单元不可编辑但可单击 [英] JTable cell not editable but clickable
问题描述
我想在有人单击某个单元格时采取措施.例如,打开另一个GUI.但是,如何使单元格可点击但不可编辑?这些是sql查询的结果.我无法使表格不可编辑.我需要听众或其他东西吗?如果是的话,我应该放在哪里?
I want to put an action whenever someone clicks a cell. open another gui for example. But how do i make a cell clickable BUT not editable? These are results for an sql query. I can't manage to make the table uneditable though. Do I need a listener or something? and if yes where should I put it?
这是我的代码:
public class AllResultsFromDB extends JFrame
{
GUI ins = new GUI();
public AllResultsFromDB(GUI x)
{
Vector columnNames = new Vector();
Vector data = new Vector();
this.ins = x;
try
{
// Initializing GUI class in order to call getSelectedTable() method.
// GUI ins = new GUI();
//System.out.println(ins.getSelectedTable());
Login sgui = new Login();
String dburl = "jdbc:oracle:thin:@localhost:1521:ORCL";
Connection connection = DriverManager.getConnection( dburl, sgui.getUsername(), sgui.getPassword() );
// Fetch data from table specified by user
String query = "SELECT * FROM " + ins.getSelectedTable() + " ORDER BY id";
System.out.println(query);
Statement stmt = connection.createStatement();
ResultSet rset = stmt.executeQuery(query);
ResultSetMetaData metad = rset.getMetaData();
int columns = metad.getColumnCount();
// This loop gets the names of the columns
for (int i = 1; i <= columns; i++)
{
columnNames.addElement( metad.getColumnName(i) );
//columnNames.addElement("PROFILES");
}
// This loop gets the data inside the rows
while (rset.next())
{
Vector row = new Vector(columns);
//Vector b = new Vector((Collection)button);
for (int i = 1; i <= columns; i++)
{
row.addElement( rset.getObject(i) );
}
data.addElement( row );
//data.addElement(b);
}
rset.close();
stmt.close();
connection.close();
// Create table with results
JTable table = new JTable(data, columnNames)
{
public Class getColumnClass(int column)
{
for (int row = 0; row < getRowCount(); row++)
{
Object obj = getValueAt(row, column);
if (obj != null)
{
return obj.getClass();
}
}
return Object.class;
}
};
JScrollPane scroll = new JScrollPane( table );
getContentPane().add( scroll );
//table.addMouseListener(l);
//table.setEnabled(false);
//table.setDragEnabled(true);
JPanel panel = new JPanel();
getContentPane().add( panel, BorderLayout.SOUTH );
} catch (SQLException e) {
}
}
}
推荐答案
首先查看如何使用表格
TableModel
确定单元格的isCellEditable
方法是否可编辑.此方法应返回false
The isCellEditable
method the TableModel
determines of a cell is editable or not. This method should return false
当您直接将列/数据信息提供给JTable
时,JTable
会在内部创建一个DefaultTableModel
.此类的isCellEditiable
方法将默认返回true
.
When you supply column/data information to the JTable
directly, the JTable
creates a DefaultTableModel
internally. This class's isCellEditiable
method will return true
by default.
通过使用DefaultTableModel
之类的方法,您可以覆盖此方法而不会带来太多麻烦,并且可以将模型直接设置为表.
By using something like DefaultTableModel
, you can override this method without with to much trouble and set the model to the table directly.
接下来,您需要在表上附加一个MouseListener
Next, you need to attach a MouseListener
to the table
然后,您可以使用 getSelectedColumn , getSelectedRow 获取选定的单元格.
You can then use getSelectedColumn, getSelectedRow to get the selected cell.
您还需要使用 convertColumnIndexToModel 在视图索引和模型索引之间进行转换
You'll also need to use convertRowIndexToModel and convertColumnIndexToModel to convert between the view and model indices
这篇关于JTable单元不可编辑但可单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!