Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > The variable is not visible.

Reply
Thread Tools

The variable is not visible.

 
 
Tintin92
Guest
Posts: n/a
 
      02-16-2007
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);
}
}





 
Reply With Quote
 
 
 
 
Hendrik Maryns
Guest
Posts: n/a
 
      02-16-2007
-----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-----
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Object variable or With block variable not set David C ASP .Net 0 10-09-2007 09:01 PM
"Variable variable name" or "variable lvalue" mfglinux Python 11 09-12-2007 03:08 AM
Object variable or With Block variable not set error =?Utf-8?B?TmV3YmllRGV2?= ASP .Net 0 04-26-2006 03:11 AM
Object variable or With block variable not set Neo Geshel ASP .Net 6 03-23-2006 06:50 AM
How do I scope a variable if the variable name contains a variable? David Filmer Perl Misc 19 05-21-2004 03:55 PM



Advertisments