2

I have 2 JPanels, each has its own class extending Jpanel each are in a jDialog. I put a click event in one of the classes which then gets the color at the point where the click occurred. Now how can I tell the second JPanel to repaint itself?

The constructor in my jDialog:

public ColorPickerDialog(java.awt.Frame parent, boolean modal){
    super(parent, modal);
    initComponents();

    cpp = new ColorPicker();
    cpp.setBounds(5, 5, 300, 300);
    cpp.setVisible(true);
    cpp.setOpaque(true);
    cpp.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    add(cpp);

    // Click event is in this class:
    ColorSlider colorSlide = new ColorSlider();
    colorSlide.setBounds(315, 5, 20, 300);
    colorSlide.setVisible(true);
    colorSlide.setOpaque(true);
    colorSlide.setBorder(BorderFactory.createBevelBorder(BevelBorder.LOWERED));
    add(colorSlide);
}

My Click event:

public void mouseClick(MouseEvent evt){
    Point pt = evt.getPoint();

    BufferedImage img = (BufferedImage)this.createImage(getWidth(), getHeight());
    int[] colors = new int[3];
    img.getRaster().getPixel(evt.getX(), evt.getY(), colors);
    ColorPickerDialog.sldColor = new Color(colors[0], colors[1], colors[2]);
}
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338
  • 2
    You could find the common ancestor and instruct it to repaint. `getParent().invalidate()`, `getParent().repaint()` – MadProgrammer Dec 07 '12 at 23:36
  • @MadProgrammer +1 should be an answer I was going to suggest passing `JPanel` instance to `JDialog` :O. Though I am not to sure why you include `invalidate()` call, see my answer [here](http://stackoverflow.com/questions/13549976/how-to-change-the-dimension-of-a-component-in-a-jframe/13551229#13551229) – David Kroukamp Dec 08 '12 at 08:32
  • 1
    @DavidKroukamp Old school ;) `revalidate` is a better option. The point is to try and force the repaint by marking the parent container (and it's children) as "invalid" – MadProgrammer Dec 08 '12 at 08:33
  • 1
    @MadProgrammer +1 I see, was just wondering, so by marking component as `invalidate()` in hopes `repaint()` will get called sooner? – David Kroukamp Dec 08 '12 at 08:34
  • 1
    @DavidKroukamp That's the idea. We want to "invalidate" the parent so it will try and repaint it's children (in this case the two components). Otherwise the repaint manager may decide that nothing needs to be updated and ignore the repaint request – MadProgrammer Dec 08 '12 at 08:36
  • 1
    @DavidKroukamp In most cases I think `repaint` would probably work, given the number of requests per second, its less likely that the repaint manager is going to ignore them. Re-validating the component hierarchy could be a time consuming operation and may actually slow down the repaint cycle. In an environment where the updates come less often, the repaint manager may need a nudge ;). PS - Nice questions ;) – MadProgrammer Dec 08 '12 at 08:42

2 Answers2

2

Use Factory pattern for your project. When you create or initiate 2nd JPanel, add it to Factory that contains instances of objects. Into mouseClick, call your 2nd panel from factory and implement: panel.repaint();

Some Example:

 public void mouseClick(MouseEvent evt){
....
 SecondPanel panel = PanelFactory.loadPanel(2);

    panel.revalidate();
    panel.repaint();    
    ....

}




public class PanelFactory{

  private static PanelFactory instance = new PanelFactory();  

  private JPanel mSomePanel = null;

  public static void storePanel(JPanel panel){
     instance.mSomePanel = panel;
  }

   public static JPanel loadPanel(int index){
     ......
     return instance.mSomePanel;

    }
}
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
  • 1
    Simply call `revalidate()` in place of `invalidate()` and `validate()`. see my answer [here](http://stackoverflow.com/questions/13549976/how-to-change-the-dimension-of-a-component-in-a-jframe/13551229#13551229) – David Kroukamp Dec 08 '12 at 08:32
2

One of the JPanel's could be an Observer and the other could be an Observable. Then, when you require an update, you just call:

setChanged();
notifyObservers();

in your Observable JPanel.

sdasdadas
  • 23,917
  • 20
  • 63
  • 148
  • 1
    +1 for clarity, although these two classes are a little quaint and not as safe or flexible as the alternatives mentioned [here](http://stackoverflow.com/a/3072979/230513). – trashgod Dec 08 '12 at 02:37
  • 1
    Seems alot of work for a simple repaint but +1 nice way to go – David Kroukamp Dec 08 '12 at 08:26