Forceing a Repaint on a JComponent

Grinler
Grinler used Ask the Experts™
on
I have an application with a JSplitPane with JTree Left - and JTabbedPane Right Component.

When you click on a node in the JTree a JPanel appears in the JTabbedPane (There can only be one JPanel on the JTabbedPane so i use the JTabbedPane.removeAll() to clear the JTabbedPane, this works fine)

The JPanel has a JButton on it to remove the JPanel from its parent the JTabbedPane, ( ((JTabbedPane)JPanel.getParent()).removeAll() ) using the same method as clicking on different nodes, but the item does not refresh before either an run time error occurs or you click on a diffrent node. Why ?

( It is either a focus error or ... ? )

Is there any one that can help me ?

Frustrated Grinler :(  


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Ovi

Commented:
post the stacktrace
try to force repainting by using repaint() or validate() for the component u wish to repaint
-gkern
Ovi

Commented:
Don't use ((JTabbedPane)JPanel.getParent()).removeAll() but ( ((JTabbedPane)innerPanel.getParent()).removeAll(), where innerPanel is your instance of JPanel used on the tabPane.
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Could you post the code?
Ovi

Commented:
Another approach is to list all the parents to the top and see at what level is the JTabbedPane.

button.addActionListener(new ActionListener() {
  public void actionPrformed(ActionEvent ae) {
    Object parent = button;
    while((parent = button.getParent()) != null)
      System.out.println("Parent : " + parent.getClass().getName());
  }
});

or directly :
button.addActionListener(new ActionListener() {
  public void actionPrformed(ActionEvent ae) {
    Object parent = button;
    while((parent = button.getParent()) != null)
      if(parent instanceof JTabbedPane) {
         ((JTabbedPane) parent).removeAll();
          break;
      }
  }
});

Author

Commented:
Ovi what i type as a sample is just a sample
The code i am using is as follow.

void deleteButton_actionPerformed(ActionEvent e) {

    JTabbedPane parent = (JTabbedPane)getParent();
    parent.removeAll();

    System.out.println( parent );


    ActionListener listener = ((CommonObject)userObject).getActionListener();
    if ( listener != null ) listener.actionPerformed( new ActionEvent(this,3,"delete") );

  }

What does validate do?
Ovi

Commented:
Force a complete relayout on the component on which is called.

Author

Commented:
I know that the getParent() retrieves the JTabbedPane i did a reference trace it that the same signature so i no that i am working the the same component

Author

Commented:
Sorry for my ignorinse but how?

Author

Commented:
Sorry for my ignorinse but how?
Ovi

Commented:
Be aware that normally events are not sended on a separate thread but on main thread of the application. Since you click on a button localized on the same panel that you want to remove this will be not possible as long the ActionEvent is not consumed yet, and the button "released" from notification process.
Ovi

Commented:
The problem could be solved with something like :

button.addActionListener(new ActionListener() {
 public void actionPrformed(ActionEvent ae) {
   new Thread(new Runnable() {
     public void run() {
       Object parent = button;
       while((parent = button.getParent()) != null)
         if(parent instanceof JTabbedPane) {
           ((JTabbedPane) parent).removeAll();
           break;
         }
     }
   }).start();
 }
});
Commented:
There is a problem in my code if I use Object instead of Component, modify'it like this :
button.addActionListener(new ActionListener() {
public void actionPrformed(ActionEvent ae) {
  new Thread(new Runnable() {
    public void run() {
      Component parent = button;
      while((parent = parent.getParent()) != null)
        if(parent instanceof JTabbedPane) {
          ((JTabbedPane) parent).removeAll();
          break;
        }
    }
  }).start();
}
});

Sorry for that, was wroted in hurry.

Author

Commented:
I'll try it and let you know ;)

Author

Commented:
It works except for the first delete any idea ?

Author

Commented:
Thanks for the help you have rely saved my bacon!!!!!!!!!!!!!!
Ovi

Commented:
Thanks. For the first delete, try a Thread.sleep(200) before you realy clear the content of the JTabbedPane

Author

Commented:
I forgot to look at the error log it was a NullPointerException on the parent.removeAll
once I placed a if statement around it it worked!
Ovi

Commented:
ok

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial