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!

