JScrollbar and keyevents?

Hello,

i've got a JPanel with a scroll pane wrapped around it. All works well, but there seems to be a problem with the keyevents.

All the keys work great, like DELETE etc. But i am having some issues with the up, down, left and right key. If my horizontal scrollbar isn't entirely scrolled to the right, and i press the right key on my keyboard, the scrollbar moves right, and nothing happens anymore in my JPanel. Same with up, down and left.

I hope you kind of understand what my problem is here :)

Here's the code of my gui:

// ik zet mijn gui in mijn main class:

Gui g =new Gui("LanZa");

g.pack ();

g.setVisible (true);

g.setResizable (true);

// de klasse Gui:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

publicclass Guiextends JFrame

{

private TekenBord tekenBord;

//

// lays out main GUI

//

public Gui(String title)

{

super(title);// set title

// create JPanel for the entire interface

JPanel appPanel =new JPanel();

appPanel.setLayout (new BorderLayout());

// create buttonbar

ButtonBar toolbar =new ButtonBar();

// create upper panel with toolbar

JPanel upperPanel =new JPanel();

upperPanel.setLayout (new BorderLayout());

upperPanel.add (toolbar, BorderLayout.NORTH);

// create drawing panel

tekenBord =new TekenBord(toolbar);

// create center panel with the painting area

JPanel centerPanel =new JPanel();

centerPanel.setLayout (new BorderLayout());

centerPanel.add (tekenBord, BorderLayout.CENTER);

// add scrollbars around centerPanel

JScrollPane sp =new JScrollPane(centerPanel);

sp.setPreferredSize(new Dimension(800, 600));

sp.getVerticalScrollBar().setFocusable(false);

sp.getHorizontalScrollBar().setFocusable(false);

// create menu bar

// !! geef tekenBord object mee door!!

JMenuBar menuBar = createMenuBar(tekenBord);

setJMenuBar(menuBar);

getContentPane().add(appPanel);

appPanel.add(upperPanel, BorderLayout.NORTH);

appPanel.add(sp, BorderLayout.CENTER);

addWindowListener (new WindowCloser());

}

//--

// Sets up the menu bar

//--

public JMenuBar createMenuBar (TekenBord tb)

{

MenuListener menuListener =new MenuListener (tb);

JMenu fileMenu =new JMenu ("File");// create new menu

// create new items

JMenuItem newMenuItem =new JMenuItem ("Nieuw");

newMenuItem.addActionListener (menuListener);

JMenuItem openMenuItem =new JMenuItem ("Open");

openMenuItem.addActionListener (menuListener);

JMenuItem saveMenuItem =new JMenuItem ("Opslaan Als ..");

saveMenuItem.addActionListener (menuListener);

JMenuItem exportMenuItem =new JMenuItem ("Export");

exportMenuItem.addActionListener (menuListener);

JMenuItem exitMenuItem =new JMenuItem ("Exit");

exitMenuItem.addActionListener (menuListener);

// insert items into menu

fileMenu.add (newMenuItem);

fileMenu.add (openMenuItem);

fileMenu.add (saveMenuItem);

fileMenu.add (exportMenuItem);

fileMenu.addSeparator ();

fileMenu.add (exitMenuItem);

// Stats

JMenu statMenu =new JMenu ("Stats");

JMenuItem statMenuItem =new JMenuItem ("Aantal Tafels");

statMenuItem.addActionListener (menuListener);

statMenu.add (statMenuItem);

// insert menu on bar

JMenuBar bar =new JMenuBar ();

bar.add (fileMenu);

bar.add (statMenu);

return bar;

}

//*****************************************

// An inner class to handle window events.

//*****************************************

publicclass WindowCloserextends WindowAdapter

{

//-

// Terminates the program when the window is closed.

//-

publicvoid windowClosing (WindowEvent event)

{

System.exit (0);

}

}

}

// tekenBord ziet er als volgt uit:

import java.awt.*;

import java.awt.event.*;

import java.util.*;

import javax.swing.*;

publicclass TekenBordextends JPanelimplements MouseListener,

MouseMotionListener,

KeyListener

{

//

// Sets up the drawing area.

//

public TekenBord (ButtonBar toolbar)

{

// constructor blabla

// eigenschappen van deze JPanel:

setMinimumSize (new Dimension (DIMENSIE_X, DIMENSIE_Y));

setPreferredSize (new Dimension (DIMENSIE_X, DIMENSIE_Y));

setFocusable(true);

addKeyListener (this);

addMouseListener (this);

addMouseMotionListener (this);

}

//

// Mouse released

//

publicvoid mouseReleased (MouseEvent event)

{

// ...

repaint();

requestFocus();

}

//

// Move item with mousepointer

//

publicvoid mouseDragged (MouseEvent event)

{

// ...

repaint();

requestFocus();

}

//

// Set up new object to draw

//

publicvoid mousePressed (MouseEvent event)

{

// ...

repaint();

requestFocus();

}

//

// Handles key events

//

publicvoid keyPressed (KeyEvent event)

{

if ( buttonBar.getButton() == buttonBar.SELECT && (currentTable !=null && currentTable.getSelected()) )

{

switch(event.getKeyCode())

{

// delete

case KeyEvent.VK_DELETE:

verwijderTafel();

break;

// nudge up

case KeyEvent.VK_UP:

currentTable.up();

break;

// nudge down

case KeyEvent.VK_DOWN:

currentTable.down();

break;

// nudge left

case KeyEvent.VK_LEFT:

currentTable.left();

break;

// nudge right

case KeyEvent.VK_RIGHT:

currentTable.right();

break;

}

}

}

//--

// Provide empty implementations for unused events.

//--

publicvoid mouseMoved (MouseEvent event){}

publicvoid mouseEntered (MouseEvent event){}

publicvoid mouseExited (MouseEvent event){}

publicvoid mouseClicked (MouseEvent event){}

publicvoid keyTyped (KeyEvent event){}

publicvoid keyReleased (KeyEvent event){}

}

