Sagewire Logo

Repaint not happening when compnent added to panel progrmatically

6 Message(s) by 3 Author(s) originally posted in java gui


From: Chanchal Date:   Friday, September 28, 2007
Hi All,

I have five class es 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 isn't 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


From: Andrew Thompson Date:   Friday, September 28, 2007
wrote in message:
..
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?

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

Message posted via JAVAKB.com
http://www.JAVAkb.com/Uwe/Forums.aspx/JAVA-gui/200709/1


From: Andrew Thompson Date:   Saturday, September 29, 2007
wrote in message:

Gmail huh? since I posted a reply a while
ago, and it doesn't appear at GG yet, I'll
(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-compnent-added-to-panel-progrmatically#78e1fc38b3664uwe

Andrew T.


From: Chanchal Date:   Saturday, September 29, 2007
Dear Mr. Thompson,

It isn't 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 don't 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


From: Knute Johnson Date:   Saturday, September 29, 2007
wrote in message:
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 isn't 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



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?

--

Knute Johnson
email s/nospam/knute/


From: Andrew Thompson Date:   Saturday, September 29, 2007
wrote in message:
..
It isn't 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 didn't
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'll 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 doesn't 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 didn't meet
your exact requirements is a good indication that
you hadn't 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 don't 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 isn't time
well spent.

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

Message posted via JAVAKB.com
http://www.JAVAkb.com/Uwe/Forums.aspx/JAVA-gui/200709/1



Next Message: how to set left click to display pop-up menu for system tray


Blogs related to Repaint not happening when compnent added to panel progrmatically

From 1 July 2007, the Better Super changes will give the ...
923 213 5567 74 online savings morrison chilton repair manual website vehicle speed/distance sensor the print spooler service is not running paint that looks wood Ojays eddie levert dead died passed hotel ac palacio universal ...

Dryvit
Dryvit Systems Although the sealant is not a specific component of the Dryvit system, and may not be the responsibility of the applicator installing the Dryvit materials, it is without question an important factor in the performance of ...

Makes, markets and sells components and accessories.
blogroland higgerson mouse trap walmart buy stock call. shannons motor auctions. palms chelsea v postmouth arsenal v manchester satliveonpc.com photogroyp studio new york Astm d2974 hp dv2416us notebook review icp behind the paint e ...

Makes, markets and sells components and accessories.
johnson city tn whirlpool 923 0846113 how to using painters tape to make stripes 9248 736 930 0 titsatthebeach.flv true north components lyman me. xenas calandar www.xvideos.com baby week by week tsar nicholas the 2nd was not a tsar ...

Makes, markets and sells components and accessories.
trypan blue the room store rankings for top art colleges www.snworks.com Indiana flooring newington. hardiplank paint 923 296 0116 titan homes sangerfield ny waverly bed linens radiorecorder free scrapbooking kits hershey car show ...

Makes, markets and sells components and accessories.
190 392 81 14 2 nicotiana tabacum poison curarie constructing a hydrocarbon tanks mustang island condos painting with awlgrip french regional dolls chocolate body paint recipe brooklyn artist aj lamps the articles of confederation and ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional