-
Opening files using GUI
I'm working on this assignment for a 'bus company'. I have my main menu as bellow:
Code:
public class MainMenu
{
public static void main (String[] args)
{
JFrame mainMenu = new JFrame ("East Midlands Bus");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
mainMenu.setBounds((int) screenSize.getWidth()/2 - 370, (int) screenSize.getHeight()/2 - 300, 600, 450); // set position and size
mainMenu.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// exits on close
mainMenu.setResizable(false);
boolean resizable = mainMenu.isResizable();
Color c = new Color(100,200,255); // set colour of JFrame
Container con = mainMenu.getContentPane();
con.setBackground( c );
mainMenu.setLayout(null);
JLabel title = new JLabel("East Midlands Bus Route"); // new label
title.setBounds(185, 20, 500, 30); // set position and size
title.setForeground(Color.BLUE); // set colour of text to blue
title.setFont(new Font("Time", Font.BOLD, 18)); // change font and size of text
mainMenu.add(title);
JLabel bus = new JLabel(new ImageIcon("bus.jpg")); // finds picture
bus.setBounds(180, 100, 259, 194);
mainMenu.add(bus);
JButton price = new JButton("PRICE"); // creates new buttons
JButton time = new JButton("TIME");
time.addActionListener(new TimeButtonListener());
JButton route = new JButton("ROUTE");
JButton admin = new JButton("ADMIN");
admin.addActionListener(new AdminButtonListener());
JButton end = new JButton("END");
end.addActionListener(new EndButtonListener());
price.setBounds(45, 100, 90, 30); // sets sizes and locations of buttons
time.setBounds(45, 190, 90, 30);
route.setBounds(45, 275, 90, 30);
admin.setBounds(475, 190, 90, 30);
end.setBounds(250, 340, 90, 30);
price.setForeground(Color.BLUE); // changes appearence colour of buttons
price.setFont(new Font("Time", Font.BOLD, 12));
time.setForeground(Color.BLUE);
time.setFont(new Font("Time", Font.BOLD, 12));
route.setForeground(Color.BLUE);
route.setFont(new Font("Time", Font.BOLD, 12));
admin.setForeground(Color.BLUE);
admin.setFont(new Font("Time", Font.BOLD, 12));
end.setForeground(Color.BLUE);
end.setFont(new Font("Time", Font.BOLD, 12));
mainMenu.add(price); // adds buttons
mainMenu.add(time);
mainMenu.add(route);
mainMenu.add(admin);
mainMenu.add(end);
mainMenu.setVisible(true); // Display the window
}
}
class TimeButtonListener implements ActionListener
{
TimeButtonListener() {}
public void actionPerformed(ActionEvent a)
{
if (a.getActionCommand().equals("TIME"))
{
Time_First_Depot x = new Time_First_Depot();
}
}
}
class AdminButtonListener implements ActionListener
{
AdminButtonListener() {}
public void actionPerformed(ActionEvent a)
{
if (a.getActionCommand().equals("ADMIN"))
{
Admin x = new Admin();
}
}
}
class EndButtonListener implements ActionListener
{
EndButtonListener(){}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("END"))
{
System.exit(0);
}
}
}
which opens my admin menu:
Code:
class Admin extends JFrame
{
Admin()
{
Container c = getContentPane();
c.setLayout ( null );
Color b = new Color(100,200,255); // set colour of JFrame
c.setBackground( b );
JButton input = new JButton("INPUT ROUTE"); // creates new buttons
JButton retrieve = new JButton("RETRIEVE ROUTE");
JButton exit = new JButton("EXIT");
JLabel title = new JLabel("Admin"); // new label
title.setBounds(270, 20, 500, 30); // set position and size
title.setForeground(Color.BLUE); // set colour of text to blue
title.setFont(new Font("Time", Font.BOLD, 18)); // change font and size of text
c.add(title);
JLabel bus = new JLabel(new ImageIcon("bus.jpg")); // finds picture
bus.setBounds(180, 100, 259, 194);
c.add(bus);
JLabel todo = new JLabel("Please select your administration option:"); // new label
todo.setBounds(180, 50, 500, 30); // set position and size
todo.setForeground(Color.BLUE); // set colour of text to blue
todo.setFont(new Font("Time", Font.BOLD, 12)); // change font and size of text
c.add(todo);
JLabel eastmid = new JLabel("East Midlands Bus");
eastmid.setBounds(250,400,500,30);
eastmid.setForeground(Color.BLUE);
eastmid.setFont(new Font("Time", Font.BOLD, 12));
c.add(eastmid);
input.setForeground(Color.BLUE); // changes appearence colour of buttons
input.setFont(new Font("Time", Font.BOLD, 12));
retrieve.setForeground(Color.BLUE);
retrieve.setFont(new Font("Time", Font.BOLD, 12));
exit.setForeground(Color.BLUE);
exit.setFont(new Font("Time", Font.BOLD, 12));
c.add(input);
c.add(retrieve);
c.add(exit);
input.setBounds(70, 320, 150, 40);
retrieve.setBounds(380, 320, 150, 40);
exit.setBounds(250, 375, 90, 30);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
this.setBounds((int) screenSize.getWidth()/2 - 370, (int) screenSize.getHeight()/2 - 300, 600, 450); // set position and size
this.setResizable(false);
this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
this.setTitle("Admin");
this.setVisible(true);
this.setResizable(false);
}
}
I need to retrieve a route which is saved as a text file using the retrieve button in the admin menu and then open the route using the route button on the main menu but I'm not at all sure as to how to do this at all, and searching the internet hasn't been helpful at all.
Thanks in advance for any help that anybody can give.
-
You need to add an Action Listener to the "retrieve" button.
-
I know that I need an actionlistener but I'm not sure what my if statement would be. I haven't been able to find anything in the Java books I have and online all I've found are command prompt examples not GUI examples.
-
You can use a jfilechooser to open a GUI which allows you to search files on the computer. From there you can select a file and use io to read the file. Check out the io tutorials for more information.