Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

The variable is not visible.

Thread view: 
Tintin92 - 16 Feb 2007 11:53 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.