3D-kreativ
3D-kreativ

Reputation: 9319

About GUI in Java?

I'm trying to learn GUI in Java, but I'm a little bit confused and wondering whats the difference between this code to add components in a window? When I view code examples it varies a lot, some examples use JPanel and just add by writing: panel.add(something); Some code just use add(something); or contentPane.add(something); I'm just curious. Thanks!

JList text;

JPanel panel = new JPanel();
frame.add(panel);
panel.add(text);

-

setLayout(new FlowLayout);
add(text);

-

Container contentPane;
contentPane = getContentPane();
contentpane.setLayout(new FlowLayout);
contentPane.add(text);

Upvotes: 0

Views: 192

Answers (2)

Edwin Buck
Edwin Buck

Reputation: 70949

The differences you are seeing have more to do with the Java language, and less to do with the actual operations being performed.

If you call a method without specifying a particular object, the Java language assumes that you are calling the method on the this object, or the object you are currently working in.

public class MyClass {

  public void doThis() {
  }

  public void doThat() {
    // the following line will call this.doThis()
    doThis();
    // this is exactly the same as the line above, except it is explicitly stated
    this.doThis();
  }

}

Sometimes a method wishes to call a method on a different object. In that case, you have a variable hold a reference to the "other" object, and when you call the method on that object, you must dereference the name reference, one example might look like:

public Class MyOtherClass {

  public void doTheOtherThing(MyClass myClass) {
    myClass.doThis();
  }

}

Different examples do Swing programming differently. Some examples tend to be more object-oriented than others. To illustrate, if you need a "special" button, one example might subclass the button and configure it appropriately within the subclass, while another example might construct a non-subclassed button and configure it from outside of the 'JButton' class.

Good object oriented programming favors subclassing as a solution over external configuration. That is because the code to configure the button becomes part of the new button subclass, and so when one moves the Button into different places architecturally through the program, the configuration code cannot be accidentally separated from the Button. Sometimes analyzing the best object oriented structure can be costly, in which case one might release code with lots of "object-external" influences.

Upvotes: 2

Taymon
Taymon

Reputation: 25676

The difference lies in where the code is located. The second section of code would only make sense inside a method of a class that had setLayout and add methods; most likely this is because the class is a custom GUI component that inherits from JPanel or another Container.

The third section calls getContentPane, which is a method of JFrame, so it most likely inherits from that class.

If you edit your question to post the surrounding context of the example code (or links to it), I (or someone more experienced with Swing than I) might be able to give a more detailed explanation of how it works.

Upvotes: 0

Related Questions