ActionPerformed doesn't work

Hi all,

I have a JInternalFrame with two JPanel (firstPan and secondPan). In firstPan I put a JTree.

When the user click on a node of JTree I views a specific JPanel into secondoPan.

This JPanel is different for every node of JTree

I have a class for every specific JPanel (extends JPanel)

This is the code that do this:

if (secondPan.getComponents().length > 0) {

secondPan.remove(secondPan.getComponent(0));

}

JPanel pan = new PianSpedUdsPan(this, treeNode, treeObj);

secondPan.add(pan, BorderLayout.CENTER);

secondPanPan.paintAll(dettaglioTreePan.getGraphics());

secondPan.validate();

This works fine!

The secondPan views the new JPanel correctly!

In PianSpedUdsPan (the new JPanel) I have a JButton.

I add an ActionListener to this button, but when the user click on it

no events called!

The MouseClicked works fine.

The button have a toolTipTex, also this doesn't work! Nothing tooltip appears!

Someone help me? I don't know where is the problem. I tried a lot of solutions but all don't work

Thanks

Monica

[1160 byte] By [monksa] at [2007-11-27 9:34:08]
# 1

You didn't post any code that relates to your problem of the ActionListener.

Once again, your problem might just be a coding error, which you obviously havent posted. If you can, re-post the part of the code that has the ActionListener problem.

Also check, if in your actionPerformed method you actually do what is required to fire the event.

ICE

icewalker2ga at 2007-7-12 22:57:32 > top of Java-index,Desktop,Core GUI APIs...
# 2

How did you add the actionlistener to your JButton in the first place?

x.addActionListener(this); //?

If so, do you also have this:

public void actionPerformed(ActionEvent e){

//something here.. like drawString etc or show text etc..

}

and implemented the actionlistener in your class?

deAppela at 2007-7-12 22:57:32 > top of Java-index,Desktop,Core GUI APIs...
# 3

another way of adding actionaListener:

button.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

// do something

}

});

Yannixa at 2007-7-12 22:57:32 > top of Java-index,Desktop,Core GUI APIs...
# 4

I made the gui with Gui Designer of Netbeans, so I sure it is correct!

This is the code:

JButton deselezionaBut = new JButton();

deselezionaBut.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/infolog/sce/resources/ivolume_togli.png")));

deselezionaBut.setToolTipText("Escludi dalla spedizione");

deselezionaBut.setMaximumSize(new java.awt.Dimension(40, 40));

deselezionaBut.setMinimumSize(new java.awt.Dimension(40, 40));

deselezionaBut.setPreferredSize(new java.awt.Dimension(40, 40));

deselezionaBut.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

deselezionaButActionPerformed(evt);

}

});

jToolBar1.add(deselezionaBut);

private void deselezionaButActionPerformed(java.awt.event.ActionEvent evt) {

System.out.println("- actionPerformed --");

}

monksa at 2007-7-12 22:57:32 > top of Java-index,Desktop,Core GUI APIs...
# 5
What is this line about:secondPanPan.paintAll(dettaglioTreePan.getGraphics());Could you try again without that line?
Andre_Uhresa at 2007-7-12 22:57:32 > top of Java-index,Desktop,Core GUI APIs...
# 6

I'd forgot to change the name of the panel for the forum.

This is the correct line:

secondPan.paintAll(secondPan.getGraphics());

(dettaglioTreePan is the original name of panel in my code, I change it for a simpler understanding)

Without this line the second pan don't view the panel inside.

This line repaint the secondPan after I inserted a new Panel in it

monksa at 2007-7-12 22:57:32 > top of Java-index,Desktop,Core GUI APIs...
# 7

> This is the correct line:

>

> secondPan.paintAll(secondPan.getGraphics());

Normally there is no need for calling getGraphics.

A call of secondPan.revalidate() should be enough.

At most you could include a secondPan.repaint() also.

Anyway, this seems to work fine:

/*

* TreePanelsDemo.java

*/

package tree;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.tree.*;

public class TreePanelsDemo extends JFrame {

private JTree tree;

private JPanel secondPan;

public TreePanelsDemo() {

super("Tree Panels Demo");

setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

setSize(400,300);

setLocationRelativeTo(null);

tree = new JTree();

secondPan = new JPanel();

tree.addTreeSelectionListener(new TreeSelectionListener() {

public void valueChanged(TreeSelectionEvent evt) {

jTree1ValueChanged(evt);

}

});

getContentPane().add(new JScrollPane(tree), BorderLayout.WEST);

secondPan.setLayout(new BorderLayout());

getContentPane().add(secondPan, BorderLayout.CENTER);

}

private void jTree1ValueChanged(TreeSelectionEvent evt) {

Object treeNode = tree.getLastSelectedPathComponent();

if (secondPan.getComponents().length > 0) {

secondPan.remove(secondPan.getComponent(0));

}

String treeObj = treeNode.toString();

JPanel pan = new PianSpedUdsPan(this, treeNode, treeObj);

secondPan.add(pan, BorderLayout.CENTER);

secondPan.revalidate();

}

public static void main(final String args[]) {new TreePanelsDemo().setVisible(true);}

}

class PianSpedUdsPan extends JPanel{

private JToolBar jToolBar1;

public PianSpedUdsPan(JFrame parent, Object node, String str){

add(new JLabel(str));

jToolBar1 = new JToolBar();

JButton deselezionaBut = new JButton();

//deselezionaBut.setIcon(new ImageIcon(getClass().getResource("/it/infolog/sce/resources/ivolume_togli.png")));

deselezionaBut.setToolTipText("Escludi dalla spedizione");

deselezionaBut.setMaximumSize(new Dimension(40, 40));

deselezionaBut.setMinimumSize(new Dimension(40, 40));

deselezionaBut.setPreferredSize(new Dimension(40, 40));

deselezionaBut.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent evt) {

deselezionaButActionPerformed(evt);

}

});

jToolBar1.add(deselezionaBut);

add(jToolBar1);

}

private void deselezionaButActionPerformed(ActionEvent evt) {

System.out.println("- actionPerformed --");

}

}

Andre_Uhresa at 2007-7-12 22:57:32 > top of Java-index,Desktop,Core GUI APIs...
# 8

I try your suggestions, this is the result:

secondPan.revalidate()

secondPan.validate()

works but the top of the panel inside it not appear... I don't know why....

secondPan.repaint()

not works. The panel inside non appear

The only method that works fine is

secondPan.paintAll(secondPan.getGraphics());

In all of its cases (exclude obviously repaint()) the ActionPerformed doesn't work :-(

Sob..

Are there the methods that disable the ActionPerfomed that I could have inserted in my code?

My class is very complicated, it does a lot of things and there are a lot of method..

Monica

monksa at 2007-7-12 22:57:33 > top of Java-index,Desktop,Core GUI APIs...
# 9

> My class is very complicated, it does a lot of things and there are a lot of method..

Then you'd better start commenting out the ones you think could be problematic and you should soon see where the problem lies.

> Are there the methods that disable the ActionPerfomed that I could have inserted in my code?

I don't think any method can override or cancel out the functionality of another unless explicitly designed to do so.

ICE

icewalker2ga at 2007-7-12 22:57:33 > top of Java-index,Desktop,Core GUI APIs...
# 10

I'm a very stupid girl!!

I correct the problem!

In a panel that I included into JInternalFrame I override the method getParent() !

In a panel I create a

private MyInternalFrame parent;

with the get and set method

The method getParent override the primitive method of JPanel. This create the problem that I write.

Thanks for yours help

Bye

Monica

monksa at 2007-7-12 22:57:33 > top of Java-index,Desktop,Core GUI APIs...