Home » Eclipse Projects » Standard Widget Toolkit (SWT) » Getting Menu to show in applet
Getting Menu to show in applet [message #447074] |
Wed, 08 December 2004 14:20 |
Chris Frommert Messages: 8 Registered: July 2009 |
Junior Member |
|
|
I am having trouble trying to get the menu and menu items to show in my
applet. It's my first attempt to create an applet and demonstarte the use
of the different toolkits while also porting over a SWT app I created. I'd
appreciate any assistance. Thank you in advance. I've included the code
below.
public void init ()
{
java.awt.Button awtButton = new java.awt.Button("AWT Button");
add(awtButton);
awtButton.addActionListener(new ActionListener (){
public void actionPerformed (ActionEvent event)
{
showStatus ("AWT Button Selected");
}
});
javax.swing.JButton jButton = new javax.swing.JButton("Swing
Button");
add(jButton);
jButton.addActionListener(new ActionListener () {
public void actionPerformed (ActionEvent event) {
showStatus ("Swing Button Selected");
}
});
this.setBounds(100,100,700,600);
Thread swtUIThread = new Thread (this);
swtUIThread.start ();
}
public void run()
{
Canvas awtParent = new Canvas();
add(awtParent);
Display display = new Display();
Shell shell = SWT_AWT.new_Shell(display, awtParent);
GridLayout grid = new GridLayout();
grid.numColumns = 2;
shell.setLayout(grid);
Menu menu = new Menu(shell, SWT.BAR);
MenuItem file = new MenuItem(menu, SWT.CASCADE);
file.setText("File");
Menu filemenu = new Menu(shell, SWT.DROP_DOWN);
file.setMenu(filemenu);
MenuItem createNewRecipe = new MenuItem(filemenu, SWT.PUSH);
createNewRecipe.setText("New Recipe");
MenuItem openExisting = new MenuItem(filemenu,SWT.PUSH);
openExisting.setText("Open Existing Recipe");
MenuItem menuDivide = new MenuItem(filemenu, SWT.SEPARATOR);
save = new MenuItem(filemenu,SWT.PUSH);
save.setText("Save");
save.setEnabled(false);
saveNew = new MenuItem(filemenu,SWT.PUSH);
saveNew.setText("Save as new XML document");
saveNew.setEnabled(false);
MenuItem menuDivide2 = new MenuItem(filemenu, SWT.SEPARATOR);
MenuItem close = new MenuItem(filemenu,SWT.PUSH);
close.setText("Exit");
shell.setMenuBar(menu);
Button swtButton = new Button(shell, SWT.PUSH);
swtButton.setText("SWT Button");
swtButton.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event){
showStatus("SWT Button selected.");
}
});
Button b = new Button(shell, SWT.PUSH);
b.setText("NEW BUTTON");
swtButton.addListener(SWT.Dispose, new Listener() {
public void handleEvent(Event event){
System.out.println("Button was disposed.");
}
});
// Size AWT Panel so that it is big enough to hold the SWT widgets
Point size = shell.computeSize (SWT.DEFAULT, SWT.DEFAULT);
awtParent.setSize(size.x * 20, size.y * 20);
validate();
shell.setBackground(new Color(display, 0,0,0));
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) display.sleep ();
}
}
|
|
|
Re: Getting Menu to show in applet [message #447121 is a reply to message #447074] |
Wed, 08 December 2004 21:33 |
Daniel Spiewak Messages: 263 Registered: July 2009 |
Senior Member |
|
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html;charset=ISO-8859-15"
http-equiv="Content-Type">
<title></title>
</head>
<body bgcolor="#ffffff" text="#000000">
<font face="serif">Really, the only problem I can see with the code
below is that you're not putting SWT in the main thread. This is an
absolute <i>must</i>. When SWT is not in the main thread, it becomes
non responsive and on some platforms the widgets don't display at all.
To this end, your applet will also not work on the Macintosh platform
because the java implementation for the mac doesn't allow access to the
main thread.<br>
<br>
Daniel<br>
</font><br>
Chris wrote:
<blockquote cite="midcp72k4$a49$1@www.eclipse.org" type="cite">I am
having trouble trying to get the menu and menu items to show in my
applet. It's my first attempt to create an applet and demonstarte the
use of the different toolkits while also porting over a SWT app I
created. I'd appreciate any assistance. Thank you in advance. I've
included the code below.
<br>
<br>
public void init () {
<br>
java.awt.Button awtButton = new java.awt.Button("AWT Button");
<br>
add(awtButton);
<br>
awtButton.addActionListener(new ActionListener (){
<br>
public void actionPerformed (ActionEvent event) {
<br>
showStatus ("AWT Button Selected");
<br>
}
<br>
});
<br>
<br>
javax.swing.JButton jButton = new javax.swing.JButton("Swing
Button");
<br>
add(jButton);
<br>
jButton.addActionListener(new ActionListener () {
<br>
public void actionPerformed (ActionEvent event) {
<br>
showStatus ("Swing Button Selected");
<br>
}
<br>
});
<br>
this.setBounds(100,100,700,600);
<br>
Thread swtUIThread = new Thread (this);
<br>
swtUIThread.start ();
<br>
}
<br>
public void run() {
<br>
Canvas awtParent = new Canvas();
<br>
add(awtParent);
<br>
<br>
Display display = new Display();
<br>
Shell shell = SWT_AWT.new_Shell(display, awtParent);
<br>
GridLayout grid = new GridLayout();
<br>
grid.numColumns = 2;
<br>
shell.setLayout(grid);
<br>
<br>
Menu menu = new Menu(shell, SWT.BAR);
<br>
<br>
MenuItem file = new MenuItem(menu, SWT.CASCADE);
<br>
file.setText("File");
<br>
Menu filemenu = new Menu(shell, SWT.DROP_DOWN);
<br>
file.setMenu(filemenu);
<br>
MenuItem createNewRecipe = new MenuItem(filemenu, SWT.PUSH);
<br>
createNewRecipe.setText("New Recipe");
<br>
MenuItem openExisting = new MenuItem(filemenu,SWT.PUSH);
<br>
openExisting.setText("Open Existing Recipe");
<br>
MenuItem menuDivide = new MenuItem(filemenu, SWT.SEPARATOR);
<br>
save = new MenuItem(filemenu,SWT.PUSH);
<br>
save.setText("Save");
<br>
save.setEnabled(false);
<br>
saveNew = new MenuItem(filemenu,SWT.PUSH);
<br>
saveNew.setText("Save as new XML document");
<br>
saveNew.setEnabled(false);
<br>
MenuItem menuDivide2 = new MenuItem(filemenu, SWT.SEPARATOR);
<br>
MenuItem close = new MenuItem(filemenu,SWT.PUSH);
<br>
close.setText("Exit");
<br>
shell.setMenuBar(menu);
<br>
<br>
Button swtButton = new Button(shell, SWT.PUSH);
<br>
swtButton.setText("SWT Button");
<br>
swtButton.addListener(SWT.Selection, new Listener() {
<br>
public void handleEvent(Event event){
<br>
showStatus("SWT Button selected.");
<br>
}
<br>
});
<br>
<br>
Button b = new Button(shell, SWT.PUSH);
<br>
b.setText("NEW BUTTON");
<br>
swtButton.addListener(SWT.Dispose, new Listener() {
<br>
public void handleEvent(Event event){
<br>
System.out.println("Button was disposed.");
<br>
}
<br>
});
<br>
<br>
// Size AWT Panel so that it is big enough to hold the SWT
widgets
<br>
Point size = shell.computeSize (SWT.DEFAULT, SWT.DEFAULT);
<br>
awtParent.setSize(size.x * 20, size.y * 20);
<br>
<br>
validate();
<br>
shell.setBackground(new Color(display, 0,0,0));
<br>
while (!shell.isDisposed()) {
<br>
if (!display.readAndDispatch()) display.sleep ();
<br>
}
<br>
}
<br>
<br>
</blockquote>
</body>
</html>
|
|
|
Re: Getting Menu to show in applet [message #447126 is a reply to message #447121] |
Wed, 08 December 2004 22:06 |
Chris Frommert Messages: 8 Registered: July 2009 |
Junior Member |
|
|
Can you please explain or provide an example? What do you mean by SWT: Are
you referring to the shell or all SWT components? By main I assume you
mean the <i>init</i> or correct me if I'm wrong.
Thanks,
Chris
Daniel Spiewak wrote:
> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
> <html>
> <head>
> <meta content="text/html;charset=ISO-8859-15"
> http-equiv="Content-Type">
> <title></title>
> </head>
> <body bgcolor="#ffffff" text="#000000">
> <font face="serif">Really, the only problem I can see with the code
> below is that you're not putting SWT in the main thread. This is an
> absolute <i>must</i>. When SWT is not in the main thread, it becomes
> non responsive and on some platforms the widgets don't display at all.
> To this end, your applet will also not work on the Macintosh platform
> because the java implementation for the mac doesn't allow access to the
> main thread.<br>
> <br>
> Daniel<br>
> </font><br>
> Chris wrote:
> <blockquote cite="midcp72k4$a49$1@www.eclipse.org" type="cite">I am
> having trouble trying to get the menu and menu items to show in my
> applet. It's my first attempt to create an applet and demonstarte the
> use of the different toolkits while also porting over a SWT app I
> created. I'd appreciate any assistance. Thank you in advance. I've
> included the code below.
> <br>
> <br>
> public void init () {
> <br>
> java.awt.Button awtButton = new java.awt.Button("AWT Button");
> <br>
> add(awtButton);
> <br>
> awtButton.addActionListener(new ActionListener (){
> <br>
> public void actionPerformed (ActionEvent event) {
> <br>
> showStatus ("AWT Button Selected");
> <br>
> }
> <br>
> });
> <br>
> <br>
> javax.swing.JButton jButton = new javax.swing.JButton("Swing
> Button");
> <br>
> add(jButton);
> <br>
> jButton.addActionListener(new ActionListener () {
> <br>
> public void actionPerformed (ActionEvent event) {
> <br>
> showStatus ("Swing Button Selected");
> <br>
> }
> <br>
> });
> <br>
> this.setBounds(100,100,700,600);
> <br>
> Thread swtUIThread = new Thread (this);
> <br>
> swtUIThread.start ();
> <br>
> }
> <br>
> public void run() {
> <br>
> Canvas awtParent = new Canvas();
> <br>
> add(awtParent);
> <br>
>
> <br>
> Display display = new Display();
> <br>
> Shell shell = SWT_AWT.new_Shell(display, awtParent);
> <br>
> GridLayout grid = new GridLayout();
> <br>
> grid.numColumns = 2;
> <br>
> shell.setLayout(grid);
> <br>
> <br>
> Menu menu = new Menu(shell, SWT.BAR);
> <br>
> <br>
> MenuItem file = new MenuItem(menu, SWT.CASCADE);
> <br>
> file.setText("File");
> <br>
> Menu filemenu = new Menu(shell, SWT.DROP_DOWN);
> <br>
> file.setMenu(filemenu);
> <br>
> MenuItem createNewRecipe = new MenuItem(filemenu, SWT.PUSH);
> <br>
> createNewRecipe.setText("New Recipe");
> <br>
> MenuItem openExisting = new MenuItem(filemenu,SWT.PUSH);
> <br>
> openExisting.setText("Open Existing Recipe");
> <br>
> MenuItem menuDivide = new MenuItem(filemenu, SWT.SEPARATOR);
> <br>
> save = new MenuItem(filemenu,SWT.PUSH);
> <br>
> save.setText("Save");
> <br>
> save.setEnabled(false);
> <br>
> saveNew = new MenuItem(filemenu,SWT.PUSH);
> <br>
> saveNew.setText("Save as new XML document");
> <br>
> saveNew.setEnabled(false);
> <br>
> MenuItem menuDivide2 = new MenuItem(filemenu, SWT.SEPARATOR);
> <br>
> MenuItem close = new MenuItem(filemenu,SWT.PUSH);
> <br>
> close.setText("Exit");
> <br>
> shell.setMenuBar(menu);
> <br>
> <br>
> Button swtButton = new Button(shell, SWT.PUSH);
> <br>
> swtButton.setText("SWT Button");
> <br>
> swtButton.addListener(SWT.Selection, new Listener() {
> <br>
> public void handleEvent(Event event){
> <br>
> showStatus("SWT Button selected.");
> <br>
> }
> <br>
> });
> <br>
> <br>
> Button b = new Button(shell, SWT.PUSH);
> <br>
> b.setText("NEW BUTTON");
> <br>
> swtButton.addListener(SWT.Dispose, new Listener() {
> <br>
> public void handleEvent(Event event){
> <br>
> System.out.println("Button was disposed.");
> <br>
> }
> <br>
> });
> <br>
>
> <br>
> // Size AWT Panel so that it is big enough to hold the SWT
> widgets
> <br>
> Point size = shell.computeSize (SWT.DEFAULT, SWT.DEFAULT);
> <br>
> awtParent.setSize(size.x * 20, size.y * 20);
> <br>
>
> <br>
> validate();
> <br>
> shell.setBackground(new Color(display, 0,0,0));
> <br>
> while (!shell.isDisposed()) {
> <br>
> if (!display.readAndDispatch()) display.sleep ();
> <br>
> }
> <br>
> }
> <br>
> <br>
> </blockquote>
> </body>
> </html>
|
|
|
Goto Forum:
Current Time: Thu Apr 25 05:00:14 GMT 2024
Powered by FUDForum. Page generated in 0.03014 seconds
|