嘿,我坚持我的程序。实际上我有一个jtable,我希望每次在jtable行上点击一个鼠标时选择特定行和特定coloumn的值... 这是我正在使用的课程:
import java.awt.event.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.JFrame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class jtable extends JFrame implements MouseListener
{
PdfViewer3 pdfViewer3;
String value;
JTable table;
//Vector dataVector;
public jtable() throws ClassNotFoundException
{
ArrayList columnNames = new ArrayList();
ArrayList data = new ArrayList();
// Connect to an MySQL Database, run query, get result set
String url = "jdbc:mysql://localhost:3306/search_imgr_data";
String userid = "root";
String password = "root";
String sql = "SELECT * FROM mission_db limit 70060,5";
// Java SE 7 has try-with-resources
// This will ensure that the sql objects are closed when the program
// is finished with them
try {
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection( url, userid, password );
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery( sql );
ResultSetMetaData md = rs.getMetaData();
int columns = md.getColumnCount();
// Get column names
for (int i = 1; i <= columns; i++)
{
columnNames.add( md.getColumnName(i) );
}
// Get row data
while (rs.next())
{
ArrayList row = new ArrayList(columns);
for (int i = 1; i <= columns; i++)
{
row.add( rs.getObject(i) );
}
data.add( row );
}
}
catch (SQLException e)
{
System.out.println( e.getMessage() );
}
// Create Vectors and copy over elements from ArrayLists to them
// Vector is deprecated but I am using them in this example to keep
// things simple - the best practice would be to create a custom defined
// class which inherits from the AbstractTableModel class
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++)
{
subVector.add(subArray.get(j));
}
dataVector.add(subVector);
}
for (int i = 0; i < columnNames.size(); i++ )
columnNamesVector.add(columnNames.get(i));
// Create table with database data
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;
}
public boolean isCellEditable(int rowIndex, int colIndex) {
return false;
}
};
table.addMouseListener(new MouseAdapter(){
public void MouseClicked(MouseEvent e) {
System.out.println("IN listener");
table.setRowSelectionAllowed(true);
table.setColumnSelectionAllowed(true);
if (e.getClickCount() != 0)
{
JTable target = (JTable)e.getSource();
int row = target.getSelectedRow();
int column = 1;
value = (String)target.getValueAt(row, column);
//value= ((Vector)rowData.elementAt(row)).elementAt(column);
Thread runner = new Thread(){
public void run(){
//pdfViewer3 = new PdfViewer3();
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
pdfViewer3.setVisible(true);}});
pdfViewer3.initComponents1(value);
}
};runner.start();
}
}
});
JScrollPane scrollPane = new JScrollPane( table );
getContentPane().add( scrollPane );
JPanel buttonPanel = new JPanel();
getContentPane().add( buttonPanel, BorderLayout.SOUTH );
}
public static void main(String[] args) throws ClassNotFoundException
{
jtable frame = new jtable();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.pack();
frame.setVisible(true);
}
public void mousePressed(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseReleased(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseEntered(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseExited(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
public void mouseClicked(MouseEvent e) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
答案 0 :(得分:5)
阅读Oracle教程How to use Tables,因为有一些错误
MouseEvents
在第Specifying Tool Tips for Cells部分中有很好的描述,包括工作代码示例
答案 1 :(得分:3)
为什么要将数据库中的数据读入ArrayList,然后将数据复制到Vector?只需将数据直接加载到Vector中即可。
table.setRowSelectionAllowed(true);
table.setColumnSelectionAllowed(true);
以上代码看起来不正确。我会摆脱它。至少不应该在MouseListener中。如果这确实是你想要的行为,那么这些语句应该在你创建表时执行,否则我猜你在单击一个单元格后更改选择属性时会丢失最后的选择值。
public void MouseClicked(MouseEvent e)
或许问题很简单,就是在mouseClicked
方法上使用“M”。它应该是“m”。在重写方法时应该使用@Override注释,以确保不会输入错误。
@Override
public void mouseClicked(MouseEvent e)