8 Replies - 378 Views - Last Post: 04 August 2013 - 09:12 AM Rate Topic: -----

#1 AndreeU17  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 137
  • Joined: 29-June 13

Having issues with my GUI JFrame?

Posted 03 August 2013 - 10:25 PM

Hi, I decided to create a small notepad using java. I followed a great tutorial from here regarding notepad/text editors. However, since i need practice into the world of GUI (Graphical User Interfaces) i decided to jump into creating a simple notepad. Well i have done this so far from aid of the tutorial and from a book called, "The All-In-One Desk Reference java for dummies book". I have created this:
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


public class jVazquez extends JFrame implements ActionListener{

	public static void main(String[] args) {
			jVazquez tedit = new jVazquez();
			tedit.setVisible(true);
	}
	public void jVazquez(){
		
		//Frame GUI setting
		JFrame frame;
		frame = new JFrame();
		frame.setTitle("jVazquez Text Editor");
		frame.setSize(300, 600);
		frame.setLocation(null);
		frame.setVisible(true);
		//frame.setJMenuBar(JMenubar);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//The end of the Frame GUI Setting
		
		JMenuBar menubar;  //MenuBar sets the bar
		menubar = new JMenuBar();
		frame.add(menubar);
		
		JMenu file; //Menu choice one (File, Edit, Source, etc.)
		file = new JMenu("File");
		menubar.add(file);
		
		JMenuItem menuOpen = new JMenuItem("Open");
		file.add(menuOpen);
		JMenuItem menuSave = new JMenuItem("Save");
		file.add(menuSave);
		JMenuItem menuClose = new JMenuItem("Close");
		file.add(menuClose);
	
	}
            //WANT TO ACCOMPLISH GUI BEFORE GETTING INTO ACTIONLISTENER
	public void actionPerformed (ActionEvent event) {
		/*if(event.getActionCommand()){
		*	
		}*/
	}
}



My question is why my window isnt showing up? Also whats the difference between a JPanel and a JFrame? Must i always use both at the same time or will i be fine if i use either or. Also can someone offer an example when i will need a JPanel or when i'll need a JFrame? Thank YOu

-Jonathan

Is This A Good Question/Topic? 0
  • +

Replies To: Having issues with my GUI JFrame?

#2 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 844
  • View blog
  • Posts: 2,578
  • Joined: 29-July 11

Re: Having issues with my GUI JFrame?

Posted 04 August 2013 - 12:00 AM

A couple of basic mistakes, which I will explain.

public class jVazquez implements ActionListener{

	public static void main(String[] args) {
			jVazquez tedit = new jVazquez();
			//tedit.setVisible(true);
	}
	public jVazquez(){
		
		//Frame GUI setting
		JFrame frame;
		frame = new JFrame();
		frame.setTitle("jVazquez Text Editor");
		frame.setSize(300, 600);
		//frame.setLocation(null);
		
		//frame.setJMenuBar(JMenubar);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//The end of the Frame GUI Setting
		
		JMenuBar menubar;  //MenuBar sets the bar
		menubar = new JMenuBar();
		
		JMenu file; //Menu choice one (File, Edit, Source, etc.)
		file = new JMenu("File");
		
		JMenuItem menuOpen = new JMenuItem("Open");
		file.add(menuOpen);
		JMenuItem menuSave = new JMenuItem("Save");
		file.add(menuSave);
		JMenuItem menuClose = new JMenuItem("Close");
		file.add(menuClose);

		menubar.add(file);
		frame.setJMenuBar(menubar);
		frame.setVisible(true);
	}
            //WANT TO ACCOMPLISH GUI BEFORE GETTING INTO ACTIONLISTENER
	public void actionPerformed (ActionEvent event) {
		/*if(event.getActionCommand()){
		*	
		}*/
	}
}





First, setVisible() should always be the last gui method called.
Second, your UI was not showing up because your class is extending JFrame AND creating a JFrame in what I think you meant to be the constructor. Yo do one or the other.
Third, constructors NEVER have return types, that makes it a method. Constructors are like so:

public JVasquez() {
   //code goes here.
}



Fourth, add your menuitems to your menu, then you add the menu to the menubar. The menubar is then added by calling

