Results 1 to 7 of 7
  1. #1
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Exclamation Switching between panels

    HI, I'm new to java and I need help with this project that i'm working on. I would like to press the next button to go to another panel but i don't know how to do that. Also i would like make it so that is one or multiple radiobuttons is un-pressed it doesn't display the message "You've order..." How do I do this. Please I only have a few more hours to work on doing this. Thanks.

    Java Code:
    import javax.swing.BorderFactory;
    import javax.swing.JComponent;
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import javax.swing.ImageIcon;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    
    
    public class Condiments extends JPanel
                                 implements ActionListener {
    	
    	
    
        static String saltString = "Salt";
        static String pepperString = "Pepper";
        static String oreganoString = "Oregano";
        static String oilString = "Oil";
        static String vinegarString = "Vinegar";
        static String ketchupString = "Ketchup";
        static String mustardString = "Mustard";
        static String mayoString = "Mayonnaise";
        static String chipotlesauceString = "Chipotle Sauce";
        static String hotsauceString = "Hot Sauce";
        static String bbqsauceString = "BAR-B-Que Sauce";
        static String nextString = "Next";
        
        JCheckBox saltButton;
        JCheckBox pepperButton;
        JCheckBox oreganoButton;
        JCheckBox oilButton;
        JCheckBox vinegarButton;
        JCheckBox ketchupButton;
        JCheckBox mustardButton;
        JCheckBox mayoButton;
        JCheckBox chipotlesauceButton;
        JCheckBox hotsauceButton;
        JCheckBox bbqsauceButton;
        JButton nextButton;
     
        JLabel picture;
        
     
        public Condiments() {
         		
           
            saltButton = new JCheckBox(saltString);
            saltButton.setMnemonic(KeyEvent.VK_B);
            saltButton.setActionCommand(saltString);
            saltButton.setSelected(false);
           
            pepperButton = new JCheckBox(pepperString);
            pepperButton.setMnemonic(KeyEvent.VK_B);
            pepperButton.setActionCommand(pepperString);
            pepperButton.setSelected(false);
     
            oreganoButton = new JCheckBox(oreganoString);
            oreganoButton.setMnemonic(KeyEvent.VK_C);
            oreganoButton.setActionCommand(oreganoString);
            oreganoButton.setSelected(false);
     
            oilButton = new JCheckBox(oilString);
            oilButton.setMnemonic(KeyEvent.VK_D);
            oilButton.setActionCommand(oilString);
            oilButton.setSelected(false);
     
            vinegarButton = new JCheckBox(vinegarString);
            vinegarButton.setMnemonic(KeyEvent.VK_E);
            vinegarButton.setActionCommand(vinegarString);
            vinegarButton.setSelected(false);
     
            ketchupButton = new JCheckBox(ketchupString);
            ketchupButton.setMnemonic(KeyEvent.VK_F);
            ketchupButton.setActionCommand(ketchupString);
            ketchupButton.setSelected(false);
            
            mustardButton = new JCheckBox(mustardString);
            mustardButton.setMnemonic(KeyEvent.VK_R);
            mustardButton.setActionCommand(mustardString);
            mustardButton.setSelected(false);
            
            mayoButton = new JCheckBox(mayoString);
            mayoButton.setMnemonic(KeyEvent.VK_P);
            mayoButton.setActionCommand(mayoString);
            mayoButton.setSelected(false);
            
            chipotlesauceButton = new JCheckBox(chipotlesauceString);
            chipotlesauceButton.setMnemonic(KeyEvent.VK_P);
            chipotlesauceButton.setActionCommand(chipotlesauceString);
            chipotlesauceButton.setSelected(false);
            
            hotsauceButton = new JCheckBox(hotsauceString);
            hotsauceButton.setMnemonic(KeyEvent.VK_P);
            hotsauceButton.setActionCommand(hotsauceString);
            hotsauceButton.setSelected(false);
            
            bbqsauceButton = new JCheckBox(bbqsauceString);
            bbqsauceButton.setMnemonic(KeyEvent.VK_P);
            bbqsauceButton.setActionCommand(bbqsauceString);
            bbqsauceButton.setSelected(false);
            
            nextButton = new JButton(nextString);
            nextButton.setMnemonic(KeyEvent.VK_P);
            nextButton.setActionCommand(nextString);
            nextButton.setSelected(false);
     
            saltButton.addActionListener(this);
            pepperButton.addActionListener(this);
            oreganoButton.addActionListener(this);
            oilButton.addActionListener(this);
            vinegarButton.addActionListener(this);
            ketchupButton.addActionListener(this);
            mustardButton.addActionListener(this);
            mayoButton.addActionListener(this);
            chipotlesauceButton.addActionListener(this);
            hotsauceButton.addActionListener(this);
            bbqsauceButton.addActionListener(this);
            nextButton.addActionListener(this);
     
           
            picture = new JLabel(new ImageIcon("D:\\Workspace\\Project1\\"
                                                 + saltString
                                                 + ".jpg")); 
     
           
            picture.setPreferredSize(new Dimension(300, 300));
            add(picture, BorderLayout.CENTER);
     
            JPanel radioPanel = new JPanel(new GridLayout(2, 6));
            radioPanel.add(saltButton);
            radioPanel.add(pepperButton);
            radioPanel.add(oreganoButton);
            radioPanel.add(oilButton);
            radioPanel.add(vinegarButton);
            radioPanel.add(ketchupButton);
            radioPanel.add(mustardButton);
            radioPanel.add(mayoButton);
            radioPanel.add(chipotlesauceButton);
            radioPanel.add(hotsauceButton);
            radioPanel.add(bbqsauceButton);
            JButton radioButton = new JButton();
            radioButton.add(nextButton);
            
            add(radioButton);
            add(radioPanel, BorderLayout.LINE_START);
            
            setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
            
        }
     
        
        public void actionPerformed(ActionEvent event) {
        	
            picture.setIcon(new ImageIcon("D:\\Workspace\\Project1\\"
                                            + event.getActionCommand()
                                            + ".jpg")); 
        	
    	        JOptionPane.showMessageDialog( null,
    	          "You've Added " + event.getActionCommand() + " To Your Order" );
        	}
        
        
        /** Returns an ImageIcon, or null if the path was invalid. */
        protected static ImageIcon createImageIcon(String path) {
            java.net.URL imgURL = Condiments.class.getResource(path);
            if (imgURL != null) {
                return new ImageIcon(imgURL);
            } else {
                System.err.println("Couldn't find file: " + path);
                return null;
            }
        }
     
       
        private static void createAndShowGUI() {
           
        	JFrame frame = new JFrame("Toppings");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
            JComponent newContentPane = new Condiments();
            newContentPane.setOpaque(true); //content panes must be opaque
            
            frame.setLocation(400,120);
            frame.setContentPane(newContentPane);
            
            
            frame.pack();
            frame.setVisible(true);
        }
     
        public static void main(String[] args) {
            
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    createAndShowGUI();
                }
            });
           
    		    
        }
    }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    2,843
    Rep Power
    6

    Default Re: Switching between panels

    Switching between JPanels is a job for CardLayout.

    You're going to have to add a listener that detects when a radio button is selected, then take the appropriate action.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: Switching between panels

    That's exactly what i did in the code for the radio button. My problem is not with the radio button but with the JButton. I want nextButton to go to a new panel. How would I do this without making all of the buttons do this?

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    2,843
    Rep Power
    6

    Default Re: Switching between panels

    Are you just asking how to do different things when different buttons are clicked?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: Switching between panels

    Yea. I Believe so. I would like the "nextButton" to change the panel and the radioButtons to add a string of their own and save it until every thing is done. Is there a way to change my code to cardLayout?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    2,843
    Rep Power
    6

    Default Re: Switching between panels

    You can either add the same ActionListener to each JButton, then compare the ActionEvent.getSource() to each button- it will contain a reference to the JButton that was clicked. Or you can just add a different ActionListener to each JButton.

    And a quick google of "Java CardLayout" will give you the tutorial that should explain everything you need to know.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    9,928
    Rep Power
    16

    Default Re: Switching between panels

    Moved from New to Java

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

Similar Threads

  1. Switching between GUI's
    By MrZalib in forum AWT / Swing
    Replies: 3
    Last Post: 05-21-2011, 08:46 PM
  2. Need some help with panels inside panels
    By kakefjes in forum AWT / Swing
    Replies: 0
    Last Post: 03-17-2011, 11:36 AM
  3. Switching Screens
    By vmcg in forum New To Java
    Replies: 4
    Last Post: 07-08-2010, 07:34 PM
  4. Calculator Switching
    By dilpreet28 in forum New To Java
    Replies: 9
    Last Post: 06-08-2010, 02:05 AM
  5. switching packages
    By DuceDuceExplorer in forum NetBeans
    Replies: 4
    Last Post: 07-09-2008, 11:11 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
  •