The variable is not visible.

Discussion in 'Java' started by Tintin92, Feb 16, 2007.

  1. Tintin92

    Tintin92 Guest

    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);
    }
    }
     
    Tintin92, Feb 16, 2007
    #1
    1. Advertising

  2. -----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-----
     
    Hendrik Maryns, Feb 16, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tshad

    button visible/not visible

    tshad, Oct 26, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    4,479
    Brian Hoops
    Oct 28, 2004
  2. tshad

    Sections visible and not visible

    tshad, Jan 31, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    2,082
    tshad
    Jan 31, 2005
  3. Patrick Olurotimi Ige
    Replies:
    7
    Views:
    6,022
    Patrick Olurotimi Ige
    Jun 15, 2005
  4. tshad
    Replies:
    1
    Views:
    4,322
    tshad
    Oct 31, 2009
  5. Jurjen de Groot
    Replies:
    0
    Views:
    323
    Jurjen de Groot
    May 19, 2008
Loading...

Share This Page