質問内容
質問を評価する
(0ポイント)
|
DBのデータを二つのコンボボックスで絞込みjtableに表示 しています。 jtable上で編集した値をDBに反映させたいのですが jtableのセルを変更した時のTableModelListener()の tableChangedが呼ばれないのです。 コードが長いので関係ありそうな部分を抜粋して載せたか ったのですが、どこが関連しているかわからなかったので あえてほとんど省略せずに載せさせていただきました。 どうしてイベントが発生しないのか教えていただけません か? package test;
import 省略
public class Records extends JFrame implements ActionListener {
private JScrollPane scroll; private DefaultTableModel model;
private static final String JDBC_DRIVER = "org.postgresql.Driver"; private static final String STR_CONN = "jdbc:postgresql://localhost:5433/yu2" + "? user=yu2admin&password=yu2&useUnicode=true&character Encoding=MS932"; private static final String[] STR_HEADER = { "r_id", "id", "s_id", "results", "n_id", "flg" }; private JTable table; private JPanel jContentPane = null; private JLabel jLabel = null; private JPanel jPanel = null; private JButton jButton3 = null; private JMenuBar jJMenuBar = null; private JMenu jMenu = null; private JMenuItem jMenuItem = null; private JComboBox comboBox; private JComboBox comboBox1; int sel = 0; int sel1 = 0;
private JMenuBar getJJMenuBar() { if (jJMenuBar == null) { jJMenuBar = new JMenuBar(); jJMenuBar.add(getJMenu()); jJMenuBar.add(getComboBox()); jJMenuBar.add(getComboBox1()); } return jJMenuBar; }
private JMenu getJMenu() { if (jMenu == null) { jMenu = new JMenu(); jMenu.setText("ファイル (F)"); jMenu.addSeparator(); jMenu.setMnemonic(KeyEvent.VK_F); jMenu.add(getJMenuItem()); } return jMenu; }
private JMenuItem getJMenuItem() { if (jMenuItem == null) { jMenuItem = new JMenuItem(); jMenuItem.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { showExitDialog(); } }); jMenuItem.setText("終了 (X)"); jMenuItem.setMnemonic(KeyEvent.VK_X); } return jMenuItem; }
private void initialize() { this.setJMenuBar(getJJMenuBar()); this.setContentPane(getJContentPane()); // this.setLocationRelativeTo(null); }
private JPanel getJPanel() { if (jPanel == null) { GridLayout gridLayout = new GridLayout(); gridLayout.setRows(1); gridLayout.setColumns(4); jPanel = new JPanel(); jPanel.setLayout(gridLayout); jPanel.add(getJButton3(), null); } return jPanel; }
private JButton getJButton3() { if (jButton3 == null) { jButton3 = new JButton(); jButton3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { showExitDialog(); } }); jButton3.setText("終了"); jButton3.setForeground(Color.red); } return jButton3; }
private JPanel getJContentPane() { if (jContentPane == null) { jLabel = new JLabel(); jLabel.setText("JLabel"); jLabel.setHorizontalAlignment(SwingConstants.CENTER) ; jLabel.setBackground(Color.white); jLabel.setFont(new Font("Dialog", Font.PLAIN, 12)); jLabel.setOpaque(true); jContentPane = new JPanel(); jContentPane.setLayout(new BorderLayout()); jContentPane.add(jLabel, BorderLayout.NORTH); // jContentPane.add(getJList(), BorderLayout.CENTER); jContentPane.add(getJPanel(), BorderLayout.SOUTH); } return jContentPane; }
private JComboBox getComboBox() { if (comboBox == null) { comboBox = new JComboBox(); String sql = "SELECT n_id, number FROM numbers order by n_id"; try { Class.forName(JDBC_DRIVER); Connection conn = DriverManager.getConnection(STR_CONN); Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); int count = 0; while (rs.next()) { comboBox.addItem(rs.getString("n_id")); } conn.close(); } catch (Exception e) { System.out.println(e.getMessage() + ":" + sql); System.exit(1); }
comboBox.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { selectTable(); table.setModel(model); } }); } return comboBox; }
private JComboBox getComboBox1() { if (comboBox1 == null) { comboBox1 = new JComboBox(); try { Class.forName(JDBC_DRIVER); Connection conn = DriverManager.getConnection(STR_CONN); Statement stmt = conn.createStatement();
int cmbsel0 = comboBox.getSelectedIndex() + 11; String mySql0 = "SELECT s_id, sector, n_id FROM sector order by s_id"; String mySql1 = "SELECT s_id, sector, n_id FROM sector" + " where n_id = " + cmbsel0 + " order by s_id";
if (cmbsel0 == -1) { String sql = mySql0; System.out.println("comboBox1_sql = " + sql); ResultSet rs = stmt.executeQuery(sql); int count = 0; while (rs.next()) { comboBox1.addItem(rs.getString("s_id")); } conn.close();
} else { String sql = mySql1; System.out.println("comboBox1_sql = " + sql); ResultSet rs = stmt.executeQuery(sql); int count = 0; while (rs.next()) { comboBox1.addItem(rs.getString("s_id")); } conn.close(); }
} catch (Exception e) { System.out.println(e.getMessage()); System.exit(1); } comboBox1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { selectTable(); table.setModel(model); } }); } return comboBox1; }
public static void main(String[] args) { Records frame = new Records(); frame.setSize(300, 520); frame.setResizable(false); frame.setVisible(true); }
public Records() { super("Records"); initialize(); table = selectTable(); scroll = new JScrollPane(table); table.getModel().addTableModelListener( new TableModelListener() { public void tableChanged(TableModelEvent te) { int row = te.getLastRow(); // セルの行位置を取得 int col = te.getColumn(); // セルの列位置を取得
if (te.getType() == TableModelEvent.INSERT) { //追加の場合の処理 insertTable(); int Crow = row + 1; int Ccol = col + 1; jLabel.setText("行" + Crow + "列" + Ccol + "が追加さ れました。"); System.out.println("行" + Crow + "列" + Ccol + "が追 加されました。"); String message = "行" + row + "列" + col + "が追加されました。"; JOptionPane.showMessageDialog(Records.this, message); }
if (te.getType() == TableModelEvent.UPDATE) { //更新の場合の処理 updateTable(row, col); int Crow = row + 1; int Ccol = col + 1; jLabel.setText("行" + Crow + "列" + Ccol + "が変更さ れました。"); System.out.println("行" + Crow + "列" + Ccol + "が変 更されました。"); String message = "行" + row + "列" + col + "が追加されました。"; JOptionPane.showMessageDialog(Records.this, message); int trc = table.getRowCount(); int mrc = model.getRowCount(); int cmbsel0 = comboBox.getSelectedIndex() + 1; Object cmbobj1 = comboBox1.getSelectedItem(); String cmbsel1 = cmbobj1.toString(); System.out.println("trc = " + trc); System.out.println("mrc = " + mrc); if (mrc == row + 1) { model.addRow(new Object[] { trc + 1, 0, cmbsel1, "000000", cmbsel0, 1 }); } updateTable(row, col); jLabel.setText("行" + Crow + "列" + Ccol + "が変更さ れました。"); System.out.println("行" + Crow + "列" + Ccol + "が変 更されました。"); Object tgv = table.getValueAt(row, 3); String stgv = tgv.toString(); int lng = stgv.length(); if (lng < 7) { StringBuilder sb1 = new StringBuilder(stgv); sb1.insert(2, ":"); sb1.insert(5, ":"); String str = new String(sb1); System.out.println("str = " + str); table.setValueAt(str, row, 3); } } } }); getContentPane().add(new JScrollPane(table)); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); }
private JTable selectTable() { JTable table = null; Vector<String> header = new Vector<String>(Arrays.asList(STR_HEADER)); model = new DefaultTableModel(0, header.size()); model.setColumnIdentifiers(header); try { // SELECTの場合の処理
} catch (Exception e) { System.out.println(e.getMessage()); System.exit(1); } return table; }
private void showExitDialog() { int ret = JOptionPane.showConfirmDialog(jContentPane, "プログ ラムを終了しますか?", "確認", JOptionPane.YES_NO_OPTION); if (ret == JOptionPane.YES_OPTION) { System.exit(0); } }
private void insertTable() { // INSERTの場合の処理 }
private void updateTable(int row, int col) { // UPDATEの場合の処理 }
@Override public void actionPerformed(ActionEvent e) { // TODO 自動生成されたメソッド・スタ ブ }
}
|