Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Menu bar to execute programs

Status
Not open for further replies.

Wrathchild

Technical User
Aug 24, 2001
303
US
Ok 2-part quesion here:

I'm still VERY new to Java and am trying to make my first basic application. At work I build Access applications and I'm trying to build a menu bar in Java that would allow the users to select an application from a listing instead of having multiple shortcuts on their desktops as they do now.

Part 1 - I have code to build the skeleton of the menu, however I'm lost when it comes to the actual handling of the selection clicked (actionhandler)

Part 2 - when the user selects an item, I need to open the corresponding Access (.mde) application

any input would be appreciated; below is what I have so far
-----------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ApplicationList extends JFrame {
public ApplicationList() {
super("Application List");
setSize(300, 225);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuItem kr1 = new JMenuItem("App1");
JMenuItem kr2 = new JMenuItem("App2");
JMenuItem kr3 = new JMenuItem("App3");
JMenuItem kr4 = new JMenuItem("App4");
JMenuItem kr5 = new JMenuItem("App5");

JMenu m1 = new JMenu("Application");

m1.add(kr1);
m1.add(kr2);
m1.addSeparator();
m1.add(kr3);
m1.add(kr4);
m1.addSeparator();
m1.add(kr5);

JMenuBar jmb = new JMenuBar();
jmb.add(m1);
setJMenuBar(jmb);
setVisible(true);

}

public static void main(String[] arguments) {
ApplicationList md = new ApplicationList();
}
}
 
^^^^^ fyi, I'm not looking to actually use Access with Java...just open the application on it's own, just as you would call an .exe application. I'm looking for the syntax to open the apps.
 
As far as handling the menu events, I can tell you how I do it. First of all, however, I don't use Swing, just AWT; but it should work pretty much the same way. I'm pretty new to Java myself but this works for me.
You need to add a listener:
kr1.addActionListener(this); for example

Then you need an event handling method:
public void actionPerformed(ActionEvent e) {
Object esrc = e.getSource();
if (esrc instanceof MenuItem && e.getActionCommand()=="App1") {
//do what you do;
}
}


Now, as far executing a Windows program, I think you do something in the Runtime class:
from the Java Developers Alamanac, try {
// Execute a command without arguments
String command = "ls";
Process child = Runtime.getRuntime().exec(command);

// Execute a command with an argument
command = "ls /tmp";
child = Runtime.getRuntime().exec(command);
} catch (IOException e) {
}


Bob Rashkin
rrashkin@csc.com
 
thanks, I'll give it a shot...I noticed on the site they are referencing a .txt document, so hopefully I can just replace with an .mde application.
 
ok, here's what I have so far: compiled ok, but it's not opening the text file ( and yes it exists )

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

public class ApplicationList extends JFrame {
public ApplicationList() {
super("Application List");
setSize(300, 225);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuItem kr1 = new JMenuItem("App1");
JMenuItem kr2 = new JMenuItem("App2");
JMenuItem kr3 = new JMenuItem("App3");
JMenuItem kr4 = new JMenuItem("App4");
JMenuItem kr5 = new JMenuItem("App5");

JMenu m1 = new JMenu("Application");

m1.add(kr1);
m1.add(kr2);
m1.addSeparator();
m1.add(kr3);
m1.add(kr4);
m1.addSeparator();
m1.add(kr5);

JMenuBar jmb = new JMenuBar();
jmb.add(m1);
setJMenuBar(jmb);
setVisible(true);


}
public void actionPerformed(ActionEvent e) {
Object esrc = e.getSource();
if (esrc instanceof MenuItem && e.getActionCommand()=="App1") {
try {
String[] commands = new String[]{"c:\\Main\\Test Doc.txt"};
Process child = Runtime.getRuntime().exec(commands);
} catch (IOException a) {
}
}
}
public static void main(String[] arguments) {
ApplicationList md = new ApplicationList();
}
}
 
kr1.addactionlistener(this);

where would I put this? it doesn't like it anywhere
 
ok, after more research this is what I have, text file still not opening though

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