frame.setJMenubar(menuBar);



JFrame's a re top level containers, meaning that they are the top of the pyramid. One JFrame cannot contain another. You add your Jpanels and other JComponents to a jFrame. You could add one jpanel to another, but that's a different scenario.

This post has been edited by farrell2k: 04 August 2013 - 12:04 AM

Was This Post Helpful? 2
  • +
  • -

#3 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: Having issues with my GUI JFrame?

Posted 04 August 2013 - 12:27 AM

The difference between JFrames and JPanel is that the first is considered a Top-level (heavy-weight) container and the second is a light-weight container. Top-level containers are used a the root container that holds all gui components. Thus, every swing gui must contain a top-level container. A JFrame is one them. A light-weight container is a general purpose container that groups gui components. If you look on oracle.com you will find great information on using containers as well as swing package in general. About your program, your main class is already extened from a JFrame, which means you don't have to use another JFrame. I would convert that into a JPanel instead.
Was This Post Helpful? 1
  • +
  • -

#4 farrell2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 844
  • View blog
  • Posts: 2,578
  • Joined: 29-July 11

Re: Having issues with my GUI JFrame?

Posted 04 August 2013 - 12:35 AM

View Postsalazar, on 04 August 2013 - 07:27 AM, said:

The difference between JFrames and JPanel is that the first is considered a Top-level (heavy-weight) container and the second is a light-weight container.


Heavyweight and lightweight refer to AWT and Swing, respectively. Swing components are called lightweight because they are written in Java. AWT components are called heavyweight because they are written in c.
Was This Post Helpful? 2
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2198
  • View blog
  • Posts: 5,226
  • Joined: 10-September 10

Re: Having issues with my GUI JFrame?

Posted 04 August 2013 - 02:33 AM

The lineage is also important:

For JFrame:
    * java.lang.Object
          o java.awt.Component
                + java.awt.Container
                      # java.awt.Window
                            * java.awt.Frame
                                  o javax.swing.JFrame


For JPanel:
    * java.lang.Object
          o java.awt.Component
                + java.awt.Container
                      # javax.swing.JComponent
                            * javax.swing.JPanel


Note that both have the same ancestry through "Container" but then split apart into Window and JComponent. The differences caused by this split will become apparent as you use them more.
Was This Post Helpful? 2
  • +
  • -

#6 AndreeU17  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 137
  • Joined: 29-June 13

Re: Having issues with my GUI JFrame?

Posted 04 August 2013 - 02:40 AM

Ohh I understand. So by me extending a JFrame im simply saying I already have a JFrame available. So that's why they use "this.setSize();" rather than what I did. Okay I understood you all completely but as for the constructor not returning a value, us confusing. I though void didnt return a value to start off with.

Thank you All for your reply :)
Was This Post Helpful? 0
  • +
  • -

#7 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Having issues with my GUI JFrame?

Posted 04 August 2013 - 03:35 AM

View PostAndreeU17, on 04 August 2013 - 05:25 AM, said:

	public void actionPerformed (ActionEvent event) {
		/*if(event.getActionCommand()){
		*	
		}*/
	}
}



A note for when you are getting to your ActionListener. I prefer using the getSource instead of getActionCommand. That way it won't be necessary to set an action command for each component you add a action listener to.

open.addActionListener(this);



    @Override
    public void actionPerformed(ActionEvent e) {
        Object o = e.getSource();
        if(o == open)
            System.out.println("Open has been clicked");
    }



You, would have to:
open.addActionListener(this);
open.setActionCommand("Open");



    @Override
    public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();
        if(cmd.equals("Open"))
            System.out.println("Open has been clicked");
    }



It's pretty much the same thing. It will save you time to not have to set an action command for each menu item and just get it from the source.

Although the getActionCommand would be useful if you ever need text field commands, which would be a case where you wouldn't have to set the command

This post has been edited by schutzzz: 04 August 2013 - 03:43 AM

Was This Post Helpful? 0
  • +
  • -

#8 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2005
  • View blog
  • Posts: 4,171
  • Joined: 11-December 07

Re: Having issues with my GUI JFrame?

Posted 04 August 2013 - 04:08 AM

