473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

using SWT Label and popmenu to construct a menu

I want to use SWT Label and popmenu to construct a menubar ,and the effect
of this menubar is same to the menubar in SWT.When click the Label,it should be highlighted and popmenu shows.The issue is when click the label and move the mouse to enter next label,the next label can not be highlighted.Can anyone have a solution for it?
Here is my code:
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.*;

public class labelmenu {

protected Shell shell;
private menuComposite menucomposite;
private Display display;
/**
* Launch the application
* @param args
*/
public static void main(String[] args) {
try {
labelmenu window = new labelmenu();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Open the window
*/
public void open() {
display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}

/**
* Create contents of the window
*/
protected void createContents() {
shell = new Shell();
shell.setSize(500, 375);
shell.setText("SWT Application");

final Composite composite = new Composite(shell, SWT.NONE);
composite.setBounds(0, 0, 331, 54);

menucomposite = new menuComposite(display,composite, SWT.NONE);
menucomposite.setBounds(0, 2, 96, 20);
//
}
}

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.*;

public class menuComposite extends Composite implements MouseTrackListener{

private Display display;
private Label helpLabel;
private Label toolsLabel;
private Label fileLabel;
private Menu filemenu;
private MenuItem newMenuItem;
private MenuItem openMenuItem;
private MenuItem closeMenuItem;
private MenuItem exitMenuItem;
private Menu exportmenu;
private MenuItem exportMenuItem;
private MenuItem propertiesMenuItem;
private MenuItem keywordMenuItem;
private MenuItem addKeywordMenuItem;
private MenuItem reviewMetaTagMenuItem;
private MenuItem annotateMenuItem;
private MenuItem reviewAnnotationMenuItem;
private MenuItem congerPlaybackHelpMenuItem;
private MenuItem checkForUpdatesMenuItem;
private MenuItem aboutCongerPlaybackMenuItem;

private Color backgroudcolor;
private Color foregroudcolor1;
private Color foregroudcolor2;

public MenuItem getReviewAnnotationMenuItem()
{
return reviewAnnotationMenuItem;
}

public MenuItem getAnnotateMenuItem()
{
return annotateMenuItem;
}

public MenuItem getReviewMetaTagMenuItem()
{
return reviewMetaTagMenuItem;
}

public MenuItem getAddKeywordMenuItem()
{
return addKeywordMenuItem;
}

public MenuItem getPropertiesMenuItem()
{
return propertiesMenuItem;
}

public MenuItem getExportMenuItem()
{
return exportMenuItem;
}

public MenuItem getExitMenuItem()
{
return exitMenuItem;
}

public MenuItem getCloseMenuItem()
{
return closeMenuItem;
}

public MenuItem getOpenMenuItem()
{
return openMenuItem;
}

public MenuItem getNewMenuItem()
{
return newMenuItem;
}

public menuComposite(Display display,Composite parent, int style) {
super(parent, style);
this.display=display;

backgroudcolor=display.getSystemColor(SWT.COLOR_DA RK_BLUE);
foregroudcolor1=display.getSystemColor(SWT.COLOR_W HITE);
// SWTResourceManager.getColor(255, 255, 255);
foregroudcolor2=display.getSystemColor(SWT.COLOR_B LACK);
// SWTResourceManager.getColor(0, 0, 0);

fileLabel = new Label(this, SWT.NONE);
fileLabel.setBounds(0, 0, 32, 20);
fileLabel.setAlignment(SWT.CENTER);
fileLabel.setText("File");

filemenu = new Menu(fileLabel);
//MouseTrackAdapter aa=new MouseTrackAdapter();


//filemenu.addListener(SWT.MouseEnter,(Listener)aa);



// filemenu.addListener(SWT.MouseMove,new Listener( ) {
//// public void mouseEnter(MouseEvent e)
//// {
//// toolsLabel.setBackground(backgroudcolor);
//// toolsLabel.setForeground(foregroudcolor1);
//// }
//// public void mouseExit(MouseEvent e)
//// {
//// toolsLabel.setBackground(null);
//// toolsLabel.setForeground(foregroudcolor2);
//// }
// public void handleEvent(Event arg0) {
// // TODO ×Ô¶¯Éú³É·½·¨´æ¸ù
// System.out.println("mouse enter");
// }
// });

fileLabel.setMenu(filemenu);
fileLabel.addMouseTrackListener(new MouseTrackAdapter( ) {
public void mouseEnter(MouseEvent e)
{
fileLabel.setBackground(backgroudcolor);
fileLabel.setForeground(foregroudcolor1);
}
public void mouseExit(MouseEvent e)
{
fileLabel.setBackground(null);
fileLabel.setForeground(foregroudcolor2);
}
// public void mouseHover(MouseEvent e)
// {
// fileLabel.setBackground(backgroudcolor);
// fileLabel.setForeground(foregroudcolor1);
// }
});

newMenuItem = new MenuItem(filemenu, SWT.NONE);
newMenuItem.setAccelerator(SWT.CTRL | 'N');
newMenuItem.setText("New Ctrl+N");


openMenuItem = new MenuItem(filemenu, SWT.NONE);
openMenuItem.setAccelerator(SWT.CTRL | 'O');
openMenuItem.setText("Open Ctrl+O");

closeMenuItem = new MenuItem(filemenu, SWT.NONE);
closeMenuItem.setAccelerator(SWT.CTRL | 'W');
closeMenuItem.setText("Close Ctrl+W");

final MenuItem menuItem = new MenuItem(filemenu, SWT.SEPARATOR);
menuItem.setText("Menu item");

exitMenuItem = new MenuItem(filemenu, SWT.NONE);
exitMenuItem.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
System.exit(0);
}
});
exitMenuItem.setAccelerator(SWT.ALT | 'X');
exitMenuItem.setText("Exit Alt+X");

toolsLabel = new Label(this, SWT.NONE);
toolsLabel.setBounds(32, 0, 32, 20);
toolsLabel.setAlignment(SWT.CENTER);
toolsLabel.setText("Tools");
toolsLabel.addMouseTrackListener(new MouseTrackAdapter( ) {
public void mouseEnter(MouseEvent e)
{
toolsLabel.setBackground(backgroudcolor);
toolsLabel.setForeground(foregroudcolor1);
}
public void mouseExit(MouseEvent e)
{
toolsLabel.setBackground(null);
toolsLabel.setForeground(foregroudcolor2);
}
// public void mouseHover(MouseEvent e)
// {
// toolsLabel.setBackground(backgroudcolor);
// toolsLabel.setForeground(foregroudcolor1);
// }
});

exportmenu = new Menu(toolsLabel);
toolsLabel.setMenu(exportmenu);

exportMenuItem = new MenuItem(exportmenu, SWT.NONE);
exportMenuItem.setAccelerator(SWT.F2);
exportMenuItem.setText("Export F2");

final MenuItem menuItem_1 = new MenuItem(exportmenu, SWT.SEPARATOR);
menuItem_1.setText("Menu item");

propertiesMenuItem = new MenuItem(exportmenu, SWT.NONE);
propertiesMenuItem.setAccelerator(SWT.F3);
propertiesMenuItem.setText("Properties F3");

final MenuItem menuItem_2 = new MenuItem(exportmenu, SWT.SEPARATOR);
menuItem_2.setText("Menu item");

keywordMenuItem = new MenuItem(exportmenu, SWT.CASCADE);
keywordMenuItem.setText("Keyword");

final Menu menu = new Menu(keywordMenuItem);
keywordMenuItem.setMenu(menu);

final Menu menu_1 = new Menu(keywordMenuItem);
keywordMenuItem.setMenu(menu_1);

final Menu menu_2 = new Menu(keywordMenuItem);
keywordMenuItem.setMenu(menu_2);

final Menu menu_2_1 = new Menu(keywordMenuItem);
keywordMenuItem.setMenu(menu_2_1);

final Menu menu_4 = new Menu(keywordMenuItem);
keywordMenuItem.setMenu(menu_4);

final Menu menu_2_2 = new Menu(keywordMenuItem);
keywordMenuItem.setMenu(menu_2_2);

final Menu menu_5 = new Menu(keywordMenuItem);
keywordMenuItem.setMenu(menu_5);

addKeywordMenuItem = new MenuItem(menu_5, SWT.NONE);
addKeywordMenuItem.setText("Add Keyword to Scene");

reviewMetaTagMenuItem = new MenuItem(menu_5, SWT.NONE);
reviewMetaTagMenuItem.setText("Review Meta Tag File");

annotateMenuItem = new MenuItem(menu_5, SWT.NONE);
annotateMenuItem.setText("Annotate");

reviewAnnotationMenuItem = new MenuItem(menu_5, SWT.NONE);
reviewAnnotationMenuItem.setText("Review Annotation");

helpLabel = new Label(this, SWT.NONE);
helpLabel.setBounds(63, 0, 32, 20);
helpLabel.setAlignment(SWT.CENTER);
helpLabel.setText("Help");

final Menu helpmenu = new Menu(helpLabel);
helpLabel.setMenu(helpmenu);
helpLabel.addMouseTrackListener(new MouseTrackAdapter( ) {
public void mouseEnter(MouseEvent e)
{
helpLabel.setBackground(backgroudcolor);
helpLabel.setForeground(foregroudcolor1);
}
public void mouseExit(MouseEvent e)
{
helpLabel.setBackground(null);
helpLabel.setForeground(foregroudcolor2);
}
// public void mouseHover(MouseEvent e)
// {
// helpLabel.setBackground(backgroudcolor);
// helpLabel.setForeground(foregroudcolor1);
// }
});

congerPlaybackHelpMenuItem = new MenuItem(helpmenu, SWT.NONE);
congerPlaybackHelpMenuItem.setAccelerator(SWT.F1);
congerPlaybackHelpMenuItem.setText("Conger Playback Help F1");

final MenuItem menuItem_3 = new MenuItem(helpmenu, SWT.SEPARATOR);
menuItem_3.setText("Menu item");

checkForUpdatesMenuItem = new MenuItem(helpmenu, SWT.NONE);
checkForUpdatesMenuItem.setText("Check For Updates");

final MenuItem menuItem_4 = new MenuItem(helpmenu, SWT.SEPARATOR);
menuItem_4.setText("Menu item");

aboutCongerPlaybackMenuItem = new MenuItem(helpmenu, SWT.NONE);
aboutCongerPlaybackMenuItem.setText("About Conger Playback");

fileLabel.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent arg0) {
fileLabel.setBackground(backgroudcolor);
fileLabel.setForeground(foregroudcolor1);
toolsLabel.setBackground(null);
toolsLabel.setForeground(foregroudcolor2);
helpLabel.setBackground(null);
helpLabel.setForeground(foregroudcolor2);
Rectangle rect = fileLabel.getBounds();
Point pt = new Point(rect.x, rect.y + rect.height);
pt = fileLabel.getParent().toDisplay(pt);
filemenu.setLocation(pt.x, pt.y);
filemenu.setVisible(true);
}
});

