我有java jtable,我想在它上面使用鼠标双击事件,我有很多代码可以将监听器粘贴到表中,无法坚持使用我的代码并且在这里阅读了数十个监听器代码在那里,我是Java的新手,需要更多的耐心和简单的直接帮助以及一些专业的内部堆栈不能提供或承担这些基本问题,我感觉到同样的原因,因为过去曾多次尝试解释一些昏暗的代码给我,我只是作为帮助?希望这次我能幸运! 最后所有avaialble监听器代码都在堆栈中使用来自设计器的表,而不是与我的代码一起创建的代码不知道为什么这可能是我的主要问题。
public class JFband extends javax.swing.JFrame {
Connection con1;
Statement stmt1;
ResultSet rs1;
Connection con;
Statement stmt;
ResultSet rs;
String fLag;
DefaultTableModel tableModel;
static String Bno;
JTable jTnameSearch = new JTable(tableModel);
private TableRowSorter<TableModel> rowSorter
= new TableRowSorter<>();
// declare sorter to sort JTable data based on jTsearch Box field entry
public JFband() {
this.setLayout(null);
// here is the code with error :
jTnameSearch.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e){
if (e.getClickCount() == 2){
JOptionPane.showMessageDialog(null, "the double click code is working");
}
}
} );
}
}
// listener for my jTable
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
public void LoadTable(){
try {
String host1= "jdbc:derby://localhost:1527/accountingsystem";
String uName1="adamin";
String uPass1="1234a1234b";
String sql1;
con1=DriverManager.getConnection(host1,uName1 ,uPass1);
//String sql ="select * from log where password= jTuser.getText() " ;
if (fLag.equals("search"))
sql1="select band_name,band_no ,band_type from band order by band_no ";
else
sql1="select band_name,band_no ,band_type from band where band_name='" +filterText.getText()+ "' order by band_no ";
stmt1=con1.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
rs=stmt1.executeQuery(sql1);
int jCounter=0;
while (rs.next()){ jCounter++;
}
if (jCounter==0)
return;
rs.beforeFirst();
String[] columnnames = {"اسم الحساب","رقم الحساب","نوع الحساب"};
String Data[][]=new String[jCounter][3];
int j=0;
while (rs.next()) {
// JOptionPane.showMessageDialog(null, i+"فاصل "+ jCounter);
Data[j][0]=rs.getString("band_name");
Data[j][1]= rs.getString("band_no");
Data[j][2]= rs.getString("band_type");
++j;
}
// DefaultTableModel tableModel;
tableModel = new DefaultTableModel(Data,columnnames);
jTnameSearch = new JTable(tableModel) { // القوس هذا جديد اضيف لعمل شرط عدم امكانية الكتابة
// في الجدول اصبح خمسة سطر
public boolean isCellEditable(int row, int col)
{
return false;
}
};
// انتهت دالة منع الكتابة
//only single selection mode
jTnameSearch.setSelectionMode(0);
rowSorter = new TableRowSorter<>(jTnameSearch.getModel());
jTnameSearch.setRowSorter(rowSorter);
jSpane3.setViewportView(jTnameSearch);
jTnameSearch.setFillsViewportHeight(true);
//jTnameSearch.setAutoCreateRowSorter(true); // enable sort and filter
jPsearch.setVisible(true);
jPanel2.setVisible(false);
filterText.requestFocus();
rs.close();
fLag="text";
}
catch (SQLException ex) {
System.out.println(ex);
}
}
}
答案 0 :(得分:1)
我发了一个附加到jtable的点击事件:
int ClickCount=0,selectedTodelete=-1;
private void JTArticleJPAddArrticleMouseClicked(java.awt.event.MouseEvent evt) {
if (ClickCount==1) {
// your double click section code here
return;
}else{
if (selectedTodelete==JTArticleJPAddArrticle.getSelectedRow()) {
ClickCount++;
//here we are checking that the event is on the same row (double click on the same row)
}else{
ClickCount=0;
selectedTodelete=JTArticleJPAddArrticle.getSelectedRow();
// the new cell selected
}
}
//one click event code here
}
我在任何地方制作双击事件的方式对我来说非常有用
表的实例化代码可以帮助你理解为什么不适合你
JTArticleJPAddArrticle = new javax.swing.JTable();
JTArticleJPAddArrticle.setBackground(new java.awt.Color(204, 204, 255));
JTArticleJPAddArrticle.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
},
new String [] {
"Reference","Libellé","Marque","Type","Prix d'achat","Prix de vente","Quantité","Total","Etat"
}
){
boolean[] canEdit = new boolean [] {
false, false, false, false
};
public boolean isCellEditable(int rowIndex, int columnIndex) {
return false;
}
});
JTArticleJPAddArrticle.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(java.awt.event.MouseEvent evt) {
JTArticleJPAddArrticleMouseClicked(evt);// that a function called in the event mouseClicked
}
});
jScrollPane8.setViewportView(JTArticleJPAddArrticle);