gpt4 book ai didi

java - isCellEditable 在 Swing 中不起作用

转载 作者:行者123 更新时间:2023-11-30 06:34:41 25 4
gpt4 key购买 nike

我想编辑一个特定的单元格,因此我在此之后编辑 isCellEditable 方法,当我调用 isCellEditable 组件时,它的编辑值为 true,但它不会编辑那个特别的。请帮帮我。按原样将行值和列值传递为 0 和 0。我应该怎样做才能使特定单元格可编辑?

package javaapplication2;

import javax.swing.table.DefaultTableModel;

/**
*
* @author Himanshu
*/
public class NewJFrame extends javax.swing.JFrame {
public boolean edit = false;
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
}

/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTable1 = new javax.swing.JTable();
jButton2 = new javax.swing.JButton();
jButton1 = new javax.swing.JButton();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jPanel1.setLayout(new java.awt.GridLayout(3, 1));

jTable1.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
) {
Class[] types = new Class [] {
java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
};
boolean[] canEdit = new boolean [] {
false, false, false, false
};

public Class getColumnClass(int columnIndex) {
return types [columnIndex];
}
public boolean isCellEditable(int rowIndex, int columnIndex) {
if(edit == true){
return true;
}
else{
return false;
}
}
}
);
jScrollPane1.setViewportView(jTable1);

jPanel1.add(jScrollPane1);

jButton2.setText("jButton2");
jButton2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton2ActionPerformed(evt);
}
});
jPanel1.add(jButton2);

jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
jPanel1.add(jButton1);

jTabbedPane1.addTab("tab1", jPanel1);

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 771, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 432, Short.MAX_VALUE)
);

pack();
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}

/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>

/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
NewJFrame n = new NewJFrame();
n.initial();
}
public void initial(){

// String [][] data = new String[][]{{"1","2","3","4"},{"1","2","3","4"},{"1","2","3","4" }};
DefaultTableModel model = (DefaultTableModel) jTable1.getModel();
edit = true;
model.isCellEditable(0, 0);

}

// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JButton jButton2;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JTable jTable1;
// End of variables declaration
}

最佳答案

你应该这样做

    java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
final NewJFrame frame = new NewJFrame();
frame.initial();
frame.setVisible(true);
}
});
/* NewJFrame n = new NewJFrame();
n.initial();*/ // delete the commented code

您之前的代码新建了两个框架,一个可编辑但不可见 n ,一个可见但不可编辑。

<小时/>

因为只有(0,0)是可编辑的,所以调用isCellEditable方法是没有用的,它是纯粹的,没有副作用,只是返回一个 boolean 值。

您可以将 isCellEditable 方法的实现更改为

 public boolean isCellEditable(int rowIndex, int columnIndex) {
return edit && (rowIndex == 0 && columnIndex == 0);
}
<小时/>

当然,您始终可以选择不对数字进行硬编码,并编写诸如 setEditableCell 之类的方法,并将其更改为 return edit && (rowIndex == rowNum && columnIndex == columnNum);

关于java - isCellEditable 在 Swing 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45401785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com