#31
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Location
    Chicago, Illinois, U.S.A.
    Posts
    98
    Rep Power
    0
    Okay I gave it a try, but I actually can't get the java command to work in the command prompt.

    java <anything> returns

    Error: Could not find or load main class Program.java

    (But the javac command works, just not java.). I thought I got some stuff fixed in my %paths.

    But anyways, when I added that into my main function, and I run it, I get 2 GUIs, my JPanel, and your JFrame.

    After doing all that Manifest.txt and jar commands, double-clicking on the file doesn't popup any of the GUIs.

    However, then I decided to just create a new Java file called Test.java and putted your code in, and adjusted the Manifest.txt too.

    Originally Posted by MrFujin
    Code:
    > java Program.java
    > jar cfm Program.jar Manifest.txt Program.class
    I didn't get to perform both commands, just the 2nd 1.

    And this time, it works, the Test.java pops up your blank JFrame window.

    That means something in my code prevents even the JFrame to pop up. I don't know if not having used the java command in the command prompt is preventing it. It still could be code related.

    Basically, I'd like to be able to double-click and get your JFrame to popup in my main. And once that happens, then I would just convert my JPanel into the JFrame. Basically use JFrame instead of JPanel.

    I have a "extends JFrame" in my code, but no JFrame.

    It looks like this.

    import stuff

    and then

    public class Program extends JFrame

    And then a bunch of declarations, like JPanel this and JButton that.

    And then a public Program()

    And then in my main, I still have the

    Program p = new Program();

    I'd prefer it if I put it all in 1 main function rather than all these classes and methods, but several commands won't work and I wouldn't know if there were alternatives.

    But, if you think not being able to use the java command in the command prompt is an issue, I should work on that 1st.

    Thanks.
    Last edited by LostShadow; January 15th, 2014 at 02:18 PM.
  2. #32
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,576
    Rep Power
    1906
    I made a mistake in my previous post. To compile the Java code you should call 'javac', not 'java'.
    If you compile the Java file in the IDE, then you don't have to call it manually.

    If you have any issues with your code, please post it.
    Remember to use the code tags, as seen in my post.
  4. #33
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Location
    Chicago, Illinois, U.S.A.
    Posts
    98
    Rep Power
    0
    Well I found a way to get the JFrame to work, just not the JPanel, from double-clicking the .jar file.

    In my main I had:

    Code:
       public static void main(String[] args)
       {
          Program pm = new Program();
          JFrame myGUI = new JFrame("TEST");
          myGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          myGUI.setPreferredSize(new Dimension(175, 100));
          myGUI.pack();
          myGUI.setVisible(true);
       }
    From here, the JFrame did not load.

    I decided to // the Program pm line, and then jar'd the file again, and the JFrame worked.

    So then I moved the Program line to the end.

    Code:
       public static void main(String[] args)
       {
          JFrame myGUI = new JFrame("TEST");
          myGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          myGUI.setPreferredSize(new Dimension(175, 100));
          myGUI.pack();
          myGUI.setVisible(true);
          Program pm = new Program();
       }
    And now the JFrame loads.

    So, this was a trick I learned years back, that the order of the code matters. But I think for something like this, something could be written better in a way that the order shouldn't matter.

    Now that I got the JFrame to work, and all my GUI was in a JPanel or 2, I tried to convert the JPanel into a JFrame.

    If I replace the word JPanel to JFrame everywhere in my code, building/compiling it is still just fine, no errors, but running it does.

    The errors I get from running are.

    Code:
    TestException in thread "main" java.lang.IllegalArgumentException: adding a window to a container
    	at java.awt.Container.checkNotAWindow(Container.java:483)
    	at java.awt.Container.addImpl(Container.java:1084)
    	at java.awt.Container.add(Container.java:998)
    	at javax.swing.JFrame.addImpl(JFrame.java:562)
    	at java.awt.Container.add(Container.java:966)
    	at Program.<init>(Program.java:62)
    	at Program.main(Program.java:219)
    So lines 62 and 219 are

    add(panel, BorderLayout.NORTH);

    and

    Program pm = new Program();

    I'd like to convert this whole code into only the main function, do you think it's possible? Remember, my public class Program has a "extends JFrame."

    When I remove the "extend JFrame" and compile, I get error messages at the same add() line:

    Code:
    error: cannot find symbol
          add(panel, BorderLayout.NORTH);
          ^
      symbol:   method add(JFrame,String)
      location: class Program
    And this next line I get in error whether I remove the extends JFrame or cut/paste the whole code into the main function:

    redButton.addActionListener(new RedButtonListener());

    Where the "new" causes the problem.

    Thanks.
  6. #34
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,576
    Rep Power
    1906
    Having the Program class extend JFrame, you can have your main like this:
    Code:
       public static void main(String[] args)
       {
          Program myGUI = new Program(); 
          myGUI.setTitle("TEST");
          myGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          myGUI.setPreferredSize(new Dimension(175, 100));
          myGUI.pack();
          myGUI.setVisible(true);
    
       }
  8. #35
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Location
    Chicago, Illinois, U.S.A.
    Posts
    98
    Rep Power
    0
    Originally Posted by MrFujin
    Code:
       public static void main(String[] args)
       {
          Program myGUI = new Program(); 
          myGUI.setTitle("TEST");
          myGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          myGUI.setPreferredSize(new Dimension(175, 100));
          myGUI.pack();
          myGUI.setVisible(true);
    
       }
    Well, you just removed the JFrame now. So there was no chance that it worked on double-click. Now, when I build and run this, I get a much smaller GUI. And it's due to this line right here:

    myGUI.setPreferredSize(new Dimension(175, 100));

    Then I //'d it. Then I rebuild and rerun. Then, the GUI popups up much bigger, as it was before.

    That's because I already declared the size earlier in the code. Then I realized all the code you had there like setsize and settitle I already had earlier, so I removed all of it.

    Now with this left.

    Program myGUI = new Program();

    That's actually the same as anyword.

    In fact, I changed it to

    JFrame myGUI = new Program(); just to see if anything changed and it didn't.

    As for mentioning earlier about the "extends JFrame" I already had that in my code, so otherwise removing the JFrame myGUI didn't help it.

    Now, I suppose if it did work on double-click, then all the myGUI.setcommands would apply for that pop-up, whereas when you run it, my old JPanel GUI popped up.

    In fact, that's what it was earlier when you 1st told me about the JFrame. At run, I would get 2 GUIs, my JPanel and your JFrame (at the same time). And by double-clicking the jar file, I would only get the JFrame GUI. Now that there is no JFrame GUI then nothing pops up at double-click.

    Thanks.
  10. #36
  11. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,576
    Rep Power
    1906
    Can you post the complete code for Program?
    Last edited by MrFujin; January 17th, 2014 at 03:07 PM.
  12. #37
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Location
    Chicago, Illinois, U.S.A.
    Posts
    98
    Rep Power
    0
    Okay have fun with this. I renamed this to ProgramGUI.

    Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class ProgramGUI extends JFrame
    {
       
       final int WINDOW_WIDTH = 250;
       final int WINDOW_HEIGHT = 150;
       
     //  JFrame window = new JFrame();
          
       JLabel messageLabel;
       JButton redButton;
       JButton blueButton;
       JButton greenButton;
       JButton purpleButton;
       JPanel panel;
       JPanel panel2;
       
       JMenuBar menuBar;
       JMenu fileMenu;
       JMenu textMenu;
       JMenuItem aboutItem;
       JMenuItem exitItem;
       JRadioButtonMenuItem redItem;
       JRadioButtonMenuItem blueItem;
       JRadioButtonMenuItem greenItem;   
       JRadioButtonMenuItem purpleItem;
       JRadioButtonMenuItem blackItem;
       
       JTextArea wordPad;
       JScrollPane scrollPane;
       
       public ProgramGUI()
       {   
    
         //setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
          //setLayout(new FlowLayout(FlowLayout.CENTER));
          messageLabel = new JLabel("Click a button to select a color.", SwingConstants.CENTER);
          messageLabel.setPreferredSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT));
          //messageLabel.setFont(new Font("Arial", Font.PLAIN, 16));
          redButton = new JButton("Red");
          blueButton = new JButton("Blue");
          greenButton = new JButton("Green");
          purpleButton = new JButton("Purple");
    
          JTextArea wordPad = new JTextArea("Text", 20, 40);
          JScrollPane scrollPane = new JScrollPane(wordPad);
       //   scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
       //   scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
     
          panel = new JPanel();
          panel2 = new JPanel();
          panel.add(messageLabel);
          panel.add(redButton);
          panel.add(blueButton);
          panel.add(greenButton);
          panel.add(purpleButton);
          panel2.add(wordPad);
        //  panel2.add(scrollPane);
          add(panel, BorderLayout.NORTH);
          add(panel2, BorderLayout.SOUTH);
          
          redButton.addActionListener(new RedButtonListener());
          blueButton.addActionListener(new BlueButtonListener());
          greenButton.addActionListener(new GreenButtonListener());
          purpleButton.addActionListener(new PurpleButtonListener());
          
          setTitle("Menu System");
          buildMenuBar();
          pack();
          setVisible(true);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }
    
       public class RedButtonListener implements ActionListener
       {
          public void actionPerformed(ActionEvent e)
          {
             panel.setBackground(Color.red);
             panel2.setBackground(Color.red);
          }
       }
       public class BlueButtonListener implements ActionListener
       {
          public void actionPerformed(ActionEvent e)
          {
             panel.setBackground(Color.blue);
             panel2.setBackground(Color.blue);
          }
       }
       public class GreenButtonListener implements ActionListener
       {
          public void actionPerformed(ActionEvent e)
          {
             panel.setBackground(Color.green);
             panel2.setBackground(Color.green);
          }
       }
       public class PurpleButtonListener implements ActionListener
       {
          public void actionPerformed(ActionEvent e)
          {
             panel.setBackground(Color.magenta);
             panel2.setBackground(Color.magenta);
          }
       }
       
       public void buildMenuBar()
       {
          menuBar = new JMenuBar();
          
          buildFileMenu();
          buildTextMenu();
         
          menuBar.add(fileMenu);
          menuBar.add(textMenu);
          setJMenuBar(menuBar);
       }
       
       public void buildFileMenu()
       {
          aboutItem = new JMenuItem("About");
          aboutItem.addActionListener(new AboutListener());
       
          exitItem = new JMenuItem("Exit");
          exitItem.setMnemonic(KeyEvent.VK_X);
          exitItem.addActionListener(new ExitListener());
       
          fileMenu = new JMenu("File");
          fileMenu.setMnemonic(KeyEvent.VK_F);
          fileMenu.add(aboutItem);
          fileMenu.add(exitItem);
       }
       
       public void buildTextMenu()
       {
          redItem = new JRadioButtonMenuItem("Red");
          redItem.setMnemonic(KeyEvent.VK_R);
          redItem.addActionListener(new ColorListener());
       
          blueItem = new JRadioButtonMenuItem("Blue");
          blueItem.setMnemonic(KeyEvent.VK_U);
          blueItem.addActionListener(new ColorListener());
       
          greenItem = new JRadioButtonMenuItem("Green");
          greenItem.setMnemonic(KeyEvent.VK_G);
          greenItem.addActionListener(new ColorListener());
       
          purpleItem = new JRadioButtonMenuItem("Purple");
          purpleItem.setMnemonic(KeyEvent.VK_P);
          purpleItem.addActionListener(new ColorListener());
       
          blackItem = new JRadioButtonMenuItem("Black");
          blackItem.setMnemonic(KeyEvent.VK_B);
          blackItem.addActionListener(new ColorListener());
       
          ButtonGroup group = new ButtonGroup();
          group.add(redItem);
          group.add(blueItem);
          group.add(greenItem);
          group.add(purpleItem);
          group.add(blackItem);
       
          textMenu = new JMenu("Text");
          textMenu.setMnemonic(KeyEvent.VK_T);
       
          textMenu.add(redItem);
          textMenu.add(blueItem);
          textMenu.add(greenItem);
          textMenu.add(purpleItem);
          textMenu.addSeparator();
          textMenu.add(blackItem);
       }
       
       public class AboutListener implements ActionListener
       {
          public void actionPerformed(ActionEvent e)
          {
             JOptionPane.showMessageDialog(null, "Made by myself.");
          }
       }
       
       public class ExitListener implements ActionListener
       {
          public void actionPerformed(ActionEvent e)
          {
             System.exit(0);
          }
       }
       
       public class ColorListener implements ActionListener
       {
          public void actionPerformed(ActionEvent e)
          {
             if (redItem.isSelected())
                messageLabel.setForeground(Color.red);
             else if (blueItem.isSelected())
                messageLabel.setForeground(Color.blue);
             else if (greenItem.isSelected())
                messageLabel.setForeground(Color.green);
             else if (purpleItem.isSelected())
                messageLabel.setForeground(Color.magenta);
             else if (blackItem.isSelected())
                messageLabel.setForeground(Color.black);
          }
       }
       
       public static void main(String[] args)
       {
          JFrame myGUI = new ProgramGUI();
          //JFrame myGUI = new JFrame("TEST");
          //myGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          //myGUI.setPreferredSize(new Dimension(175, 100));
          //myGUI.pack();
          //myGUI.setVisible(true);
       }
    }
  14. #38
  15. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,576
    Rep Power
    1906
    You forgot to tell that you use several inner classes for the differenet listeners.
    You have to include all class files in the Jar-file

    I got it working with these commands:
    Code:
    >javac ProgramGUI.java
    
    >jar -cfm ProgramGUI.jar Manifest.txt *.class
    
    >java -jar ProgramGUI.jar
  16. #39
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Location
    Chicago, Illinois, U.S.A.
    Posts
    98
    Rep Power
    0
    Wow, that's pretty sweet, I got it working.

    I even made some using only JOptionPanes.

    Is there like different types of jar files? Jar files and, executable jar files?

    Thanks.
  18. #40
  19. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,576
    Rep Power
    1906
    There is only this one type of Jar file.

    If you by "executable" mean "being able to double click", then it should be working.
    It doesn't work on your system?

    Otherwise, please explain what you mean with "executable".
  20. #41
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Location
    Chicago, Illinois, U.S.A.
    Posts
    98
    Rep Power
    0
    Hey, can we talk about, compatibility here?

    I can't get a different 1 to work on another computer.

    I made another jar file that only has JOptionPane.

    The jar file executes on my computer (Windows 7), but not on another Windows (XP).

    I moved the jar file into a stick drive and the stick drive into another computer.

    I loaded my stick drive in new computer, and double click the jar file and nothing happens. I then copy the jar file into a folder into the computer and double click, still nothing.

    This is a computer that does not have a java compiler installed, like jGrasp, so no Java SE either.

    But it does have some kind of java to view java applets on web pages.

    Thanks.
  22. #42
  23. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,576
    Rep Power
    1906
    Can you run java command?

    Sounds like JRE is not installed correctly/properly.

    Try look in the environment PATH variable and see whether the java/bin directory has been added.
  24. #43
  25. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Location
    Chicago, Illinois, U.S.A.
    Posts
    98
    Rep Power
    0
    Okay, I stick the stick drive into my old laptop, double click the jar file, just an error sound, no JOptionPane.

    Stick the stick drive back into my new laptop, and it works.

    So I decided to try to jar the file again in the other computer. I copy the .java, .class, and Manifest.txt into the stick drive. And on the other computer, none of the commands work in the command prompt.

    So I was going to ask about C++. Say I made the equivalent of JOptionPane in C++.

    For Java it's .java --> .class --> .jar.

    I don't know for C++, but let's say it's .cpp --> ?? --> .exe.

    I suppose C++ doesn't solve this problem either, because if I copy/paste a .exe file (even if it's just popup windows), I don't suspect it can work on a new computer because you would have to install/setup a new program, right?

    So now's a Windows question... is there a way to make popup windows (like JOptionPane) in Windows? Or other than programming and compiling?
  26. #44
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,711
    Rep Power
    347
    Do all the PCs have the same version of java? Java 1.7 programs will not execute with java 1.6
  28. #45
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2005
    Location
    Chicago, Illinois, U.S.A.
    Posts
    98
    Rep Power
    0
    See, I'm a very applied person. What good is knowing all this theoretical stuff, if you can't apply it to anything?

    What good is knowing all this math, when you can't do anything with it. If you were a mechanical or civil engineer, you learn math to help you become a mechanical and civil engineer. But if not for that, what good is pure theoretical math good for?

    So I posted a thread on the Windows help section. Asking for help on how to make alert/popups like JOptionPane.

    So essentially, the closest I can think of is just open up Notepad file and use that as a popup-window...

IMN logo majestic logo threadwatch logo seochat tools logo