The variable is not visible.

T

Tintin92

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);
}
}
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Tintin92 schreef:
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);

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 :


// 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);
}
}

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFF1aNSe+7xMGD3itQRAizCAJ4xo5d6t7QbzkjbQoMPK+CJC1FzkgCdFPei
JHWMKCUe0cxL3r1t0+Wvc8A=
=veYq
-----END PGP SIGNATURE-----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top