Results 1 to 18 of 18
- 02-28-2009, 07:54 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 40
- Rep Power
- 0
[SOLVED] Cannot find symbol error
Can anyone tell me how to resolve the following compile-time error?
I have two classes: UserInterface and TestUserInterface. Both are in the same package.TestUserInterface.java:5: cannot find symbol
symbol: class UserInterface
location: class projects.web.TestUserInterface
UserInterface ui = new UserInterface();
To compile and run them I use the following cmds respectively:
Java Code:javac -d ..\classes projects\web\TestUserInterface.java
Following is the code for both classes:Java Code:java -cp ..\classes projects.web.TestUserInterface
Java Code:package projects.web; import java.awt.*; import javax.swing.*; public class UserInterface extends JFrame{ JPanel menuPanel = new JPanel(); JPanel contentPanel = new JPanel(); JPanel selectionPanel = new JPanel(); JButton save = new JButton("Save"); JButton addFiles = new JButton("Add"); public UserInterface(){ super("File Upload"); setSize(500, 500); menuPanel.add(addFiles); selectionPanel.add(save); setLayout(new BorderLayout()); add(menuPanel, BorderLayout.NORTH); add(contentPanel, BorderLayout.CENTER); add(selectionPanel, BorderLayout.SOUTH); } // end constructor } // end UserInterface clasJava Code:package projects.web; public class TestUserInterface{ public static void main(String[] args){ UserInterface ui = new UserInterface(); } } // end TestUserInterface classLast edited by dan0; 03-03-2009 at 05:11 PM. Reason: Error resolved
-
The class code both look fine. I see you using a -cp switch. Do you need also to include "this" directory in the class path? by this I mean to include a period: "."?
- 02-28-2009, 08:15 PM #3
Member
- Join Date
- Feb 2009
- Posts
- 40
- Rep Power
- 0
I have included "." (this directory) in the classpath - i.e. in my environment variable "classpath" I have the following,
Could the problem be with the package or how I've setup the directory to hold the compiled files?.;C:\Program Files\Java\jdk1.6.0_11\lib\tools.jar
-
I'm thinking not. If this were the problem, it wouldn't be able to find the TestUserInterface class at all (I think). I'm at a loss to figure this one out. Sorry.Could the problem be with the package or how I've setup the directory to hold the compiled files?
Edit: Still, I'd try including the period in your java -cp statement just to see what happens. You've nothing to lose.
- 02-28-2009, 08:33 PM #5
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
This
will work just fine, since it doesn't need anything else to compile. This (assuming the problem is during compilation)Java Code:javac -d ..\classes projects\web\UserInterface.java
however, may not, as it needs the UserInterface class in order to compile. You should tryJava Code:javac -d ..\classes projects\web\TestUserInterface.java
although the -d may assume that automatically, I don't think it does.Java Code:javac -cp ..\classes -d ..\classes projects\web\TestUserInterface.java
Also, when using -cp, the CLASSPATH environment variable is ignored, and when using -jar, both -cp and the CLASSPATH environment variable is ignored (it will use only the MANIFEST file).
- 02-28-2009, 08:43 PM #6
Member
- Join Date
- Feb 2009
- Posts
- 40
- Rep Power
- 0
In the cmd prompt I selected the directory that holds the class file:
Then I entered the following cmd:C:\Document and Settings\Dan\My Documents\workspace\classes>
I then got the following error:Java Code:java -cp .; projects\web\TestUserInterface
NOTE: When I tried the following version of the cmd, while in the directory that has the class files, nothing happened.Exception in thread "main" java.lang.NoClassDefFoundError: projects\web\TestUserInterface
I also tried re-compling the dependent file via:Java Code:java -cp .; projects.web.TestUserInterface
But when I tried to run the class file, I got the same error:Java Code:javac -cp ..\classes -d ..\classes projects\web\TestUserInterface.java
At this point, I'm thoroughly confused and only managed to give myself a headache...Exception in thread "main" java.lang.NoClassDefFoundError: projects\web\TestUserInterfaceLast edited by dan0; 02-28-2009 at 08:45 PM.
- 02-28-2009, 08:47 PM #7
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 8
Which means you were successful, because nothing in that code actually displays the interface, just creates it.
I also tried re-compling the dependent file via:
But when I tried to run the class file, I got the same error:Java Code:javac -cp ..\classes -d ..\classes projects\web\TestUserInterface.java
At this point, I'm thoroughly confused and only managed to give myself a headache...
- 02-28-2009, 10:03 PM #8
Member
- Join Date
- Feb 2009
- Posts
- 40
- Rep Power
- 0
-
Don't forget to pack it either. Your test class should look something like so:
Java Code:import javax.swing.JFrame; import javax.swing.SwingUtilities; public class TestUserInterface { private static void createGui() { UserInterface ui = new UserInterface(); ui.pack(); ui.setLocationRelativeTo(null); ui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ui.setVisible(true); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { createGui(); } }); } }
- 02-28-2009, 10:37 PM #10
Member
- Join Date
- Feb 2009
- Posts
- 40
- Rep Power
- 0
I have a couple of follow-up questions. My end goal is to create an applet class that has its interface created by another class. From what I understand, JFrame, JApplet, etc. all rely on the Root Pane to manage their content pane. So via the Root Pane I should be able to access the content pane and add which ever components I'd like.
- Does my approach/theory make sense?
- What will I have to change to make my UserInterface class work as described above?
-
Hm, not quite. You will want to place your Swing widgets into a JPanel likely and the Jpanel will be placed into the JApplet or JFrames contentPane, not the rootPane.
- 02-28-2009, 11:45 PM #12
Member
- Join Date
- Feb 2009
- Posts
- 40
- Rep Power
- 0
Placing the JPanels in the JApplet (or JFrame, etc.) contentPane is what I'm confused about. For example, in my UserInterface code I have the following components:
Let's say I change TestUserInterface to:Java Code:JPanel menuPanel = new JPanel(); JPanel contentPanel = new JPanel(); JPanel selectionPanel = new JPanel();
Since UserInterface doesn't extend JApplet I can't use the add method, so how do I add the JPanels?Java Code:public class TestUserInterface extends JApplet{ public static void main(String[] args){ UserInterface ui = new UserInterface(mainContainer); } } // end TestUserInterface class
-
Most of my GUI classes don't extend JFrame, JPanel, or other components. I try to only extend these classes if my class alters its basic behavior somehow. Otherwise, I'll have my class produce a JPanel or other component on request. Hang on a sec and I'll see if I can dig up an example.
- 03-01-2009, 07:51 PM #14
Member
- Join Date
- Feb 2009
- Posts
- 40
- Rep Power
- 0
Any luck with finding an example? In the meantime, I put together the following code. Is it the proper way to have one class create a GUI for another applet class?
Java Code:package projects.web; import java.awt.*; import javax.swing.*; public class UserInterface{ JPanel buttonPanel = new JPanel(); JButton saveButton = new JButton("Save"); public UserInterface(JRootPane topContainer){ buttonPanel.add(saveButton); topContainer.getContentPane().add(buttonPanel); } } // end UserInterface classJava Code:package projects.web; import javax.swing.*; public class TestUserInterface extends JApplet{ JRootPane topContainer; public void init(){ topContainer = this.getRootPane(); UserInterface ui = new UserInterface(topContainer); } } // end TestUserInterface class
-
I'm wondering whether you're doing things a little backwards here. You would normally have a class create a JPanel and then the root component that displays that will get the JPanel, place it in the root's contentPane, and display it. You seem to be trying to have the JPanel request the object that's supposed to be displaying it. I'm trying to think of situations where you'd need that set up, and I can't think of any right now.
Anyway, here's an example based on code created earlier:
QueryComponent2.java
creates a JPanel that displays a bunch of textfields and labels and can be used to extract text info out of the fields
QueryComponentMain.javaJava Code:import java.awt.BorderLayout; import java.awt.GridLayout; import java.util.HashMap; import java.util.Map; import javax.swing.BorderFactory; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JComponent; import javax.swing.JTextField; public class QueryComponent2 { private static final int FIELD_LENGTH = 12; private static final int MID_GAP = 25; private static final int LINE_GAP = 10; private static final int BORDER_GAP = 10; private JPanel mainPanel = new JPanel(); private Map<String, JTextField> fieldMap = new HashMap<String, JTextField>(); private String[] labelStrings; public QueryComponent2(String[] labels) { JPanel labelPanel = new JPanel(new GridLayout(0, 1, 0, LINE_GAP)); JPanel fieldPanel = new JPanel(new GridLayout(0, 1, 0, LINE_GAP)); labelStrings = new String[labels.length]; System.arraycopy(labels, 0, labelStrings, 0, labels.length); for (String string : labels) { labelPanel.add(new JLabel(string)); JTextField textField = new JTextField(FIELD_LENGTH); fieldPanel.add(textField); fieldMap.put(string, textField); } mainPanel.setBorder(BorderFactory.createEmptyBorder(BORDER_GAP, BORDER_GAP, BORDER_GAP, BORDER_GAP)); mainPanel.setLayout(new BorderLayout(MID_GAP, 0)); mainPanel.add(labelPanel, BorderLayout.WEST); mainPanel.add(fieldPanel, BorderLayout.CENTER); } public String getFieldText(String key) { return fieldMap.get(key).getText(); } // get the mainPanel to place into a JOptionPane public JComponent getComponent() { return mainPanel; } }
creates a JPanel that holds a button. When pressed, this will display a JDialog with fields based on the Strings in the FIELD_NAMES array
QueryComponentFrame.javaJava Code:import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JComponent; public class QueryComponentMain { private static final String[] FIELD_NAMES = { "First Name", "Second Name", "Phone Number", "What the Heck", "DILLIGAF", "Monday", "Tuesday", "Wednesday" }; private JPanel mainPanel = new JPanel(); public QueryComponentMain() { JButton getNamesBtn = new JButton("Get Names"); getNamesBtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { getNamesAction(); } }); mainPanel.add(getNamesBtn); } // occurs when button is pressed private void getNamesAction() { // create object to place into JOptionPane QueryComponent2 queryComp = new QueryComponent2(FIELD_NAMES); // show JOptionPane int result = JOptionPane.showConfirmDialog(mainPanel, queryComp .getComponent(), // place the component into the JOptionPane "Get Names", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if (result == JOptionPane.OK_OPTION) // if ok selected { // query the queryComp for its contents by calling its getters for (int i = 0; i < FIELD_NAMES.length; i++) { System.out.println(FIELD_NAMES[i] + ": " + queryComp.getFieldText(FIELD_NAMES[i])); } } } public JComponent getComponent() { return mainPanel; } }
create a JFrame and place the QueryComponentMain's JPanel inside of it.
QueryComponentApplet.javaJava Code:import javax.swing.JFrame; public class QueryComponentFrame { private static void createAndShowUI() { JFrame frame = new JFrame("Query Component"); frame.getContentPane().add(new QueryComponentMain().getComponent()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } }
create a JApplet and place QueryComponentMain's JPanel inside of it's contentPane and display it
Java Code:import javax.swing.JApplet; import javax.swing.SwingUtilities; public class QueryComponentApplet extends JApplet { private void createGUI() { getContentPane().add(new QueryComponentMain().getComponent()); } @Override public void init() { try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { createGUI(); } }); } catch (Exception e) { e.printStackTrace(); } } }
- 03-02-2009, 09:08 PM #16
Member
- Join Date
- Feb 2009
- Posts
- 40
- Rep Power
- 0
Thank you for the examples; they helped me understand how to add components to a target root's contentPane.
For future reference, is there a best practice when it comes to adding components to a container? Is it bad practice to have the component request the container that's supposed to hold it?Last edited by dan0; 03-02-2009 at 09:25 PM.
-
Please know that I'm not a pro, so take my recommendations for what they are: hobbiest recommendations, but I do have my component classes sometimes request a reference to the container that holds them, but the trade-off is that this increases coupling, something we try to avoid doing.
- 03-03-2009, 05:09 PM #18
Member
- Join Date
- Feb 2009
- Posts
- 40
- Rep Power
- 0
Similar Threads
-
cannot find symbol symbol :constructor Error. Please help! =(
By KalEl in forum New To JavaReplies: 9Last Post: 10-18-2008, 08:26 PM -
'Cannot find symbol' error
By minihazard10 in forum New To JavaReplies: 6Last Post: 10-10-2008, 04:05 AM -
Programm Error: cannot find symbol Help?
By junix in forum New To JavaReplies: 2Last Post: 12-10-2007, 05:30 AM -
Error: cannot find symbol
By silvia in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:39 AM -
Error: cannot find symbol
By cachi in forum AWT / SwingReplies: 1Last Post: 08-06-2007, 08:12 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks