Results 1 to 7 of 7
- 03-19-2010, 12:32 PM #1
Member
- Join Date
- Jan 2010
- Posts
- 81
- Rep Power
- 0
Question about ActionListeners and JAR files
I made an application that pops up a simple frame with 2 TextFields (Username & Password) and 2 Buttons (OK & Cancel). Everything works fine and I am able to create an executable JAR file and also am able to launch in from my website on multiple computers with no problem with a JNLP file that calls the JAR file.
When I try adding an ActionListener to the Buttons or TextFields the application runs fine in NetBeans IDE, but when I create a JAR file it isn't executable. I've narrowed it down to the ActionListener being the culprit.
Do I have to add something to the manifest or JAR file to get ActionListeners working?
-
ActionListeners should work fine in a Jar file. Are you per chance trying to access a file in this ActionListener's actionPerformed method? If so and if this file is in the Jar file, then this is where your problem likes as there are no "files" in Jar files but rather resources. You may wish to try to create and post an SSCCE that shows your problem.
- 03-19-2010, 12:58 PM #3
Member
- Join Date
- Jan 2010
- Posts
- 81
- Rep Power
- 0
I'm not trying to access another file. The only thing I'm trying to do is dispose the window when the Cancel Button is clicked.
I highlighted the section in Red that causes the JAR to not execute when I add it. It works correctly in NetBeans IDE though.
Here's my code...
StartingFrame.java
andJava Code:public class StartingFrame extends JFrame { public static void main(String [] args) { StartingFrame app = new StartingFrame(); app.createGUI(); } private void createGUI() { //Create and set up the content pane. StartingPanel newContentPane = new StartingPanel(); newContentPane.setOpaque(true); setContentPane(newContentPane); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); int frameWidth = 308; int frameHeight = 184; setSize(frameWidth, frameHeight); setTitle("Login - Test 1"); setVisible(true); setResizable(false); // Get the size of the screen Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); int x = (dim.width-frameWidth)/2; int y = (dim.height-frameHeight)/2; // Move the window setLocation(x, y); } }
StartingPanel.java
Java Code:public class StartingPanel extends JPanel { public StartingPanel() { setLayout(null); JPanel panel3 = new JPanel(); panel3.setLayout(null); panel3.setBounds(0, 110, 300, 40); add(panel3); JButton cancelButton = new JButton("Cancel"); cancelButton.setBounds(160, 5, 75, 25); panel3.add(cancelButton); [B][COLOR="Red"]cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { Window win = SwingUtilities.getWindowAncestor(getParent()); win.dispose(); } });[/COLOR][/B] } }
- 03-19-2010, 01:05 PM #4
Member
- Join Date
- Jan 2010
- Posts
- 81
- Rep Power
- 0
My JAR file (GameTestJAR.jar) contains:
• kevin\StartingFrame.class
• kevin\StartingPanel.class
• META-INF\MANIFEST.MF
Here is my manifest file:
This manifest file works fine when I don't include the ActionListener part of the code.Java Code:Manifest-Version: 1.0 Ant-Version: Apache Ant 1.7.1 X-COMMENT: Main-Class will be added automatically by build Class-Path: GameTestJAR.jar Created-By: 14.3-b01 (Sun Microsystems Inc.) Main-Class: kevin.StartingFrame
My package name is kevin obviously.
- 03-19-2010, 04:00 PM #5
When running the jar via jnlp, have you observed any exceptions in the Java Console?
If you're on Windows, you can show the Java console by going to Control Panel --> Java --> Advanced tab --> expand the Console node --> select Show
If you do get an exception, post the stack trace from the console, and indicate the corresponding line numbers in your code.
db
- 03-20-2010, 04:40 AM #6
Member
- Join Date
- Jan 2010
- Posts
- 81
- Rep Power
- 0
OK, I think I resolved the problem. I wasn't including StartingPanel$1.class in the JAR file, just StartingPanel.class.
I thought I read somewhere or maybe I just assumed that this was just a temporary file since it wasn't always created. It doesn't show up when I leave out the ActionListener but I hadn't made the connection.
What exactly are the class files with $x added? I can see that there is one created for each ActionListener I have. Are they created by/for anything else?
-
That is the class file for your anonymous inner class. i.e., the ActionListener class. You can now see why it is necessary.
Similar Threads
-
Convert avi, mpeg, wmv media files to .flv files in java code
By vinay1497 in forum New To JavaReplies: 8Last Post: 07-30-2010, 05:47 PM -
2 Part question about cfg xml and dat files
By Samgetsmoney in forum New To JavaReplies: 0Last Post: 02-18-2009, 02:36 AM -
Cannot Implement Stop And Pause...multiple Actionlisteners..please Help.
By pkumar85 in forum Advanced JavaReplies: 1Last Post: 12-07-2008, 05:50 PM -
Behaving text files like binary files
By Farzaneh in forum New To JavaReplies: 2Last Post: 08-27-2008, 03:20 PM -
basic question about files
By oregon in forum New To JavaReplies: 1Last Post: 08-05-2007, 02:34 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks