By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,784 Members | 1,239 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,784 IT Pros & Developers. It's quick & easy.

using SWT Label and popmenu to construct a menu

P: 2
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
Share this Question
Share on Google+
1 Reply


blazedaces
100+
P: 284
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

Post your reply

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