Repaint not happening whe
To: comp.lang.java.gui
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
---
* Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24