Dont reaxh MouseListener
In my JPanel-ClassI use
privatevoid makeWuerfel(){
for (int i = 0; i < Jobs.MAX_WUERFEL ; i++ ){
aWuerfel[i] =new Wuerfel();
aWuerfel[i].setBounds(
40 * Jobs.BLOCK_LINKS + i * 60,
20 * Jobs.BLOCK_HOEHE,
getWidth(),
getHeight()
);
aWuerfel[i].setName("Wuerfel_" + Integer.toString(i));
aWuerfel[i].setColorNotSelected(colorWuerfelNotSelected);
aWuerfel[i].setColorSelected(colorWuerfelSelected);
aWuerfel[i].setSelected(true);
aWuerfel[i].setVisible(true);
aWuerfel[i].tag = i;
aWuerfel[i].addMouseListener(spieleBlattEventHandler);
add(aWuerfel[i]);
}
}
class SpieleBlattEventHandlerimplements
java.awt.event.ActionListener,
java.awt.event.KeyListener,
java.awt.event.MouseListener{
//.......some code
publicvoid mouseClicked(MouseEvent e){
if (e.getComponent().getName().substring(0,8).equals("Wuerfel_"))
wuerfelClicked(e);
System.out.println(e);
}
I dont reach my Eventhandler. with another Type the same procedure works.
and I cant find out Why
TIA Hanns
# 1
I don't understand your question?What do you mean by reach?What goes wrong? Maybe you should post an example of each case?
# 2
What type of Component is your Wuerfel? I'd guess some sort of button. If that is the case, I think you want to use an ActionListener and "actionPerformed" in order to process a mouse click on it. Or, if a toggle button or checkbox, I'd say use ItemListener and itemStateChanged.
And, next time, Swing questions should be posted in the Swing forum.
# 3
Sorry for the Wrong forum.Wuerfel is a JTextField
# 4
Clicking on a Wuerfel should throw an MouseClicked-Event which I try to catch in my Eventhandler. but stepping through the code never reaches the Enventhandler
# 5
your setBounds() looks strange - every aWuerfel[x]'s width/height is supposed to bethe same width/height of the JPanel ?aWuerfel[x].setBounds(..., ..., getWidth(), getHeight());and getWidth() and getHeight() generally return 0 during 'construction'
# 6
> your setBounds() looks strange - every aWuerfel[x]'s
> width/height is supposed to be
> the same width/height of the JPanel ?
NO
> aWuerfel[x].setBounds(..., ..., getWidth(),
> getHeight());
Changed it to ... aWuerfel[x].getWidth(),...
where width and height are set form the Wuerfel.constructor
# 7
Are all of your textfields showing up in the container?
Is the mouseClicked event being entered at all?
Are you sure you're pressing and releasing the mouse button at the same location? Try implementing mousePressed or mouseReleased and see if they fire.
If that doesn't work, I'd try implementing actionlistener on the fields to see if that works. If it does, you know it's a problem with the mouse listener specifically. If not, it's probably a problem with the fields themselves, or their layout.
Have you tried using a layoutmanager like flowlayout?
# 8
> Are all of your textfields showing up in the
> container?
Yes
>
> Is the mouseClicked event being entered at all?
I put a System.out.println( mouseEvent.getComponent().getName());
to MouseClicked. other textfields appear here
>
> Are you sure you're pressing and releasing the mouse
> button at the same location?
I believe Yes.
But even MouseEntered or MouseExited wont fire
Try implementing
> mousePressed or mouseReleased and see if they fire.
Neither
>
> If that doesn't work, I'd try implementing
> actionlistener on the fields to see if that works. If
> it does, you know it's a problem with the mouse
> listener specifically. If not, it's probably a
> problem with the fields themselves, or their layout.
>
I will try that
> Have you tried using a layoutmanager like flowlayout?
No I set layout to null. because I want to position my elements myself
# 9
Well I'm sort of grasping at straws here, but unless someone else has a better suggestion, try setting the layout of the container to flowlayout, it's possible the positioning is screwing it up.
# 10
Can you show the initialization of "spieleBlattEventHandler" that is used in the following line?aWuerfel[i].addMouseListener(spieleBlattEventHandler);
# 11
class SpieleBlattEventHandler implements
java.awt.event.ActionListener,
java.awt.event.KeyListener,
java.awt.event.MouseListener{
public void actionPerformed(java.awt.event.ActionEvent e) {
// ..
}
public void keyPressed(java.awt.event.KeyEvent e) {
System.out.println("keyPressed "+ e.getSource().toString());
};
public void keyReleased(java.awt.event.KeyEvent e) {};
public void keyTyped(java.awt.event.KeyEvent e) { };
public void mouseClicked(MouseEvent e) {
/* the following is working and is as well an array of JTextfields
-
*/
if ( e.getComponent().getName().startsWith("tfWert_",0)){
String name = e.getComponent().getName();
for ( int index = 0 ; index < werteArray.length ; index++){
if ( werteArray[index].getName().equals(name)) {
eintragen(index);
break;
}
}
}
/* here is where I never get
* --
*/
if (e.getComponent().getName().substring(0,8).equalsIgnoreCase("Wuerfel_"))
wuerfelClicked(e);
System.out.println(e);
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
/* JTextFields [] is working too */
public void mouseEntered(MouseEvent e) {
if ( e.getComponent().getName().startsWith("tfWert_",0)){
String name = e.getComponent().getName();
for ( int index = 0 ; index < werteArray.length ; index++){
if ( werteArray[index].getName().equals(name)) {
werteFeldEntered(index);
break;
}
}
}
System.out.println("entered " +e.getComponent().getName());
}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){ };
}
SpieleBlattEventhandler is an inner-class to SpieleBlatt
# 12
> SpieleBlattEventhandler is an inner-class to SpieleBlatt
I figured that much. But, where is the variable declared and initialized? That is, where do you say the following?
SpieleBlattEventHandler spieleBlattEventHandler = new SpieleBlattEventhandler();
Your code might look nicer if you had three separate listener classes--one for Action, one for Key, and one for Mouse. They should all be inner classes to SpieleBlatt.
# 13
works OK in this
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Testing
{
public void buildGUI()
{
JTextField Wuerfel_tf = new JTextField(10);
Wuerfel_tf.setName("Wuerfel_tf");
JFrame f = new JFrame();
f.getContentPane().add(Wuerfel_tf);
f.pack();
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
Wuerfel_tf.addMouseListener(new SpieleBlattEventHandler());
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable(){
public void run(){
new Testing().buildGUI();
}
});
}
}
class SpieleBlattEventHandler implements java.awt.event.MouseListener
{
public void mouseClicked(MouseEvent e)
{
/* here is where I never get
* --
*/
if (e.getComponent().getName().substring(0,8).equalsIgnoreCase("Wuerfel_"))
{
System.out.println("I'm here");
}
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {
System.out.println("entered " +e.getComponent().getName());
}
public void mouseExited(MouseEvent e){}
public void mouseMoved(MouseEvent e){ };
}
so, if this part isn't working
if (e.getComponent().getName().substring(0,8).equalsIgnoreCase("Wuerfel_"))
wuerfelClicked(e);
what's in
wuerfelClicked(e)
# 14
>In wuerfelClicked(e) I change Colors and Text.
Meanwhile it runs. I changed the addMouseListener line and added a Casr:// from
aWuerfel[i].addMouseListener(spieleBlattEventHandler);
// to
((Wuerfel)aWuerfel[i]).addMouseListener(spieleBlattEventHandler);
// in the MouseEvent from
if (e.getComponent().getName().substring(0,8).equalsIgnoreCase("Wuerfel_"))
//to
if (e.getComponent().getName().startsWith("Wuerfel_") ) {
//....
}