[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


JFrame questions .......

Posted on 2004-08-31
Medium Priority
Last Modified: 2013-11-23
Hi Experts,

   I got some questions in the following code :
import javax.swing.*;   // need this for JPanel, JButton and JLabel
import java.awt.*;     // need this for BorderLayout
import java.awt.event.*;  // need this for ActionListenjer and ActionEvent

public class ClickPanel extends JPanel implements ActionListener {

  private JButton clickButton;
  private JLabel countLabel;
  private int n;

  public ClickPanel() {
    setLayout(new BorderLayout());
    clickButton = new JButton("Click me!");
    add(clickButton, BorderLayout.CENTER);            // line 01

    n = 0;  // n is the number of clicks by the user
    countLabel = new JLabel("Number of Clicks: "+n+"      ");   // the extra spaces on the end are necessary to
                                                                // make sure that numbers of more than two digits
                                                                // will be visible
    add(countLabel, BorderLayout.SOUTH);

  // this function has to be implemented, since this class implements ActionListener
  // which requires the void actionPerformed(ActionEvent e).
  // It only listens to clickButton

  public void actionPerformed(ActionEvent e) {                     //line 02
    // we know that the ActionEvent was generated by ClickButton, but just to make sure
    // we are going to check it by calling the method ActionEvent.getSource():
    if (e.getSource() == clickButton) {
      countLabel.setText("Number of Clicks: "+n);

  public static void main(String[] arg) {
    // create a frame to show this panel in:
    JFrame f = new JFrame("Example 2");
    f.getContentPane().add(new ClickPanel());
    f.pack();                                                                           // line 03
    f.show();                                                                          // line 04
Q1. In line 01, how come the add() can be invoked directly ? I mean: why don't we need an object such as :                
      MyObject.add() ?

Q2. In line 02, is " public void actionPerformed(ActionEvent e) " a function of the interface "ActionListener" ?
Q3. In line 03 & 04, at the standard Java tutorial of sun website : In the JFrame class, I can not find the pack() & show() function ! where do these two functions come from ? and what do they do exactly ? thanks !
Question by:meow00
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +1
LVL 35

Accepted Solution

TimYates earned 1000 total points
ID: 11942511
Q1.  Because you are adding it to "this", ie, you are calling add on the current "ClickPanel" object
Q2.  Yes
Q3.  pack() and show() are from java.awt.Window which is a class that JFrame extends...
LVL 35

Expert Comment

ID: 11942533
To expand Q1;

Because you are extending JPanel, you get to call all of JPanel (and it's superclasses) public methods as if they were your own.  So calling

   add(clickButton, BorderLayout.CENTER);

is the same as calling

   this.add(clickButton, BorderLayout.CENTER);

ie; you are calling the add method for this JPanel...

Assisted Solution

RuadRauFlessa earned 600 total points
ID: 11942602
A1. add is a method within the JPanel class and since you extend from JPanel you can call it directly. You can also use the super.add() method and it will do the same thing.

A2. Hmm now let us see do you have a clue as to what an API is fore. go to http://java.sun.com/j2se/1.4.2/docs/api/java/awt/event/ActionListener.html and see for yourself.

A3. Again take a look at the API's at http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JFrame.html pack() and show() is in the java.awt.Window class. You can find them just below the method descriptions of the class itself where it shows all of the inherited methods.


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


Expert Comment

ID: 11942627
I tend to find that the people here really underestimate what they can find in the API's. It is the biggest source of information as to what is really going on within a class and can be used to do anything. Yes anything if you can read it correctly you can teach yourself how to use any java component/class ever written.
LVL 37

Expert Comment

ID: 11942734
>> I tend to find that the people here really underestimate what they can find in the API's
I tend to agree.
On the other hand, why should they read the API's docs if they can get the answers in minutes here at EE. ;°)

Expert Comment

ID: 11942832
I'd rather have someone be able to think for themselves than spundge on others. Finding those links that I have posted above is as easy as fall out of a tree and would be way quicker than loggin on here and asking a question and then having to wait for an answer and then in the end of the day waste money. But then again if the are really that desperate to give out points I'd say go for it. The only things I would post a q for is when I have tried, tried, tried and tried again, and then came across a problem that I cannot solve by means of the API which would generally be something like logic. Not academic. When working in any form of Object Oriented Language you should know these type of things.

In any case this looks more like a school homework question to me than anything else.

meow00, sorry if I am a bit harshe here pal. Not meaning to blast anyone. I just find that people tend to go out and ask someone else something when they could have figured it out for themselves in the same time and learned something from it. If you do not look fo yourself you may as well give up trying to program. Next time when you want to know about wether a method belongs to a specific class go to http://java.sun.com/j2se/1.4.2/docs/api/index.html and check it out.

LVL 37

Expert Comment

ID: 11942914
I completely see your point. But that's how life is here at EE.
Of course nobody forces you to answer such questions.
Or even read them.
Or even read questions at all.
Or even login to EE.

Expert Comment

ID: 11947902
I am actually wondering why Rua is on EE if thats how he feels.  

I see his point about the learning process and that is important, but sometimes, some people, need a little push.  Its a fact some people need that push, others go spend donkees years trying to work something out instead of asking someone if they can.  There is nothing bad about asking questions, i am sure you have done it before even if you had spent hours trying to figure it out.

There is an element of laziness on the other hand for those who do ask the questions.  But i can tell you this, I was an electrical engineer for 12 years before i started on my degree.  I remember someone telling me that lazy people make the best engineers, that applies to getting a job done in good time with the maximum effect. It is a very true concept about the engineers.  Maybe that also appies to IT developers too... Make those shortcuts if you like, if it helps you toward your final goal.

You will still learn the lesson, just a bit quicker than the person who took all day about it because he was too stubborn to ask for friendly advice.  I think EE is excellent in that repect, there are a mixture of people on here some will help more than others, some will point in the right direction.  

All in all the concept works well so why not just enjoy it


Expert Comment

ID: 11949293
Sorry for that I had a bit of a bad day.

I also like the idea of EE. The thing is just that ok they ask questions and rather than just giving them the answer give them a resource by which to figure out the answer and if they then have questions pertaining to the resource and such go on. I feel that the process of learning is just a tad more complex than getting a strait forward answer. You take for example Q3 yes he got his answer but does he really see the point of object oriented concepts such as pertaining to why you can call them or even where they actually come from. Fair enouth that he had a look at the Java tutorial of sun website but that was the wrong place to go and look for a method that you want to use. In mosts of those cases I feel that you can simply point a person into the correct direction and let him or her go and look again. And then if they cannot find anything by all means give it to them.

pjcrooks2000, I have to agree with you that it is the lazy people that are good at the engeneering stuff cuz they tend to build up a resourse of info and re-usable code that they can always make use of again cuz they are too damn lazy to write something again. And that is providing they do it properly the first time round. Quite ironic really that there is one field that lazy ppl can thrive in. By the way programming (Software Engeneering) is an engeering position and would then apply to your hypothesis.

Again sorry about that I just kinda feel a bit diffirent about education and the way that a person learns something than most.

Expert Comment

ID: 11951601
Thanks for that Ruad I think I may have been having one of them days too :)  

It looks like an answer to me Q3.  I think if someone told me that answer I would have learnt something, either way there are many different ways to learn.  Whether that be the lazy route or the long way round.  Perhaps by being lazy, you will allow yourself more time to absorb more snippts that taking far too much time to work it out one single snippet.

Let me put it this way, when I started my Uni course up I had to learn c++, now I undertook the job in small bite sized chunks.  I thought the best way for me to learn it at that time was to read the book very slowly and start right at the beginning.  

Everytime i came back to doing the work I found that i hadn't really learned anything and it was because I was taking too much time learning the early chapters of the book.  Most of it was useless information in the early chapters, and I dare say quite a lot of information throughout most books is useless information.  Book authors usually ramble on about code from their own perspective and try to explain why they have done it.  At the end of the day it dosn't matter why they have done it as long as the reader knows how to do it.  The fact that the reader is there in the first place shows that he wants to learn.

So now I am able to skip a lot of the useless junk that will be no use to me and I can concentrate on "the whats" and "the fors" instead of the "whys".

Anyway, no more rambling chances are no ones reading this :)   take care!
LVL 35

Expert Comment

ID: 11951622
> Anyway, no more rambling chances are no ones reading this :)


Expert Comment

ID: 11951775
HEH or this ;)

Expert Comment

ID: 11952776
Interesting point of view.


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question