Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / September 2007

Tip: Looking for answers? Try searching our database.

Repaint not happening when compnent added to panel progrmatically

Thread view: 
Chanchal - 28 Sep 2007 08:11 GMT
Hi All,

I have five classes MyFrame, MyContainerPanel,MyPanel, MyButton and
MyLine.
MyFrame contains MyContainerPanel, MyContainerPanel contains MyPanel
and MyPanel
contains MyButton.

Constructor in MyButton takes an object of MyContainerPanel. So when
MyPanel is created an instance of MyContainerPanel is passed, which is
in turn passed to the contained MyButton instance.

MyButton has an inner class ML which extends MouseInputAdapter and the
mouseClicked() method is defined.

Now when MyButton is clicked, a function addLine() in MyContainerPanel
should be called, which will add a MyLine instance to the contaied
MyPanel instance. My problem is that when this add() is called, the
overrideen paintComponent() method in MyLine is not getting called.

Please find the code..

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

public class MyButton extends JPanel {
   MyContainerPanel mp;
   public MyButton(MyContainerPanel mp){
       this.mp = mp;
       addMouseListener(new ML());
       setBorder(javax.swing.BorderFactory.createLineBorder(new
java.awt.Color(0, 0, 0)));
   }

   class ML extends MouseInputAdapter{
       public void mouseClicked(MouseEvent e){
           System.out.println("MB CLICKED");
           mp.addLine();
       }
   }
}

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class MyPanel extends JPanel{
   MyContainerPanel mp;
   public MyPanel(MyContainerPanel mp){
       System.out.println("MyPanel constructor");
       MyButton mb = new MyButton(mp);
       setLayout(null);
       mb.setBounds(10,10,100,50);
       add(mb);
   }
}

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class MyContainerPanel extends JPanel{
   MyPanel mp;
   public MyContainerPanel(){
       mp = new MyPanel(this);
       setLayout(new BorderLayout());
       add(mp);
   }

    public void addLine(){
       MyLine ml = new MyLine();
       mp.add(ml);
       mp.validate();
   }
}

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class MyFrame extends JFrame{

   public MyFrame(){

   }

   public static void main(String[] args){
       MyFrame mf = new MyFrame();
       mf.add(new MyContainerPanel());
       mf.setSize(400,300);
       mf.setVisible(true);
   }
}

import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Color;
public class MyLine extends JPanel{
   protected void paintComponent(Graphics g){
       System.out.println("Painting MyLine");
   }
}

Kindly advice what needs to be done so that the paintComponent() in
MyLine instance is called, when it is added to the MyPanel instance.

Thanks in advance

Chanchal
Andrew Thompson - 28 Sep 2007 08:59 GMT
...
>I have five classes ...

Which can all be enclosed in a single SSCCE.

Please *read* the document.

After wrestling this into a form that is compilable in a
single file, then trying to sort the mess, I noticed this..

...
 setLayout(null);
...

Which makes me wonder.
a) Why you are ignoring my advice?
..and more importantly ..
b) Why I should spend (or waste) any more time trying
to help you?

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Andrew Thompson - 29 Sep 2007 09:26 GMT
Gmail huh?  since I posted a reply a while
ago, and it does not appear at GG yet, I will
(hopefully) progress this matter by linking
to another web representation of my reply.
http://www.javakb.com/Uwe/Forum.aspx/java-gui/7356/Repaint-not-happening-when-co
mpnent-added-to-panel-progrmatically#78e1fc38b3664uwe


Andrew T.
Chanchal - 29 Sep 2007 15:35 GMT
Dear Mr. Thompson,

It is not that i'm ignoring your advice. I pretty much appreciate
that. i know that the porblem here is again setLayout(null). But i'm a
bit helpless here. It could be a design flaw, but i'm left here with
five classes which are represented by the five classes in my original
post. I'm having to develop this UI, but situation here is that i
cannot go and change the design. Also the added components needs to
come in the specified places, not in a region as defined by the
BorderLayout or at an available place as decided by the FlowLoayout.
Using GridLayout or GridBagLayout turns out to be too complex. i'm
forced to use null layout. i do not have a choice as far as the design
of this UI is considered. So all i can do is to find out how i can fit
things into this may-be-bad design. Again if you are able to give some
suggestion on how this can be implemented in this given
scenario,rather than thinking that you are wasting your time, i'd
appreciate it.

Thanks again

Chanchal
Andrew Thompson - 29 Sep 2007 20:02 GMT
...
>It is not that i'm ignoring your advice.

Have you done the layout tutorial yet?

>..I pretty much appreciate
>that. i know that the porblem here is again setLayout(null). But i'm a
>bit helpless here. It could be a design flaw, but i'm left here with
>five classes which are represented by the five classes in my original
>post. I'm having to develop this UI, but situation here is that i
>cannot go and change the design.

Yes you can.  If you had done the layout tutorial,
that much should be obvious.

>..Also the added components needs to
>come in the specified places, ..

Which is what layouts do.  The only reason my versions
of the layouts differed from yours, is that I did not
take much care to place them 'pixel by pixel' exactly
as you had them, that was left as an exercise for you.
'Batteries not included'.

And while we are on 'specified places', I will point out
that null layout/exact positioning GUI's might well fail
under a different PLAF or Java version, on screens of
different resolution or size, on different platforms, with
a different default font size, or.. any number of other
reasons.

Exact pixel positioning just does not make any sense
in a language defined to be X-plat - unless you cover
all of the things mentioned above.  Once your GUI has
tacked every aspect of that down, you might as well
have put that logic into a layout manager.

Using the inbuilt layouts might give you a good start
at understanding how to make a custom layout
manager.

>..not in a region as defined by the
>BorderLayout or at an available place as decided by the FlowLoayout.

Adjust the borders or insets.  Your comment about
how the FlowLayout of an earlier example did not meet
your exact requirements is a good indication that
you had not checked the JavaDocs.  They are invaluable
for this stuff, and programmers need to read a lot of
documentation if they expect to be successful.

Asking people on usenet newsgroups to fix the basic
problems with an already very broken null layout, is
no substitute.

>Using GridLayout or GridBagLayout turns out to be too complex. i'm
>forced to use null layout.

No, you just need to put the effort in to learning how to
use them.  If you really cannot figure how best to layout
any particular GUI - try describing it in either a text diagram
(good for usenet, but not the sort of precise definitions
that you seem to want), or an actual (crude or otherwise)
drawing - which cannot be posted to this usenet group,
but can be shown to others via URL.

>..i do not have a choice as far as the design
>of this UI is considered. So all i can do is to find out how i can fit
>things into this may-be-bad design. Again if you are able to give some
>suggestion on how this can be implemented in this given
>scenario,rather than thinking that you are wasting your time, i'd
>appreciate it.

Layouts.

By the way.  You seem to be putting more time and effort
into your excuses, than you are into doing the layout tutorial,
or otherwise learning how to use them.  That is not time
well spent.

Signature

Andrew Thompson
http://www.athompson.info/andrew/

Knute Johnson - 29 Sep 2007 17:13 GMT
> Hi All,
>
[quoted text clipped - 105 lines]
>
> Chanchal

MyLine has no size or location and as a result will never show up.  Are
you just learning Java and/or is this a school project?

Signature

Knute Johnson
email s/nospam/knute/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.