toolsLabel.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent arg0) {
toolsLabel.setBackground(backgroudcolor);
toolsLabel.setForeground(foregroudcolor1);
fileLabel.setBackground(null);
fileLabel.setForeground(foregroudcolor2);
helpLabel.setBackground(null);
helpLabel.setForeground(foregroudcolor2);
Rectangle rect = toolsLabel.getBounds();
Point pt = new Point(rect.x, rect.y + rect.height);
pt = toolsLabel.getParent().toDisplay(pt);
exportmenu.setLocation(pt.x, pt.y);
exportmenu.setVisible(true);
}
});

helpLabel.addMouseListener(new MouseAdapter() {
public void mouseDown(MouseEvent arg0) {
helpLabel.setBackground(backgroudcolor);
helpLabel.setForeground(foregroudcolor1);
toolsLabel.setBackground(null);
toolsLabel.setForeground(foregroudcolor2);
fileLabel.setBackground(null);
fileLabel.setForeground(foregroudcolor2);
Rectangle rect = helpLabel.getBounds();
Point pt = new Point(rect.x, rect.y + rect.height);
pt = helpLabel.getParent().toDisplay(pt);
helpmenu.setLocation(pt.x, pt.y);
helpmenu.setVisible(true);
}
});
this.addMouseTrackListener(this);
}

