Hello,
With the following test program, I try to add a row to the JTable when I
click on the item menu.
I cant compile it.
I have the following error message.
dtm cannot be resolved
at this ligne :
dtm.addRow(ob);
Regards,
Tintin92
Here all the source code :
// SimpleTable.java
// A test of the JTable class using default table models and a convenience
// constructor.
//
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.*;
public class TestJTable03 extends JFrame {
private Action addRowAction = new AddRowAction();
public class AddRowAction extends AbstractAction {
public AddRowAction() {
putValue(NAME, "Add Row");
putValue(SMALL_ICON, new ImageIcon("images/up.gif"));
putValue(SHORT_DESCRIPTION, "Add Row");
putValue(MNEMONIC_KEY, new Integer(KeyEvent.VK_U));
}
public void actionPerformed(ActionEvent ae) {
System.out.println("Clik sur menu");
}
}
public TestJTable03() {
super("Simple JTable Test");
setSize(300, 200);
setDefaultCloseOperation(EXIT_ON_CLOSE);
DefaultTableModel dtm = new DefaultTableModel(
new String[][] {
{"1", "2", "3"},
{"4", "5", "6"} },
new String[] {"Names", "In", "Order"});
JTable jt = new JTable(dtm);
Object[] ob = {"7", "8", "9"};
dtm.addRow(ob);
JScrollPane jsp = new JScrollPane(jt);
getContentPane().add(jsp, BorderLayout.CENTER);
JMenuBar mb = new JMenuBar();
JMenu menu = new JMenu("File");
menu.add(new JMenuItem(addRowAction));
mb.add(menu);
setJMenuBar(mb);
}
class SampleAction extends AbstractAction {
// This is our sample action. It must have an actionPerformed()
// method,
// which is called when the action should be invoked.
public SampleAction(String text, Icon icon) {
super(text, icon);
}
public void actionPerformed(ActionEvent e) {
Object[] ob = {"10", "11", "12"};
dtm.addRow(ob);
System.out.println("Action [" + e.getActionCommand()
+ "] performed!");
}
}
public static void main(String args[]) {
TestJTable03 st = new TestJTable03();
st.setVisible(true);
}
}
Hendrik Maryns - 16 Feb 2007 12:28 GMT
Tintin92 schreef:
> Hello,
>
[quoted text clipped - 7 lines]
> at this ligne :
> dtm.addRow(ob);
Always copy the whole error message. You should have mentioned that is
is the _second_ occurrence of that line that causes the problem. And of
course, there is no dtm there, since dtm is a local variable in the
constructor. What did you expect?
Make it an instance variable.
> Here all the source code :
>
[quoted text clipped - 73 lines]
> }
> }
H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html