ComponentListener does not work in my case. Consider a situation where the panel size is 1200x1200 and visible rectangle is 200x300 initially when split bar is moved Visble rectangle of the panel increase or decreases. In this case the panel size remains the same. sample code is given below
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
public class VisibleRect extends JFrame{
private JPanel centerPanel = null;
private JSplitPane splitPane1 = null;
public VisibleRect() {
intiComp();
}
private void intiComp() {
centerPanel = new JPanel();
centerPanel.setBackground(Color.white);
centerPanel.setPreferredSize(new Dimension(1200, 1200));
centerPanel.addComponentListener(new ComponentListener() {
public void componentHidden(ComponentEvent e) {
System.out.println("componentHidden");
}
public void componentShown(ComponentEvent e) {
System.out.println("componentShown");
}
public void componentMoved(ComponentEvent e) {
System.out.println("componentMoved");
}
public void componentResized(ComponentEvent e) {
System.out.println("componentResized");
}
});
splitPane1 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(centerPanel), new JLabel());
add(splitPane1, BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setSize(200, 300);
setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
new VisibleRect();
}
}
Based on you original question how where supposed to know that you where talking about a JSplitPane?
Why is it so difficult to ask a meaningful question with all the details the first time so we don't waste our time guessing what you are talking about?
You could try adding a PropertyChangeListener to the split pane and listen for changes in the divider location.