public void mouseEnter(MouseEvent e){
if(e.x>=fileLabel.getBounds().x && e.x<=fileLabel.getBounds().x+fileLabel.getBounds() .width && e.y>=fileLabel.getBounds().y && e.y<=fileLabel.getBounds().y+fileLabel.getBounds() .height){
fileLabel.setBackground(backgroudcolor);
fileLabel.setForeground(foregroudcolor1);
toolsLabel.setBackground(null);
toolsLabel.setForeground(foregroudcolor2);
helpLabel.setBackground(null);
helpLabel.setForeground(foregroudcolor2);
}else if(e.x>=toolsLabel.getBounds().x && e.x<=toolsLabel.getBounds().x+toolsLabel.getBounds ().width && e.y>=toolsLabel.getBounds().y && e.y<=toolsLabel.getBounds().y+toolsLabel.getBounds ().height){
toolsLabel.setBackground(backgroudcolor);
toolsLabel.setForeground(foregroudcolor1);
fileLabel.setBackground(null);
fileLabel.setForeground(foregroudcolor2);
helpLabel.setBackground(null);
helpLabel.setForeground(foregroudcolor2);
}else if(e.x>=helpLabel.getBounds().x && e.x<=helpLabel.getBounds().x+helpLabel.getBounds() .width && e.y>=helpLabel.getBounds().y && e.y<=helpLabel.getBounds().y+helpLabel.getBounds() .height){
helpLabel.setBackground(backgroudcolor);
helpLabel.setForeground(foregroudcolor1);
fileLabel.setBackground(null);
fileLabel.setForeground(foregroudcolor2);
toolsLabel.setBackground(null);
toolsLabel.setForeground(foregroudcolor2);
}
}

