私はJTable
を使用しています。私は、セル選択値が変わるたびに通知を出したい。しかし、私のコードは動作していません。私はTableModelListener
を使用しています。表のセルの値を変更しても何も起こりません。表モデル・リスナーが機能しない
public class Main extends JFrame{
private JFrame frame;
private DrawPanel jPanel;
JRadioButton rdb2d;
JRadioButton rdb3d;
private JTable tbRect;
private JTable tbCircle;
List<Object> listCircle = new ArrayList<>();
List<Object> listRect = new ArrayList<>();
List<Object> undoCircle = new ArrayList<>();
List<Object> undoRect = new ArrayList<>();
List<Object> redoRedo = new ArrayList<>();
public static void main(String[] args) {
Main window = new Main();
window.frame.setVisible(true);
}
public Main() {
frame = new JFrame();
frame.setBounds(100, 100, 846, 494);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
frame.setVisible(true);
jPanel = new DrawPanel();
jPanel.setBounds(220, 76, 600, 369);
frame.getContentPane().add(jPanel);
JPanel panel_1 = new JPanel();
panel_1.setBounds(220, 11, 600, 54);
frame.getContentPane().add(panel_1);
JButton btnAdd = new JButton("Add");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String[] buttons = { "Circle", "Rectangle" };
int option = JOptionPane.showOptionDialog(null, "Choose a shape", "Confirmation", JOptionPane.WARNING_MESSAGE, 0, null, buttons, buttons[1]);
if (rdb2d.isSelected()) {
AbstractFactory aFactory = new FactoryProducer().getFactory("2d");
Shape2D shape2d = null;
if (option == 0) {
shape2d = aFactory.getShape2D("circle");
listCircle.add(shape2d);
updateTableCircle(listCircle);
}else if (option == 1) {
shape2d = aFactory.getShape2D("rectangle");
listRect.add(shape2d);
updateTableRect(listRect);
}
jPanel.add(shape2d);
}else if (rdb3d.isSelected()) {
AbstractFactory aFactory = new FactoryProducer().getFactory("3d");
Shape3D shape3d = null;
if (option == 0) {
shape3d = aFactory.getShape3D("circle");
listCircle.add(shape3d);
updateTableCircle(listCircle);
}else if (option == 1) {
shape3d = aFactory.getShape3D("rectangle");
listRect.add(shape3d);
updateTableRect(listRect);
}
jPanel.add(shape3d);
}
}
});
panel_1.add(btnAdd);
JButton btnRemove = new JButton("Remove");
btnRemove.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (tbCircle.getSelectedRow() != -1) {
jPanel.remove(listCircle.get(tbCircle.getSelectedRow()));
listCircle.remove(tbCircle.getSelectedRow());
updateTableCircle(listCircle);
}
if (tbRect.getSelectedRow() != -1) {
jPanel.remove(listRect.get(tbRect.getSelectedRow()));
listRect.remove(tbRect.getSelectedRow());
updateTableRect(listRect);
}
}
});
panel_1.add(btnRemove);
JButton btnUndo = new JButton("Undo");
btnUndo.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
jPanel.undo();
}
});
panel_1.add(btnUndo);
rdb3d = new JRadioButton("3D");
panel_1.add(rdb3d);
ButtonGroup buttonGroup = new ButtonGroup();
buttonGroup.add(rdb2d);
buttonGroup.add(rdb3d);
JPanel panel = new JPanel();
panel.setBounds(10, 11, 200, 434);
frame.getContentPane().add(panel);
panel.setLayout(null);
JScrollPane jPane = new JScrollPane();
jPane.setBounds(0, 36, 200, 125);
panel.add(jPane);
tbCircle = new JTable();
tbCircle.setModel(new DefaultTableModel(
new Object[][]{
},
new String[]{
"Position", "Radius"
}
));
jPane.setViewportView(tbCircle);
tbCircle.getModel().addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
System.out.println("Test Output");
}
});
JScrollPane jPane2 = new JScrollPane();
jPane2.setBounds(0, 248, 200, 163);
panel.add(jPane2);
tbRect = new JTable();
tbRect.getModel().addTableModelListener(new TableModelListener() {
@Override
public void tableChanged(TableModelEvent e) {
System.out.println(e.getColumn());
}
});
tbRect.setModel(new DefaultTableModel(
new Object[][]{},
new String[]{
"Position", "Width", "Height"
}
));
jPane2.setViewportView(tbRect);
}
}
テーブルセルの値を変更しても何も起こりませんでした。
http://stackoverflow.com/questions/12376826/jtable-cell-listener私のために罰金作品 – dit
:http://stackoverflow.com/questions/3540661/tablemodellistener-and-multiple-column-validation/3541876#3541876を参照してください。さらにヘルプが必要な場合は、問題を示す適切な[SSCCE](http://sscce.org/)を投稿してください。私たちはあなたのアプリケーション全体のコードを読むためのものではありません。あなたのSSCCEはJTableを持つJFrameに過ぎないので、あなたは簡単にあなたの問題を見ることができます。これを実行すると、アプリケーションの違いを確認できます。 – camickr