A lot of good advice in this thread. Just a couple of quick clarifications:

	public static void main(String[] args) {
			jVazquez tedit = new jVazquez();
			tedit.setVisible(true);
	}



This is actually OK. You need to make sure you call setVisible(true) after you have added all the other components to the JFrame. By doing it outside the constructor, you are ensuring that. I think this is the best place for it. The windows in Java don't force themselves to be visible on instantiation and neither should yours. What if your JVazquez class becomes part of a larger program. That program will want to control the visibility. More likely, what if you want to subclass JVazquez. You don't want to make it visible until after the subclasses constructor runs.

Quote

A note for when you are getting to your ActionListener. I prefer using the getSource instead of getActionCommand.


There is a third option which is creating a separate ActionListener for every component:

button1.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		System.out.println("Button 1 pressed.");
	}
});

button2.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		System.out.println("Button 2 pressed.");
	}
});



ActionListener sharedListener = new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		callSomeOtherMethod();
	}
};

button3.addActionListener(sharedListener);
button4.addActionListener(sharedListener);


This is more object orientated. If you think about it, why should an ActionListener care about what GUI component caused the action. It should trust the programmer to have added it to the correct component or components. If the action is more than just a couple of lines then consider creating a whole new class for it.

The other methods do have their uses. You should think about which one is the best for your case. An example for the getSource moght be if you have an array of buttons and just want to use 1 action listener for all of them.

I've never used the action command version but I can see how it makes the whole thing more slightly decoupled from the getSource method. Imagine you don't know how you want your gui to look but you know how you want it to behave. You can create an abstract superclass with all the correct behaviour but no gui components. You can write your action listener to respond to action commmands and then your subclasses can create buttons, text fields, etc that use those commands.

Or maybe action commands can be used to change the behaviour of a Next button to a Finish button in a wizard.

Certainly, there are other, better, ways to do these.
Was This Post Helpful? 1
  • +
  • -

#9 AndreeU17  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 137
  • Joined: 29-June 13

Re: Having issues with my GUI JFrame?

Posted 04 August 2013 - 09:12 AM

View Postcfoley, on 04 August 2013 - 05:08 AM, said:

A lot of good advice in this thread. Just a couple of quick clarifications:

	public static void main(String[] args) {
			jVazquez tedit = new jVazquez();
			tedit.setVisible(true);
	}



This is actually OK. You need to make sure you call setVisible(true) after you have added all the other components to the JFrame. By doing it outside the constructor, you are ensuring that. I think this is the best place for it. The windows in Java don't force themselves to be visible on instantiation and neither should yours. What if your JVazquez class becomes part of a larger program. That program will want to control the visibility. More likely, what if you want to subclass JVazquez. You don't want to make it visible until after the subclasses constructor runs.

Quote

A note for when you are getting to your ActionListener. I prefer using the getSource instead of getActionCommand.


There is a third option which is creating a separate ActionListener for every component:

button1.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		System.out.println("Button 1 pressed.");
	}
});

button2.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		System.out.println("Button 2 pressed.");
	}
});



ActionListener sharedListener = new ActionListener() {
	public void actionPerformed(ActionEvent e) {
		callSomeOtherMethod();
	}
};

button3.addActionListener(sharedListener);
button4.addActionListener(sharedListener);


This is more object orientated. If you think about it, why should an ActionListener care about what GUI component caused the action. It should trust the programmer to have added it to the correct component or components. If the action is more than just a couple of lines then consider creating a whole new class for it.

The other methods do have their uses. You should think about which one is the best for your case. An example for the getSource moght be if you have an array of buttons and just want to use 1 action listener for all of them.

I've never used the action command version but I can see how it makes the whole thing more slightly decoupled from the getSource method. Imagine you don't know how you want your gui to look but you know how you want it to behave. You can create an abstract superclass with all the correct behaviour but no gui components. You can write your action listener to respond to action commmands and then your subclasses can create buttons, text fields, etc that use those commands.

Or maybe action commands can be used to change the behaviour of a Next button to a Finish button in a wizard.

Certainly, there are other, better, ways to do these.


thank you for the help and its crazy how java is so strong to create all these different possibilities. I would love to code my program in 1 class but then after done i would clean it up with multiple lasses and what not...

Thank you i figured out my problem
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1