0
私はJTableにadd関数を実装しましたが、更新を呼び出すときにフィールドが空になるため、更新が困難です。選択したテーブル項目をどのように参照し、その列からデータを引き出すのですか。私が正しくJTableの項目を更新する
1見ればここで
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();
}
}
}
から
JTable
ためのすべてのデータがああ、あなたはあなたの問題はまだ必要minimunにアプリケーションを簡素化することができませんロード持続する? – kajacx