Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: 'Run' isn't 'Running'

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 'Run' isn't 'Running'

    Hello.
    This is my first time dealing with GUI in Java.
    I'm using Eclipse (Galileo) with the Visual Editor.
    I tried Running my application (not really an application, just a Frame with a Button) both with and without the mains class (as Java Application and java Beans, respectively), but I can preview my frame and button. The application does run, because if I put some simple commands on mains it does run in the console, but the GUI doesn't 'load'.
    Anyone knows what I'm doing wrong? I'm sure its a simple configuration mistake.

    Thanks!


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 'Run' isn't 'Running'

    Please post your code and we can help you find where the problem is.

    Likely this is what I think the problem could be: You forgot to set the JFrame to be visible.

  3. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: 'Run' isn't 'Running'

    I did set the frame as visible.
    Now that I see I think I'm doing something wrong with the layout. I set the layout as null for the frame and ContentPane, but when I try to add a component (the Button) it is awkward and I cannot simply drag and drop. When I click on a component to add it and then click on the Pane the component gets added just besides the pane and I cant drag-and-drop to the pane.

    When I run as Bean (after removing mains) I get the following in the console.
    "IWAV0048I Java Bean VisualTest started with null constructor"


    Here is the code (all generated by the VE):
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import javax.swing.JButton;
    import javax.swing.JLabel;
    public class VisualTest {
     
    	private JFrame jFrame = null;  //  @jve:decl-index=0:visual-constraint="125,54"
    	private JPanel jContentPane = null;
    	private JButton jButton = null;  //  @jve:decl-index=0:visual-constraint="424,159"
    	/**
    	 * This method initializes jFrame	
    	 * 	
    	 * @return javax.swing.JFrame	
    	 */
    	private JFrame getJFrame() {
    		if (jFrame == null) {
    			jFrame = new JFrame();
    			jFrame.setSize(new Dimension(298, 187));
    			jFrame.setVisible(true);
    			jFrame.setContentPane(getJContentPane());
    		}
    		return jFrame;
    	}
     
    	/**
    	 * This method initializes jContentPane	
    	 * 	
    	 * @return javax.swing.JPanel	
    	 */
    	private JPanel getJContentPane() {
    		if (jContentPane == null) {
    			jContentPane = new JPanel();
    			jContentPane.setLayout(null);
    		}
    		return jContentPane;
    	}
     
    	/**
    	 * This method initializes jButton	
    	 * 	
    	 * @return javax.swing.JButton	
    	 */
    	private JButton getJButton() {
    		if (jButton == null) {
    			jButton = new JButton();
    		}
    		return jButton;
    	}
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
     
    	}
     
    }

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 'Run' isn't 'Running'

    In Java, the program entry point is always the main method (well, applets are a special case. You don't write the main method yourself.).

    A second comment is that you should really create a constructor instead of having 3 methods which create components of the GUI.

    import java.awt.Dimension;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class VisualTest
    {
     
    	private JFrame	jFrame;
    	private JPanel	jContentPane;
    	private JButton	jButton;
     
    	public VisualTest()
    	{
    		jFrame = new JFrame();
    		jContentPane = new JPanel();
    		jButton = new JButton("button");
    		jContentPane.setPreferredSize(new Dimension(298, 187));
    		jContentPane.add(jButton);
    		jFrame.setContentPane(jContentPane);
    		jFrame.pack();
    		jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
     
    	/**
    	 * This method initializes jFrame
    	 * 
    	 * @return javax.swing.JFrame
    	 */
    	public JFrame getJFrame()
    	{
     
    		return jFrame;
    	}
     
    	/**
    	 * This method initializes jContentPane
    	 * 
    	 * @return javax.swing.JPanel
    	 */
    	private JPanel getJContentPane()
    	{
    		return jContentPane;
    	}
     
    	/**
    	 * This method initializes jButton
    	 * 
    	 * @return javax.swing.JButton
    	 */
    	private JButton getJButton()
    	{
    		return jButton;
    	}
     
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args)
    	{
    		// calls the VisualTest constructor
    		VisualTest v = new VisualTest();
    		v.getJFrame().setVisible(true);
    	}
     
    }

  5. The Following User Says Thank You to helloworld922 For This Useful Post:

    leonsas (December 27th, 2010)

  6. #5
    Junior Member
    Join Date
    Dec 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: 'Run' isn't 'Running'

    Thanks. I thought Java Beans worked without calling the GUI from main.

    A second comment is that you should really create a constructor instead of having 3 methods which create components of the GUI.
    May I ask why? It seems easier to work with the way the editor parses the code, without the constructor.

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: 'Run' isn't 'Running'

    Ah, I didn't see the part about where you're using a tool to generate the code. I usually write my GUI's by hand, and the above code is what I would do by hand.

    I guess it's ok with this code if it's generated, but I think it's a rather dumb way to generate the objects by calling the getter methods. I gave that tool a test, and it did generate the constructor for VisualTest. It also created VisualTest as a subclass of JFrame (I like this method because you have direct access to all the JFrame members).

    Using that tool I generated this code (note that I still had to write the main method):

    import java.awt.BorderLayout;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class VisualTest extends JFrame
    {
     
    	private static final long	serialVersionUID	= 1L;
    	private JPanel				jContentPane		= null;
    	private JButton				jButton				= null;
     
    	/**
    	 * This is the default constructor
    	 */
    	public VisualTest()
    	{
    		super();
    		initialize();
    	}
     
    	/**
    	 * This method initializes this
    	 * 
    	 * @return void
    	 */
    	private void initialize()
    	{
    		this.setSize(300, 200);
    		setContentPane(getJContentPane());
    		setTitle("JFrame");
    	}
     
    	/**
    	 * This method initializes jContentPane
    	 * 
    	 * @return javax.swing.JPanel
    	 */
    	private JPanel getJContentPane()
    	{
    		if (jContentPane == null)
    		{
    			jContentPane = new JPanel();
    			jContentPane.setLayout(new BorderLayout());
    			jContentPane.add(getJButton(), BorderLayout.CENTER);
    		}
    		return jContentPane;
    	}
     
    	/**
    	 * This method initializes jButton
    	 * 
    	 * @return javax.swing.JButton
    	 */
    	private JButton getJButton()
    	{
    		if (jButton == null)
    		{
    			jButton = new JButton();
    		}
    		return jButton;
    	}
     
    	public static void main(String[] args)
    	{
    		VisualTest v = new VisualTest();
    		v.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		v.setVisible(true);
    	}
    }

Similar Threads

  1. Problem running .jar in Windows 7
    By SpiceProgrammer in forum Java Theory & Questions
    Replies: 3
    Last Post: December 21st, 2011, 12:28 AM
  2. Running a external exe in Mac OS
    By supertreta in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: November 15th, 2010, 12:32 PM
  3. Runtime Error running in UVA
    By mathfxr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 17th, 2010, 02:06 PM
  4. Help running SQL queries in Ubuntu
    By Mic[RSA] in forum Java IDEs
    Replies: 0
    Last Post: June 29th, 2010, 12:53 PM
  5. Replies: 1
    Last Post: March 3rd, 2009, 07:04 AM