Results 1 to 4 of 4
  1. #1
    chirag123 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Exclamation Opening files using GUI

    I'm working on this assignment for a 'bus company'. I have my main menu as bellow:
    Java 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:

    Java 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.

  2. #2
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    You need to add an Action Listener to the "retrieve" button.

  3. #3
    chirag123 is offline Member
    Join Date
    Apr 2011
    Posts
    9
    Rep Power
    0

    Default

    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.

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

Similar Threads

  1. opening an URL -
    By Sparky in forum New To Java
    Replies: 3
    Last Post: 02-07-2011, 03:58 PM
  2. Opening files in java
    By robby14 in forum Advanced Java
    Replies: 11
    Last Post: 04-19-2010, 03:59 AM
  3. Replies: 4
    Last Post: 10-07-2009, 09:14 AM
  4. Opening Text Files with Default System Editor
    By Pesch in forum Advanced Java
    Replies: 5
    Last Post: 10-08-2008, 06:17 PM
  5. opening jar files on pc prepared by mac
    By willemjav in forum Advanced Java
    Replies: 42
    Last Post: 09-27-2008, 05:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •