• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronise JScrollbar and keybord tabbing

 
Josee Paradis
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I would like when I pressed the key "ctrl tab" that my scrollbar be synchronise with the place where the focus is. I try the "scrollRectToVisible" but the problem is when my component is in another component I don't have access to the bounds of my component. The return value for X and Y is 0.

This is my code.
Does anybody have an idea to fix this problem?
Thanks

Josee

import java.awt.Component;
import java.awt.KeyboardFocusManager;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;


/**
* Insert type description
*/
public class TestFocus extends JFrame implements PropertyChangeListener
{
public TestFocus ()
{
super();
}

JPanel panel;


protected void frameInit ()
{
super.frameInit();
panel = new JPanel();
setContentPane( new JScrollPane( panel ) );
panel.setLayout( new BoxLayout( panel, BoxLayout.Y_AXIS ) );

for ( int i = 0; i < 20; i++ )
{
panel.add( new JComboBox( new String[] { "#" + i + " Combox1", "#" + i + " Combox2" } ) );
panel.add( new JTextField( "Textfield " + i ) );
panel.add( new JScrollPane( new JTextArea( "TextArea " + i + "\nTextArea " + i + "\nTextArea " + i ) ) );
}

KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener( "focusOwner", this );
}


/* (non-Javadoc)
* @see
java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
*/
public void propertyChange ( PropertyChangeEvent evt )
{
Component compo = KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusOwner();

if ( compo != null && compo.getParent() != null && compo.getParent() instanceof JComponent )
{
System.out.println("Component Class: " + compo.getClass() + " Bound: " + compo.getBounds());
( (JComponent) compo.getParent() ).scrollRectToVisible( compo.getBounds() );
}
}


public static void main ( String[] args )
{
TestFocus t = new TestFocus();
t.pack();
t.setVisible( true );
}

}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!