tags:

views:

33

answers:

1

Ok say I have a JPanel "controls" and jpanel "graphPanel" within another jpanel

public class outer extends JPanel implements ActionListener{
   private JPanel controls,graphPanel;
   private JButton doAction

   public outer(){
      JPanel controls = new JPanel();
      JButton doAction = new JButton("Do stuff");
      doAction.addActionListener(this);
      controls.add(doAction);

      JPanel graphPanel = new JPanel();
      this.add(controls);
      this.add(graphPanel);
   }



public void actionPerformed(ActionEvent e) {
    if(e.getSource()==doAction){
        //How do I fire paintComponent of controls JPanel on this click
}

How do i make "graphPanel" repaint after my button is clicked

+1  A: 

The doAction and graphPanel are declared twice - once at the class level, then again in the method:

   private JPanel controls,graphPanel; // declared here first
   private JButton doAction; // declared here first

   public outer(){
      JPanel controls = new JPanel(); // Whoops, declared here again
      JButton doAction = new JButton("Do stuff"); // Whoops, declared here again
      doAction.addActionListener(this);
      controls.add(doAction);

      JPanel graphPanel = new JPanel(); // Whoops, declared here again
      ...

In the method remove the declaration, and make them simple assignments, like this:

controls = new JPanel(); // no leading 'JPanel'

Do that and the additional repaint code won't throw a NPE

Keilly