public class ApplicationList2 extends JFrame {
private JMenuItem itsInput = new JMenuItem();
public ApplicationList2() {
super("Application List");
setSize(300, 225);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuItem kr1 = new JMenuItem("App1");
itsInput.addActionListener (new kr1());
JMenuItem kr2 = new JMenuItem("App2");
JMenuItem kr3 = new JMenuItem("App3");
JMenuItem kr4 = new JMenuItem("App4");
JMenuItem kr5 = new JMenuItem("App5");

JMenu m1 = new JMenu("Application");

m1.add(kr1);
m1.add(kr2);
m1.addSeparator();
m1.add(kr3);
m1.add(kr4);
m1.addSeparator();
m1.add(kr5);

JMenuBar jmb = new JMenuBar();
jmb.add(m1);
setJMenuBar(jmb);
setVisible(true);

}
private class kr1 implements ActionListener {

public void actionPerformed(ActionEvent e) {
Object esrc = e.getSource();
if (esrc instanceof MenuItem && e.getActionCommand()=="App1") {
try {
String[] commands = new String[]{"Notepad.exe","C:\\Main\\TestDoc.txt"};
Process child = Runtime.getRuntime().exec(commands);
} catch (IOException a) {
}
}
}
}
public static void main(String[] arguments) {
ApplicationList md = new ApplicationList();
}
}
 
Try declaring your public class as:

public classApplicationList extends JFrame implements ActionListener

Then place the all your methods in one class. Before adding your menu items to the menu, you must add an actionlistener to the menu item.

i.e kr1.addActionListener(this);
m1.add(kr1);

Also declare your menu items as module variables. Then you will be able to use the following in your actionperformed method:
if (esrc == kr1)
 
stefanwagner - I've tried the complete path before, no luck

metheus - I've tried to use "public classApplicationList extends JFrame implements ActionListener" but it always errors...I think the code is screwy in several places and I can't figure out where since it compiles.

could somebody run my code and get it to open a text document? I've researched many hours and I think I just need to see the final product to actually understand what I'm doing wrong.

thanks
 
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;

public class ApplicationList2 extends JFrame
{
	private JMenuItem itsInput = new JMenuItem ();

	public ApplicationList2 ()
	{
		super ("Application List");
		setSize (300, 225);
		setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

		JMenuItem kr1 = new JMenuItem ("App1");
		JMenuItem kr2 = new JMenuItem ("App2");
		JMenuItem kr3 = new JMenuItem ("App3");
		JMenuItem kr4 = new JMenuItem ("App4");
		JMenuItem kr5 = new JMenuItem ("App5");

		ActionListener al = new MyListener ();
		kr1.addActionListener (al);

		JMenu m1 = new JMenu ("Application");

		m1.add (kr1);
		m1.add (kr2);
		m1.addSeparator ();
		m1.add (kr3);
		m1.add (kr4);
		m1.addSeparator ();
		m1.add (kr5);

		JMenuBar jmb = new JMenuBar ();
		jmb.add (m1);
		setJMenuBar (jmb);
		setVisible (true);
	}

	private class MyListener implements ActionListener
	{
		public void actionPerformed (ActionEvent e)
		{
			String cmd = e.getActionCommand ();
			if (cmd.equals ("App1"))
			{
				try
				{
					String [] commands = new String [] {"/usr/bin/scite","/home/stefan/tmp.txt"};
					System.out.println (commands);
					Process child = Runtime.getRuntime ().exec (commands);
				} catch (IOException a)
				{
					System.err.println (a.getMessage ());
				}
			}
			else System.out.println ("foo");
		}
	}

	public static void main (String [] arguments)
	{
		ApplicationList2 md = new ApplicationList2 ();
	}
}
starts working.

a) You renamed ApplicationList to ApplicationList2, but called ApplicationList from main.
b) instanceof isn't needed.
c) Stringcomparision is done by s.equals ("App1"), not s=="App1".
d) use code-tags in the forum
e) and proper indentation
f) make a cleaner solution with help of this link:
For further documentation and for examples, see How to Use Menus in The Java Tutorial.


seeking a job as java-programmer in Berlin:
 
stef: what does "/usr/bin/scite" represent? and why is there no root directory in there?
 
woo-hoo! I finally got it to work! Here's what I had to change from Stef's response:

String [] commands = new String [] {"C:\\WINNT\\NOTEPAD.EXE","C:\\My Data\\java\\Test.txt"};

just tried to open an Access db and it worked fine

thanks a million to all responders, especially Stef!
 
I am new to Java. I got the program above working my only problem is I wanted to add an

else if (

for the next option. It doesn't work. I just copied the same if and added else. I also changed the .equals to the next button name.

Any help?
 
Sorry I figured it out. I forgot the kr2.addActionListener (al);

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top