The Artima Developer Community
Sponsored Link

Java Answers Forum
swing , when mouse moves over jlabel not firing the event

3 replies on 1 page. Most recent reply: Apr 19, 2002 8:46 AM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
shekar

Posts: 2
Nickname: chandra
Registered: Apr, 2002

swing , when mouse moves over jlabel not firing the event Posted: Apr 19, 2002 2:44 AM
Reply to this message Reply
Advertisement
package swingtest;

import javax.swing.JFrame;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

/**
* <p>Title: Application class</p>
* <p>Description: This is a sample class which includes menu,toolbar,JSplitPane </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: scaraboo.GmbH </p>
* @author chandra chandra@scaraboo.com
* @version 1.0
*/

public class Application extends JFrame implements ActionListener {
JMenu menu_file, menu_application, menu_help;
JMenuBar menubar;
JToolBar toolbar;
JLabel label_navigation,label_contentpane,label_status;
JSplitPane splitpane; // this pane separates the components with a divider
Application application;
JButton right_button,left_button;

public Application() {
super("Application");
menubar = new JMenuBar();
menu_file = new JMenu("File");
menu_file.add(new JMenuItem("save"));
menu_file.add(new JMenuItem("quit"));
menubar.add(menu_file);
menu_application = new JMenu("Application");
menu_application.add(new JMenuItem("Run")).addActionListener(this);
menu_application.add(new JMenuItem("Stop"));
menubar.add(menu_application);
menu_help = new JMenu("Help");
menu_help.add(new JMenuItem("Contents"));
menu_help.add(new JMenuItem("about"));
menubar.add(menu_help);

toolbar = new JToolBar(JToolBar.HORIZONTAL);
toolbar.setFloatable(false);
toolbar.setAlignmentX(JToolBar.LEFT_ALIGNMENT);
//toolbar.add(new JButton(new ImageIcon("images/left.gif")));
left_button =new JButton(new ImageIcon("images/left.gif","left"));
left_button.addActionListener(this);
toolbar.add(left_button);
toolbar.add(new JButton(new ImageIcon("images/middle.gif")));

right_button =new JButton(new ImageIcon("images/right.gif","right"));
right_button.addActionListener(this);
toolbar.add(right_button);
//toolbar.add(new JButton(new ImageIcon("images/right.gif","right")).addActionListener(this));

Container content_pane = getContentPane();
content_pane.setLayout(new BoxLayout(content_pane,BoxLayout.Y_AXIS));
setJMenuBar(menubar);
content_pane.add(toolbar);

JPanel panel_navigation = new JPanel(new BorderLayout());
label_navigation = new JLabel("Navigation-Pane");
panel_navigation.add(label_navigation,BorderLayout.NORTH);
JScrollPane scrollpane_navigation = new JScrollPane(panel_navigation);

label_status = new JLabel("Status");
JPanel panel_contentpane = new JPanel();
label_contentpane = new JLabel("Content-Pane");
label_contentpane.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseEntered(MouseEvent e) {
System.out.println("hello");
label_status.setText("Content-Pane");
}
public void mouseExited(MouseEvent e) {
label_status.setText(" ");
}
public void mouseClicked(MouseEvent e) {
System.out.println("hello");
label_status.setText("Content-Pane");
}
});
label_contentpane.setAlignmentX(JLabel.CENTER_ALIGNMENT);
panel_contentpane.add(label_contentpane);

JScrollPane scrollpane_contentpane = new JScrollPane(panel_contentpane);
//JSplitPane separates the two scrollpanes with divider between them
splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,
scrollpane_navigation,scrollpane_contentpane);

splitpane.setOneTouchExpandable(true);
splitpane.setDividerLocation(100);
Dimension minimum_size = new Dimension(100,50);
scrollpane_navigation.setMinimumSize(minimum_size);
scrollpane_contentpane.setMinimumSize(minimum_size);
splitpane.setPreferredSize(new Dimension(400, 200));

content_pane.add(splitpane);


JToolBar statusbar =new JToolBar("Status",JToolBar.HORIZONTAL);
statusbar.setAlignmentX(JToolBar.LEFT_ALIGNMENT);
statusbar.add(label_status);
statusbar.setFloatable(false);
content_pane.add(statusbar);
}
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Run")) {
label_status.setText("Run");
}
if(e.getSource().equals(right_button)) {
splitpane.setDividerLocation(200);
}
if(e.getSource().equals(left_button)) {
splitpane.setDividerLocation(100);
}
}
public void setStatus(String s) {
label_status.setText(s);
}
/**
*@params args a String array
*/
public static void main(String[] args) {
Application frame = new Application();
frame.pack();
frame.setVisible(true);
//frame.setStatus("Hello World");

}
}


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: swing , when mouse moves over jlabel not firing the event Posted: Apr 19, 2002 4:55 AM
Reply to this message Reply
chandra,
What is the matter ?
Or more exactly, where is the matter ?
I got you code on the laptop & there is no bug ...
So Over what label do you want to see twhat ?
A ToolTip over the menu items or ...
Thomas,
p.s. : Could you also use the following page http://www.artima.com/forums/howtopost.html formatting always life easier !

shekar

Posts: 2
Nickname: chandra
Registered: Apr, 2002

Re: swing , when mouse moves over jlabel not firing the event Posted: Apr 19, 2002 8:24 AM
Reply to this message Reply
Hello Thomas my problem is when mouse enters the JLabel, i.e label_contentpane in my code it is not firing the event and set another JLabel , i.e label_status ,must execute label_status.setText("Content-Pane"); hope u understood my point.

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: swing , when mouse moves over jlabel not firing the event Posted: Apr 19, 2002 8:46 AM
Reply to this message Reply
To me eveything looks fine ...
Should a Label / JLabel react to the events you want to trap ?
I dunno by heart but I believe NOT


