Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adding JPanel to JFrame

171 views
Skip to first unread message

BEHROUZ

unread,
Feb 15, 2010, 3:52:19 AM2/15/10
to
Hi Every one,
I am new at java swing and I am trying to add a Jpanel to my frame. I
have three simple class as below
1-Class MyPanel
2-Class MyFrame
3-Class Main
I did my best to add the panel to my frame but it did not work! Could
you please let me know what I am doing wrong!?
//=====================
1.
2. package mypaneltest;
3. import javax.swing.JButton;
4. import javax.swing.JPanel;
5.
6. public class MyPanel extends JPanel {
7. public MyPanel(){
8. JPanel pan = new JPanel();
9. JButton okButton = new JButton("OK");
10. pan.add(okButton);
11. }
12. }
13. //=====================
14. package mypaneltest;
15. import javax.swing.JFrame;
16.
17. public class MyFrame extends JFrame {
18. public MyFrame(){
19. super("Test");
20. setSize(300,200);
21. setLocationRelativeTo(null);
22. MyPanel pane = new MyPanel();
23. add(pane);
24. }
25. }
26. //======================
27. package mypaneltest;
28.
29. public class Main {
30. public static void main(String[] args) {
31. new MyFrame().setVisible(true);
32. }
33. }

Best Regards

RedGrittyBrick

unread,
Feb 15, 2010, 5:18:49 AM2/15/10
to
On 15/02/2010 08:52, BEHROUZ wrote:
> 2. package mypaneltest;
> 3. import javax.swing.JButton;
> 4. import javax.swing.JPanel;
> 5.
> 6. public class MyPanel extends JPanel {
> 7. public MyPanel(){
> 8. JPanel pan = new JPanel();
> 9. JButton okButton = new JButton("OK");
> 10. pan.add(okButton);

Note pan is a separate instance of JPanel from that constructed by MyPanel

Delete 8. and replace 109 with
add(okButton)


Note that your should also construct your GUI on the EDT - you will
have problems later if you don't.

You should also pack() your JFrame.

> 11. }
> 12. }
> 13. //=====================
> 14. package mypaneltest;
> 15. import javax.swing.JFrame;
> 16.
> 17. public class MyFrame extends JFrame {
> 18. public MyFrame(){
> 19. super("Test");
> 20. setSize(300,200);
> 21. setLocationRelativeTo(null);
> 22. MyPanel pane = new MyPanel();
> 23. add(pane);
> 24. }
> 25. }
> 26. //======================
> 27. package mypaneltest;
> 28.
> 29. public class Main {
> 30. public static void main(String[] args) {
> 31. new MyFrame().setVisible(true);
> 32. }
> 33. }

It's good that you provided the full code that illustrates your problem.
It's bad that you added line numbers. I couldn't immediately see what
was causing your problems and the line numbers prevent me using
cut&paste to put your code into a Java compiler.


Here's how I'd have written it

---------------------8<--------------------------
package org.redgrittybrick.test.swing;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class MyPanelTest {
/**
* @author: RedGrittyBrick
*/

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {
public void run() {
new MyPanelTest().createAndShowGUI();
}
});
}

private void createAndShowGUI() {
JPanel p = new JPanel();
p.add(new JButton("OK"));

JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(p);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
---------------------8<--------------------------

BEHROUZ

unread,
Feb 15, 2010, 2:08:25 PM2/15/10
to
On Feb 15, 2:18 am, RedGrittyBrick <RedGrittyBr...@spamweary.invalid>
wrote:
Thanks,
I got it now, but what is EDT?
**Note that your should also construct your GUI on the EDT - you will
have problems later if you don't.**
could you please tell me more?

Lew

unread,
Feb 15, 2010, 2:50:10 PM2/15/10
to
BEHROUZ wrote:
> I got it now, but what is EDT?
> **Note that your should also construct your GUI on the EDT - you will
> have problems later if you don't.**
> could you please tell me more?

This is covered in the Swing tutorial, which you should have read and should
read now. In particular, the EDT is covered in:
<http://java.sun.com/docs/books/tutorial/uiswing/concurrency/index.html>

See also:
<http://www.google.com/search?q=Java+EDT>
or
<http://www.google.com/search?q=Java+"Event+Dispatch+Thread">

GIYF.

--
Lew

Roedy Green

unread,
Feb 16, 2010, 5:54:59 PM2/16/10
to
On Mon, 15 Feb 2010 10:18:49 +0000, RedGrittyBrick
<RedGrit...@spamweary.invalid> wrote, quoted or indirectly quoted
someone who said :


>
>It's good that you provided the full code that illustrates your problem.
>It's bad that you added line numbers. I couldn't immediately see what
>was causing your problems and the line numbers prevent me using
>cut&paste to put your code into a Java compiler.

Visual SlickEdit is great for this. You just drag a rectangle with
the right mouse button and hit delete. Intellij has column mode -- not
quite so convenient, but great for doing things like stripping off
line numbers.

I do all kinds of data manipulation tasks using just the column, sort
and regex search replace features of Slick Edit. It saves writing
reams of one-shot code.
--
Roedy Green Canadian Mind Products
http://mindprod.com

Nothing has really happened until it has been recorded.
~ Virginia Woolf (born: 1882-01-25 died: 1941-03-28 at age: 59)

Roedy Green

unread,
Feb 16, 2010, 5:55:34 PM2/16/10
to
On Mon, 15 Feb 2010 00:52:19 -0800 (PST), BEHROUZ <behs...@gmail.com>

wrote, quoted or indirectly quoted someone who said :

>1-Class MyPanel
>2-Class MyFrame
>3-Class Main

see http://mindprod.com/jgloss/jframe.html
http://mindprod.com/jgloss/jpanel.html

Arne Vajhøj

unread,
Feb 16, 2010, 8:01:48 PM2/16/10
to
On 16-02-2010 17:54, Roedy Green wrote:
> On Mon, 15 Feb 2010 10:18:49 +0000, RedGrittyBrick
> <RedGrit...@spamweary.invalid> wrote, quoted or indirectly quoted
> someone who said :
>> It's good that you provided the full code that illustrates your problem.
>> It's bad that you added line numbers. I couldn't immediately see what
>> was causing your problems and the line numbers prevent me using
>> cut&paste to put your code into a Java compiler.
>
> Visual SlickEdit is great for this. You just drag a rectangle with
> the right mouse button and hit delete. Intellij has column mode -- not
> quite so convenient, but great for doing things like stripping off
> line numbers.
>
> I do all kinds of data manipulation tasks using just the column, sort
> and regex search replace features of Slick Edit. It saves writing
> reams of one-shot code.

UltraEdit can edit rectangular.

Arne

Lew

unread,
Feb 16, 2010, 8:55:54 PM2/16/10
to
Roedy Green wrote:
>> Visual SlickEdit is great for this. You just drag a rectangle with
>> the right mouse button and hit delete. Intellij has column mode -- not
>> quite so convenient, but great for doing things like stripping off
>> line numbers.
>>
>> I do all kinds of data manipulation tasks using just the column, sort
>> and regex search replace features of Slick Edit. It saves writing
>> reams of one-shot code.

Arne Vajhøj wrote:
> UltraEdit can edit rectangular.

So can emacs. So can vi. So can Textpad. So can eclipse with a plugin.
There used to be a plugin for NetBeans but I'm not finding it right now.

--
Lew

0 new messages