A small example of what i exactly mean can be seen here: http://www.student.kuleuven.ac.be/~s0106609/record.avi Whenever there is a green circle around the mouse pointer, i am clicking the mouse. When there is not a circle, i am using the keyboard. Hopefully this could clear things up :)

Thanks a lot for any help!

[12760 byte] By [dekoffie] at [2007-9-30 22:49:48]
# 1
Hi,Try to call revalidate() in the panel that is inside the scrollpane.Regards,Andr?
acneto at 2007-7-7 13:24:01 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 2
Hi Andr?thanks for the tip but it does not seem to fix it.. any more suggestions? :)Thanks!
dekoffie at 2007-7-7 13:24:01 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 3
Hi again,I can test your code to find out the problem, but I need all the classes that you use. Can you zip it and place it in some webpage?Andre'
acneto at 2007-7-7 13:24:01 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 4
That would be great! I tried a lot of things by now, still couldnt figure it out. I sure hope you will :)The .java files can be found on http://www.student.kuleuven.ac.be/~s0106609/LanZa.rarThanks a bunch for your time!
dekoffie at 2007-7-7 13:24:01 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 5

Hi,

I found the problem... you have to consume the event so it doens't arrive to the scrollpane sliders. I've also fixed you that problem of updating the scrollbars when you move arround with the square:

//

// Move item with mousepointer

//

public void mouseDragged(MouseEvent event)

{

int currentTool = buttonBar.getButton();

if( !checkIntersection(event) )

{

// if there isnt any intersection, update the point

switch(currentTool)

{

case ButtonBar.SELECT:

// only move the currentTable if we've selected it!

if( selectionMade )

currentTable.updatePoint(event.getPoint());

break;

case ButtonBar.RECT:

currentTable.updatePoint(event.getPoint());

break;

}

}

else

{

// if the selected rectangle is intersecting with another rectangle,

// we will clip it together with this other rectangle.

clipRectangles(event);

}

Point corner = currentTable.getCorner();

scrollRectToVisible(new java.awt.Rectangle((int)corner.getX(), (int)corner.getY(), (int)currentTable.getDimension().getWidth(), (int)currentTable.getDimension().getHeight()));

repaint();

requestFocus();

}

//

// Handles key events

//

public void keyPressed(KeyEvent event)

{

boolean left = false;

boolean up = false;

if ( buttonBar.getButton() == buttonBar.SELECT && (currentTable != null && currentTable.getSelected()) )

{

switch(event.getKeyCode())

{

// delete

case KeyEvent.VK_DELETE:

verwijderTafel();

break;

// nudge up

case KeyEvent.VK_UP:

currentTable.up();

up = true;

break;

// nudge down

case KeyEvent.VK_DOWN:

currentTable.down();

break;

// nudge left

case KeyEvent.VK_LEFT:

left = true;

currentTable.left();

break;

// nudge right

case KeyEvent.VK_RIGHT:

currentTable.right();

break;

}

Point corner = currentTable.getCorner();

scrollRectToVisible(new java.awt.Rectangle((int)corner.getX(), (int)corner.getY(), (int)currentTable.getDimension().getWidth(), (int)currentTable.getDimension().getHeight()));

if(currentTable.getSelected())

{

event.consume();

}

}

You may also want to change the way you load images, so they can be loaded anywhere (like inside a jar):

...

selectButton = new JRadioButton (

new ImageIcon (getClass().getResource("/img/select.gif")));

selectButton.setSelectedIcon (

new ImageIcon (getClass().getResource("/img/selectSel.gif")));

selectButton.setToolTipText ("Select tool.");

selectButton.addActionListener (listener);

selectButton.setSelected(true);

rectButton = new JRadioButton (

new ImageIcon (getClass().getResource("/img/rect.gif")));

rectButton.setSelectedIcon (

new ImageIcon (getClass().getResource("/img/rectSel.gif")));

...

Let me know if it all worked well.

Regards,

Andre'

acneto at 2007-7-7 13:24:01 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 6

Hello Andr?

all your suggestions were great! I fixed the problem, and i now i have my scrollbars updating when i move the rectangles around to! Great! ;)

Also, i had no idea about this way of loading the images. I found it pretty stupid that i had to put my image directory outside the jar file, but now i know!

Thanks a lot for your time and helpful suggestions!

dekoffie at 2007-7-7 13:24:01 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...