Results 1 to 7 of 7
  1. #1
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default 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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  3. #3
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    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
    Java 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);
    
        }
    }
    and

    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]
      }
    
    }

  4. #4
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    My JAR file (GameTestJAR.jar) contains:

    kevin\StartingFrame.class
    kevin\StartingPanel.class
    META-INF\MANIFEST.MF

    Here is my manifest file:

    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
    This manifest file works fine when I don't include the ActionListener part of the code.

    My package name is kevin obviously.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default

    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

  6. #6
    Psyclone is offline Member
    Join Date
    Jan 2010
    Posts
    81
    Rep Power
    0

    Default

    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?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Replies: 8
    Last Post: 07-30-2010, 05:47 PM
  2. 2 Part question about cfg xml and dat files
    By Samgetsmoney in forum New To Java
    Replies: 0
    Last Post: 02-18-2009, 02:36 AM
  3. Replies: 1
    Last Post: 12-07-2008, 05:50 PM
  4. Behaving text files like binary files
    By Farzaneh in forum New To Java
    Replies: 2
    Last Post: 08-27-2008, 03:20 PM
  5. basic question about files
    By oregon in forum New To Java
    Replies: 1
    Last Post: 08-05-2007, 02:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •