# Java GUI help, please?

**URL:** <https://boards.straightdope.com/t/java-gui-help-please/243923>\
**Category:** Factual Questions\
**Created:** [May 6, 2004, 10:46pm UTC](https://boards.straightdope.com/t/java-gui-help-please/243923 "2004-05-06T22:46:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![zev\_steinhardt](https://avatars.discourse-cdn.com/v4/letter/z/97f17d/32.png) [@zev\_steinhardt](https://boards.straightdope.com/u/zev_steinhardt)\
**Post date:** [May 6, 2004, 10:46pm UTC](https://boards.straightdope.com/t/java-gui-help-please/243923/1 "2004-05-06T22:46:26Z")

</div>

I’ve been trying to get my feet wet with Java lately. However, I’m following an example from a book but having trouble getting it to work. Can someone please tell me what I’m doing wrong?

I’m trying to get an orange rectangle to be painted on a window. The code I have is as follows:

```auto

import java.awt.*;
import javax.swing.*;

class MyDrawPanel extends JPanel {

	public static void main(String[] args) {

		MyDrawPanel gui = new MyDrawPanel();
		gui.go();
	 }//close main
	 public void go() {

		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(600,600);
		frame.setVisible(true);

	 }//close go
	public void paintComponet(Graphics g) {

			g.setColor(Color.orange);
			g.fillRect(20,50,100,100);

	}//close paintComponent
}//close class

```

From what I understand, the program is supposed to call paintComponent on it’s own; I don’t have to call it. So, why can’t I get an orange rectangle?

Any help would be greatly appreciated.

Zev Steinhardt

---

<div class="post-metadata">

**Author:** ![Capt.Ridley\_s\_Shooting\_Party](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@Capt.Ridley\_s\_Shooting\_Party](https://boards.straightdope.com/u/Capt.Ridley_s_Shooting_Party)\
**Post date:** [May 6, 2004, 10:57pm UTC](https://boards.straightdope.com/t/java-gui-help-please/243923/2 "2004-05-06T22:57:56Z")

</div>

You spelled component wrong?

---

<div class="post-metadata">

**Author:** ![zev\_steinhardt](https://avatars.discourse-cdn.com/v4/letter/z/97f17d/32.png) [@zev\_steinhardt](https://boards.straightdope.com/u/zev_steinhardt)\
**Post date:** [May 6, 2004, 11:03pm UTC](https://boards.straightdope.com/t/java-gui-help-please/243923/3 "2004-05-06T23:03:47Z")

</div>

:smack: :smack: :smack:

D’oh!

But that’s not it. I fixed the spelling and it still doesn’t work. I get a plain grey window. Any other ideas?

Zev Steinhardt

---

<div class="post-metadata">

**Author:** ![Capt.Ridley\_s\_Shooting\_Party](https://avatars.discourse-cdn.com/v4/letter/c/cc9497/32.png) [@Capt.Ridley\_s\_Shooting\_Party](https://boards.straightdope.com/u/Capt.Ridley_s_Shooting_Party)\
**Post date:** [May 6, 2004, 11:12pm UTC](https://boards.straightdope.com/t/java-gui-help-please/243923/4 "2004-05-06T23:12:46Z")

</div>

I’m taking a guess here as I haven’t really worked with Swing musch, but try calling update() from within the paint method, passing the Graphics object to it.

---

<div class="post-metadata">

**Author:** ![Eegba](https://avatars.discourse-cdn.com/v4/letter/e/7cd45c/32.png) [@Eegba](https://boards.straightdope.com/u/Eegba)\
**Post date:** [May 6, 2004, 11:17pm UTC](https://boards.straightdope.com/t/java-gui-help-please/243923/5 "2004-05-06T23:17:34Z")

</div>

The JFrame is a regular window that you’d see on your screen. But that’s all it is. You need to add your panel to its content pane in order for your panel to be seen.

```auto

	 public void go() {

		JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(600,600);
                this.setSize(600,600);
                frame.getContentPane().add(this); // right here
		frame.setVisible(true);

	 }//close go

```

---

<div class="post-metadata">

**Author:** ![yabob](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/yabob/32/2821_2.png) [@yabob](https://boards.straightdope.com/u/yabob)\
**Post date:** [May 6, 2004, 11:38pm UTC](https://boards.straightdope.com/t/java-gui-help-please/243923/6 "2004-05-06T23:38:16Z")

</div>

Basic misunderstanding here - the paintComponent you overrode is for your JPanel subclass which you aren’t even displaying. The only function it is serving is to define your main(). You create an independent frame and display it.

You may have intended something like this, or what I see **Eegba** suggests, which doesn’t completely override the painting of the whole container, like I’m doing here:

```auto

import java.awt.Graphics;
import java.awt.Color;

import javax.swing.JFrame;

public class MyFrame extends JFrame {
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        try {
            JFrame frame = new MyFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(600,600);
            frame.show();
        } catch (Exception ex) {
            System.err.println(ex);
        }
    }
    
    public void paint(Graphics g) {
        g.setColor(Color.orange);
        g.fillRect(20,50,100,100);
    }
}

```

---

<div class="post-metadata">

**Author:** ![yabob](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/yabob/32/2821_2.png) [@yabob](https://boards.straightdope.com/u/yabob)\
**Post date:** [May 6, 2004, 11:41pm UTC](https://boards.straightdope.com/t/java-gui-help-please/243923/7 "2004-05-06T23:41:29Z")

</div>

Oh, and you don’t need the try … catch - artifact of my cutting and pasting into an existing test file.

---

<div class="post-metadata">

**Author:** ![zev\_steinhardt](https://avatars.discourse-cdn.com/v4/letter/z/97f17d/32.png) [@zev\_steinhardt](https://boards.straightdope.com/u/zev_steinhardt)\
**Post date:** [May 7, 2004, 1:53pm UTC](https://boards.straightdope.com/t/java-gui-help-please/243923/8 "2004-05-07T13:53:02Z")

</div>

Thank you all very much. I added the line of code from **Eegba** ’s example and it worked perfectly. Thank you.

Zev Steinhardt