public void mouseExit(MouseEvent e){
fileLabel.setBackground(null);
fileLabel.setForeground(foregroudcolor2);
toolsLabel.setBackground(null);
toolsLabel.setForeground(foregroudcolor2);
helpLabel.setBackground(null);
helpLabel.setForeground(foregroudcolor2);
}

public void mouseHover(MouseEvent e){
if(e.x>=fileLabel.getBounds().x && e.x<=fileLabel.getBounds().x+fileLabel.getBounds() .width && e.y>=fileLabel.getBounds().y && e.y<=fileLabel.getBounds().y+fileLabel.getBounds() .height){
fileLabel.setBackground(backgroudcolor);
fileLabel.setForeground(foregroudcolor1);
toolsLabel.setBackground(null);
toolsLabel.setForeground(foregroudcolor2);
helpLabel.setBackground(null);
helpLabel.setForeground(foregroudcolor2);
}else if(e.x>=toolsLabel.getBounds().x && e.x<=toolsLabel.getBounds().x+toolsLabel.getBounds ().width && e.y>=toolsLabel.getBounds().y && e.y<=toolsLabel.getBounds().y+toolsLabel.getBounds ().height){
toolsLabel.setBackground(backgroudcolor);
toolsLabel.setForeground(foregroudcolor1);
fileLabel.setBackground(null);
fileLabel.setForeground(foregroudcolor2);
helpLabel.setBackground(null);
helpLabel.setForeground(foregroudcolor2);
}else if(e.x>=helpLabel.getBounds().x && e.x<=helpLabel.getBounds().x+helpLabel.getBounds() .width && e.y>=helpLabel.getBounds().y && e.y<=helpLabel.getBounds().y+helpLabel.getBounds() .height){
helpLabel.setBackground(backgroudcolor);
helpLabel.setForeground(foregroudcolor1);
fileLabel.setBackground(null);
fileLabel.setForeground(foregroudcolor2);
toolsLabel.setBackground(null);
toolsLabel.setForeground(foregroudcolor2);
}
}

@Override
public void dispose() {
super.dispose();
}

protected void checkSubclass() {
}
}
Jun 6 '07 #1
1 3728
blazedaces
284 100+
First of all, never post this much code, read the forum posting guidelines about that. In general though, if you had posted a small section of code that contained your problem I or anyone else would be more inclined to read it then if it's such a large amount.

Secondly, put your code in code brackets (when you post look to the right in the box that says "reply guidelines").

Now, I believe your problem has to do with the fact that you are not calling repaint or even have a paint method.

Check out this tutorial: http://java.sun.com/docs/books/tutor...concepts2.html

The only way to update any image color or draw something is to use the paint method and repaint to change what area you want.

Good luck,
-blazed
Jun 6 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Anthony | last post by:
Hi all, i have managed to create an asp.net menu using xml and xslt.. i am now trying to highlite the selected path by sending in the path info and hilite it using an xsl select choose function....
1
by: Hrvoje Voda | last post by:
How to put an icon beside a text in popupmenu? Hrcko
4
by: Dennis | last post by:
A dumb question but what is the difference between the two properties; ContextMenu and PopMenu? -- Dennis in Houston
2
by: William Youngman | last post by:
We are developing an application that presents data to the user in a gridview and we are using the dropdown extender to give the user a SharePoint 2007 type dropdown menu attached to the cells of a...
1
by: palani ganesh | last post by:
hi all, i m ganesh a newbie to this site........ In my program i want to insert, delete items in the listcontrol using the popmenu.....Is ther anyone to help me out???? awaiting for ur...
0
missinglinq
by: missinglinq | last post by:
Having been asked, for the umpteenth time, how to use the MouseMove property to change the appearance of an object, I put together this short tutorial and sample database today. Perhaps it will be of...
0
by: ddubey | last post by:
Hello Developer, I need an urgent help. I have a xml file that contains some Menu tag like this. <?xml version="1.0" encoding="utf-8" ?> <Menu> <Group Flow="Horizontal"> <Item...
1
by: viv1tyagi | last post by:
Hi everyone ! ! ! I'm just a month old in the world of Python and trying to develop an application using Tkinter in which a new window pops out on a particular button press.The code for this new...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.