gpt4 book ai didi

java - 更新 JTable 中的项目

转载 作者:行者123 更新时间:2023-12-02 07:57:29 25 4
gpt4 key购买 nike

我已经成功地向 JTable 实现了添加功能,但在更新时遇到了困难,因为当我调用更新时,我的字段被拉空。我如何引用选定的表项并从其列中提取数据。下面是我的代码

public class GUIMemos {
Calendar calendar = Calendar.getInstance();
Date timeStamp = new java.sql.Timestamp(calendar.getTime().getTime());
private Frame frame = new Frame();
private DefaultTableModel tablemodel;
private Memos memos = new Memos();
private AddDialog add_dialog = new AddDialog(frame, true);



public GUIMemos()throws Exception{
}
public void init() {
final int WIDTH = 600, HEIGHT = 600;
frame.setBounds(10, 10, WIDTH, HEIGHT);
frame.setDivider((int) (HEIGHT * .4));
frame.setTitle("GUI Memos");

String[] column_names = {"id" , "timestamp" , "title",};
tablemodel = frame.setTableModel(column_names);

double[] column_widths = {10, 50, 40}; // percentages adding to 100
frame.setColumnWidths(column_widths);

load_table();
frame.setVisible(true); // invoke the frame

frame.addListSelectionListener(new ListSelectionListener() {
@Override
@SuppressWarnings({"CallToThreadDumpStack"})
public void valueChanged(ListSelectionEvent evt) {
if (evt.getValueIsAdjusting()) {
return;
}
int row = frame.getSelectedModelRow();
int id = (Integer) tablemodel.getValueAt(row, 0);

try {
Memo memo = memos.fetch(id);
//frame.setContentText(memo.toString()); // doesn't matter either way
frame.setContentText(memo.getContent());
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
});

add_dialog.setSize(500, 300);

frame.addMenuAddActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
add_dialog.setTitle("Add Memo");
add_dialog.setTitleText("");
add_dialog.setContentText("");
add_dialog.setVisible(true);
}
});


add_dialog.addOKActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {

String title = add_dialog.getTitleText().trim();
String content = add_dialog.getContentText();
Timestamp timeStamp = new Timestamp(calendar.getTime().getTime());

try {
if (title.isEmpty()) {
throw new Exception("cannot have empty title");
}
Memo memo = new Memo(title, timeStamp, content);
int id = memos.insert(memo);
tablemodel.addRow(new Object[]{id, title, timeStamp});
add_dialog.setVisible(false);
}
catch (Exception x) {
JOptionPane.showMessageDialog(frame, x.getMessage());
}
}
});
frame.addMenuRemoveActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent evt){

}
});
frame.addMenuUpdateActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent evt){

String title = add_dialog.getTitleText();
String content = add_dialog.getContentText();
add_dialog.setTitle("Add Memo");
add_dialog.setTitleText(title);
add_dialog.setContentText(content);
add_dialog.setVisible(true);
}

});

add_dialog.addCancelActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
handle_add_cancel();
}
});

add_dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

add_dialog.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent evt) {
handle_add_cancel();
}
});

}

@SuppressWarnings({"CallToThreadDumpStack"})
private void load_table() {
try {
for (Memo memo : memos.fetchAll()) {
tablemodel.addRow(new Object[]{
memo.getId(),
memo.getTimeStamp(),
memo.getTitle(),});
}
}
catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}

private void handle_add_cancel() {
if (add_dialog.getTitleText().trim().equals("")
&& add_dialog.getContentText().trim().equals("")) {

add_dialog.setVisible(false); // nothing of significance added
}
else { // something is there: query the user
String message = "Do you want to exit\nwithout adding ?";

int response = JOptionPane.showOptionDialog(
frame, message, null, JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE, null,
new String[]{"yes", "no"}, "no"
);

if (response == JOptionPane.YES_OPTION) {
add_dialog.setVisible(false);
}
}
}

@SuppressWarnings("CallToThreadDumpStack")
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new GUIMemos().init();
}
catch (Exception e) {
e.printStackTrace();
}
}
}

最佳答案

如果我没看错

1) 你忘记定义 JTable :-)

private Frame frame = new Frame();

然后

tablemodel = frame.setTableModel(column_names);
frame.setColumnWidths(column_widths);

2) frame.setVisible(true); 必须是 GUI 创建后的最后一个代码行

3) 真的只有Twinkle Twinkle Little Star可以知道你的类(class)返回什么

Memo memo = memos.fetch(id);

4) 从 SwingWorker 加载 JTable 的所有数据

关于java - 更新 JTable 中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9418645/

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