package test.gui;
 
import javax.swing.JFrame;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
 
/**
* <p>Title: Application class</p>
* <p>Description: This is a sample class which includes menu,toolbar,JSplitPane </p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: scaraboo.GmbH </p>
* @author chandra chandra@scaraboo.com
* @version 1.0
*/
 
public class Application 
	extends JFrame 
	implements ActionListener 
{
	JMenu menu_file, menu_application, menu_help;
	JMenuBar menubar;
	JToolBar toolbar;
	JLabel label_navigation,label_contentpane,label_status;
	JSplitPane splitpane; // this pane separates the components with a divider
	Application application;
	JButton right_button,left_button;
	
	public Application() {
		super("Application");
		menubar = new JMenuBar();
		menu_file = new JMenu("File");
		menu_file.add(new JMenuItem("save"));
		menu_file.add(new JMenuItem("quit"));
		menubar.add(menu_file);
		menu_application = new JMenu("Application");
		menu_application.add(new JMenuItem("Run")).addActionListener(this);
		menu_application.add(new JMenuItem("Stop"));
		menubar.add(menu_application);
		menu_help = new JMenu("Help");
		menu_help.add(new JMenuItem("Contents"));
		menu_help.add(new JMenuItem("about"));
		menubar.add(menu_help);
		
		toolbar = new JToolBar(JToolBar.HORIZONTAL);
		toolbar.setFloatable(false);
		toolbar.setAlignmentX(JToolBar.LEFT_ALIGNMENT);
		//toolbar.add(new JButton(new ImageIcon("images/left.gif")));
		left_button =new JButton(new ImageIcon("images/left.gif","left"));
		left_button.addActionListener(this);
		toolbar.add(left_button);
		toolbar.add(new JButton(new ImageIcon("images/middle.gif")));
		
		right_button =new JButton(new ImageIcon("images/right.gif","right"));
		right_button.addActionListener(this);
		toolbar.add(right_button);
		//toolbar.add(new JButton(new ImageIcon("images/right.gif","right")).addActionListener(this));
		
		Container content_pane = getContentPane();
		content_pane.setLayout(new BoxLayout(content_pane,BoxLayout.Y_AXIS));
		setJMenuBar(menubar);
		content_pane.add(toolbar);
		
		JPanel panel_navigation = new JPanel(new BorderLayout());
		label_navigation = new JLabel("Navigation-Pane");
		panel_navigation.add(label_navigation,BorderLayout.NORTH);
		JScrollPane scrollpane_navigation = new JScrollPane(panel_navigation);
		
		label_status = new JLabel("Status");
		JPanel panel_contentpane = new JPanel();
		label_contentpane = new JLabel("Content-Pane");
		label_contentpane.addMouseListener(
			new MouseListener ()
			{
				public void mouseClicked (MouseEvent e) 
				{
          			System.out.println("mouseClicked");
				}
				public void mouseEntered (MouseEvent e) 
				{
          			System.out.println("mouseEntered");          	
				}
				public void mouseExited (MouseEvent e) 
				{
          			System.out.println("mouseExited");          	
				}
				public void mousePressed (MouseEvent e) 
				{
          			System.out.println("mousePressed");          	
				}
				public void mouseReleased (MouseEvent e) 
				{
          			System.out.println("mouseReleased");          	
				}
	
			} );
		label_contentpane.addMouseMotionListener(new MouseMotionAdapter() 
		{
			public void mouseEntered(MouseEvent e) 
			{
				System.out.println("MouseMotionAdapter::mouseEntered");
				label_status.setText("Content-Pane");
			}
	
			public void mouseExited(MouseEvent e) 
			{
				label_status.setText("MouseMotionAdapter::mouseExited");
			}
	
			public void mouseClicked(MouseEvent e) 
			{
				System.out.println("MouseMotionAdapter::mouseClicked");
				label_status.setText("Content-Pane");
			}
		} );
	label_contentpane.setAlignmentX(JLabel.CENTER_ALIGNMENT);
	panel_contentpane.add(label_contentpane);
	
	JScrollPane scrollpane_contentpane = new JScrollPane(panel_contentpane);
	//JSplitPane separates the two scrollpanes with divider between them
	splitpane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,true,
	scrollpane_navigation,scrollpane_contentpane);
	
	splitpane.setOneTouchExpandable(true);
	splitpane.setDividerLocation(100);
	Dimension minimum_size = new Dimension(100,50);
	scrollpane_navigation.setMinimumSize(minimum_size);
	scrollpane_contentpane.setMinimumSize(minimum_size);
	splitpane.setPreferredSize(new Dimension(400, 200));
	
	content_pane.add(splitpane);
	
	
	JToolBar statusbar =new JToolBar("Status",JToolBar.HORIZONTAL);
	statusbar.setAlignmentX(JToolBar.LEFT_ALIGNMENT);
	statusbar.add(label_status);
	statusbar.setFloatable(false);
	content_pane.add(statusbar);
	}
	
	public void actionPerformed(ActionEvent e) 
	{
		if(e.getActionCommand().equals("Run")) 		
			label_status.setText("Run");
		
		if(e.getSource().equals(right_button)) 
			splitpane.setDividerLocation(200);
	
		if(e.getSource().equals(left_button)) 
			splitpane.setDividerLocation(100);
	
	}
	
	public void setStatus(String s) 
	{
		label_status.setText(s);
	}
	/**
	*@params args a String array
	*/
	public static void main(String[] args) 
	{
		Application frame = new Application();
		frame.pack();
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//frame.setStatus("Hello World");	
	}
} 
 

Flat View: This topic has 3 replies on 1 page
Topic: credit card payement page! Previous Topic   Next Topic Topic: File Dependencies with Ant

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use