双击编辑 Jtable
Edit Jtable on double click
我需要在用户双击行时用新值编辑 table。我尝试使用 setValuesAt
方法,但它没有做任何事情。
这是我目前得到的结果,但我不知道如何将新值设置为 Jtable
。
public class Table extends JFrame {
private JTable table;
private JScrollPane jsPane;
private DefaultTableModel model;
private JPanel dialogPanel;
private JTextField tf[];
private JLabel lbl[];
private JPanel panel;
Object[] columns = new Object[]{"Name", "Last Name", "ID", "Email"};
Object[][] inData ;
public void prepareAndShowGUI() {
setTitle("Overview");
model = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
model.setColumnIdentifiers(columns);
table = new JTable(model);
for (int i = 1; i <= 10; i++) {
model.addRow(new Object[]{"a", "s", "w", "e"});
}
jsPane = new JScrollPane(table);
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
int x = e.getX();
int y = e.getY();
int row = table.rowAtPoint(new Point(x, y));
int col = table.columnAtPoint(new Point(x, y));
String array[] = new String[table.getColumnCount()];
for (int i = 0; i < array.length; i++) {
array[i] = (String) table.getValueAt(row, i);
}
populateTextField(array);
JOptionPane.showMessageDialog(null, dialogPanel, "Information", JOptionPane.INFORMATION_MESSAGE);
String[] values = new String[tf.length];
for (int i = 0; i < tf.length; i++) {
values[i] = tf[i].getText();
}
model.setValueAt(values, row, row);
}
}
});
panel = new JPanel(new BorderLayout());
JPanel panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
panel.add(panel1, BorderLayout.NORTH);
panel.add(jsPane, BorderLayout.CENTER);
getContentPane().add(panel);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
prepareDialogPanel();
setVisible(true);
}
private void prepareDialogPanel() {
dialogPanel = new JPanel();
int col = table.getColumnCount();
dialogPanel.setLayout(new GridLayout(col, 1));
tf = new JTextField[col];
lbl = new JLabel[col];
for (int i = 0; i < col; i++) {
lbl[i] = new JLabel(table.getColumnName(i));
tf[i] = new JTextField(10);
dialogPanel.add(lbl[i]);
dialogPanel.add(tf[i]);
}
}
private void populateTextField(String[] s) {
for (int i = 0; i < s.length; i++) {
tf[i].setText(s[i]);
}
}
}
public static void main(String st[])
{
SwingUtilities.invokeLater( new Runnable()
{
@Override
public void run()
{
Table td = new Table();
td.prepareAndShowGUI();
}
});
}
对于初学者,您应该使您的模型可编辑,因此更改此行:
model = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
收件人:
model = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
return true; //use the row and col to determine which
//cells are editable. If you want all, have this return true.
}
};
这将调用 JTable 的 DefaultCellEditor,当用户对编辑进行更改时,它将调用模型的 setValueAt 方法。
所有这些组件都可以替换为自定义组件以执行不同的操作。
这是关于 JTables 的官方 Oracle 文档:
https://docs.oracle.com/javase/tutorial/uiswing/components/table.html
如果您想让对话框正常工作,问题出在这些行中:
String[] values = new String[tf.length];
for (int i = 0; i < tf.length; i++) {
values[i] = tf[i].getText();
}
model.setValueAt(values, row, row);
基本上 setValueAt 只能在逐个单元格的基础上起作用。你不能像这样更新整行。而是尝试:
for(int i=0;i<tf.length;i++)
{
model.setValueAt(tf[i].getText(), row, i);
}
我需要在用户双击行时用新值编辑 table。我尝试使用 setValuesAt
方法,但它没有做任何事情。
这是我目前得到的结果,但我不知道如何将新值设置为 Jtable
。
public class Table extends JFrame {
private JTable table;
private JScrollPane jsPane;
private DefaultTableModel model;
private JPanel dialogPanel;
private JTextField tf[];
private JLabel lbl[];
private JPanel panel;
Object[] columns = new Object[]{"Name", "Last Name", "ID", "Email"};
Object[][] inData ;
public void prepareAndShowGUI() {
setTitle("Overview");
model = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
model.setColumnIdentifiers(columns);
table = new JTable(model);
for (int i = 1; i <= 10; i++) {
model.addRow(new Object[]{"a", "s", "w", "e"});
}
jsPane = new JScrollPane(table);
table.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
int x = e.getX();
int y = e.getY();
int row = table.rowAtPoint(new Point(x, y));
int col = table.columnAtPoint(new Point(x, y));
String array[] = new String[table.getColumnCount()];
for (int i = 0; i < array.length; i++) {
array[i] = (String) table.getValueAt(row, i);
}
populateTextField(array);
JOptionPane.showMessageDialog(null, dialogPanel, "Information", JOptionPane.INFORMATION_MESSAGE);
String[] values = new String[tf.length];
for (int i = 0; i < tf.length; i++) {
values[i] = tf[i].getText();
}
model.setValueAt(values, row, row);
}
}
});
panel = new JPanel(new BorderLayout());
JPanel panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
panel.add(panel1, BorderLayout.NORTH);
panel.add(jsPane, BorderLayout.CENTER);
getContentPane().add(panel);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
prepareDialogPanel();
setVisible(true);
}
private void prepareDialogPanel() {
dialogPanel = new JPanel();
int col = table.getColumnCount();
dialogPanel.setLayout(new GridLayout(col, 1));
tf = new JTextField[col];
lbl = new JLabel[col];
for (int i = 0; i < col; i++) {
lbl[i] = new JLabel(table.getColumnName(i));
tf[i] = new JTextField(10);
dialogPanel.add(lbl[i]);
dialogPanel.add(tf[i]);
}
}
private void populateTextField(String[] s) {
for (int i = 0; i < s.length; i++) {
tf[i].setText(s[i]);
}
}
}
public static void main(String st[])
{
SwingUtilities.invokeLater( new Runnable()
{
@Override
public void run()
{
Table td = new Table();
td.prepareAndShowGUI();
}
});
}
对于初学者,您应该使您的模型可编辑,因此更改此行:
model = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
收件人:
model = new DefaultTableModel() {
@Override
public boolean isCellEditable(int row, int column) {
return true; //use the row and col to determine which
//cells are editable. If you want all, have this return true.
}
};
这将调用 JTable 的 DefaultCellEditor,当用户对编辑进行更改时,它将调用模型的 setValueAt 方法。
所有这些组件都可以替换为自定义组件以执行不同的操作。
这是关于 JTables 的官方 Oracle 文档:
https://docs.oracle.com/javase/tutorial/uiswing/components/table.html
如果您想让对话框正常工作,问题出在这些行中:
String[] values = new String[tf.length];
for (int i = 0; i < tf.length; i++) {
values[i] = tf[i].getText();
}
model.setValueAt(values, row, row);
基本上 setValueAt 只能在逐个单元格的基础上起作用。你不能像这样更新整行。而是尝试:
for(int i=0;i<tf.length;i++)
{
model.setValueAt(tf[i].getText(), row, i);
}