Results 1 to 12 of 12
  1. #1
    zzuum is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default Multiple JPanels in Swing?

    Hello, I am making an "ArtGallery" with JSwing (which I am just learning on my own) and want to use multiple JButtons to navigate around different JPanels. he first is a main menu in which a user chooses an artist, second is a menu in which a user chooses an art work from that artist, and the last is a panel that displays the art work. I have the individual panels set up, but have no idea how to get the buttons working to navigate around. Any help? I used 6 classes: ArtWork is the class that stores the picture, Artist stores artist info, ArtGallery stores the overall gallery, ArtSwing initiates the Swing part of it, SwingTester runs the program, and NewButton is an extension of JButton that contains a button name. Here is the ArtSwing class:

    Java Code:
    import javax.swing.*;
    import java.util.ArrayList;
    import java.util.Scanner;
    import java.awt.Image;
    import java.awt.Toolkit;
    import java.awt.event.*;
    /**
     * Runs the ArtGallery in a swing window. Allows user to access Artists and their
     * ArtWorks, as well as add or remove ArtWorks and Artists
     * @author Niklas
     *
     */
    public class ArtSwing implements ActionListener {
    	private JFrame window = new JFrame("Art Gallery");
    	private ArtGallery myArtGallery = new ArtGallery();
    	private JPanel mainGal;
    	private ArrayList<Artist> artists = myArtGallery.getArtists();
    	private int artistTracker = 0, workTracker = 0;
    	
    	public ArtSwing(String galName){
    		initialize();
    	}
    	private void initialize(){
    		myArtGallery.loadDefaultArtists();
    		window.add(mainMenu());
    		
    		window.setSize(500,200);
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setVisible(true);
    	}
    	
    	private JPanel mainMenu(){
    		mainGal = new JPanel();
    		JLabel label1 = new JLabel("Select an artist to explore: ");
    		JComboBox dropDown = new JComboBox();
    		for(int i = 0; i < artists.size(); i++){
    			dropDown.addItem(artists.get(i));
    		}
    		NewButton selector = new NewButton("Explore");
    		mainGal.add(label1);
    		mainGal.add(dropDown);
    		mainGal.add(selector);
    		artistTracker = dropDown.getSelectedIndex();
    		selector.addActionListener(this);
    		return mainGal;
    	}
    	
    	private JPanel artistMenu(int index){
    		mainGal = new JPanel();
    		JLabel label1 = new JLabel("Select an art work to display: ");
    		NewButton selector = new NewButton("Display");
    		NewButton retToMenu = new NewButton("Back");
    		JComboBox dropDown = new JComboBox();
    		for(int i = 0; i < artists.get(index).getWorksList().size(); i++){
    			dropDown.addItem(artists.get(index).getWorksList().get(i));
    		}
    		mainGal.add(label1);
    		mainGal.add(dropDown);
    		mainGal.add(selector);
    		mainGal.add(retToMenu);
    		selector.addActionListener(this);
    		retToMenu.addActionListener(this);
    		return mainGal;
    	}
    	
    	private JPanel imageDisp(int artistIndex, int workIndex){
    		Toolkit toolkit = Toolkit.getDefaultToolkit();
    		mainGal = new JPanel();
    		Artist current = artists.get(artistIndex);
    		ArtWork displayed = current.getWorksList().get(workIndex);
    		Image image = toolkit.getImage(displayed.getFileName());
    		JLabel pic = new JLabel(new ImageIcon(image));
    		JLabel label1 = new JLabel("" + displayed + "");
    		NewButton retToMenu = new NewButton("Back");
    		retToMenu.addActionListener(this);
    		mainGal.add(label1);
    		mainGal.add(pic);
    		mainGal.add(retToMenu);
    		return mainGal;
    	}
    	
    	public void actionPerformed(ActionEvent a) {
    		if(a.getSource() instanceof NewButton){
    			NewButton b1 = (NewButton)a.getSource();
    			if(b1.getName() == "Explore"){
    				window.remove(mainGal);
    				window.add(artistMenu(artistTracker));
    			}
    			else if(b1.getName() == "Display"){
    				
    			}
    			else if(b1.getName() == "Back"){
    				
    			}
    		}
    	}
    }
    
    /*
     * sources for:
     * Images:
     * [url=http://www.charlesli.org/java/articles/imageio/imageionotes.html]Image IO[/url]
     * [url]http://www.java-forums.org/new-java/4477-how-add-images-jpanels.html[/url]
     * Buttons/Actions:
     * [url=http://forum.codecall.net/java-tutorials/2137-java-tutorial-make-your-button-work.html]Java:Tutorial - Make Your Button Work[/url]
     * [url=http://www.daniweb.com/software-development/java/threads/302545]One action listener for multiple JButtons? - Java[/url]
     */
    Last edited by Fubarable; 05-27-2011 at 01:12 AM. Reason: code tags added

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    have no idea how to get the buttons working
    What do you want the action to be when a button is pressed?
    Java Code:
    b1.getName() == "Display"
    Do not use == to test the contents of a String object. Use the equals method.

    Use code tags when posting code. See this:http://www.java-forums.org/misc.php?do=bbcode#code
    Last edited by Norm; 05-26-2011 at 11:51 PM.

  3. #3
    zzuum is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    right, forgot about .equals. Anyway, I want the button, depending on which one it is, to go to another JPanel. For instance, "Explore" button will go from mainMenu to artistMenu of the selected artist in the drop down menu. Same for the "Display," I want it to go to the work of the selected works in the drop down menu. And I want the "Back" button to return to the main menu.

  4. #4
    zzuum is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    Anyone? Need help within the week

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    Can you make a small program that compiles and executes that demonstrates your problem?

  6. #6
    zzuum is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    Well, the button simply does not respond, and I'm trying to make it; but actually is it possible to simply add an action to the drop down list so that when an item in the list is selected it creates an action based on that selection? It would be much easier than having a button correspond to the list.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    Yes you probably can add a listener that will do something when the use selects an item.

  8. #8
    zzuum is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    Ok, messed around with it, and added actionListeners to each drop down list and button. Right now they do nothing. What I want the drop downs to do is to create and display a new JPanel(is this possible?) within the JFrame for the selected object in the list. Each button is supposed to return to the previous JPanel. Here is my Action listener code:

    Java Code:
    	public void actionPerformed(ActionEvent a) {
    		if(a.getSource() instanceof JComboBox){
    			int index = (((JComboBox) a.getSource()).getSelectedIndex());
    			trackedArtist = artists.get(index);
    			window.add(artistMenu(trackedArtist));
    		}
    		else if(a.getSource() instanceof JComboBox2){
    			int index = (((JComboBox2) a.getSource()).getSelectedIndex());
    			window.add(imageDisp(trackedArtist, index));
    		}
    		else if(a.getSource() instanceof NewButton){
    			NewButton b1 = (NewButton)a.getSource();
    			if(b1.getName().equals("Back to Menu"))
    				window.add(mainMenu());
    			else
    				window.add(artistMenu(trackedArtist));	
    		}
    	}
    The artistMenu(), mainMenu(), and imageDisp() return JPanels. I want them to make new JPanels to display in the current JFrame. I'm sure the problem is in the window.add (window is the JFrame). How can I correct this to make it resposive to action?

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by zzuum View Post
    Need help within the week
    Why? If you are learning on your own then you do not have any imposed deadlines for school assignments.

    Why do so many people try to be deceitful?

  10. #10
    zzuum is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    Actually, deadline is June 10, so not in the week but within the next week. And I am required to learn it on my own as we are not learning it in class but it is a home assignment.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default

    To figure out the techniques, Write a small program that does the changes of panels as you want. Something that you can post here if you have problems.
    When you get the technique down, move it to the larger program.
    Things to consider: layout manager, add and removal, validating container

  12. #12
    zzuum is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    I actually got it yesterday. Found out you have to .remove(Current JPanel) then .add the next one. Found it looking through the API and trying stuff out. Oracle tutorials really help too.

Similar Threads

  1. Replies: 2
    Last Post: 04-26-2011, 02:50 PM
  2. adding multiple jpanels to content pane
    By tooktook22 in forum AWT / Swing
    Replies: 2
    Last Post: 01-19-2011, 06:40 PM
  3. Multiple JPanels within a JFrame
    By horndinkle in forum New To Java
    Replies: 5
    Last Post: 01-09-2011, 07:51 PM
  4. Multiple JPanels Not Working
    By jgezau in forum AWT / Swing
    Replies: 5
    Last Post: 04-17-2009, 04:52 AM
  5. Can't synchronize multiple JPanels in a JFrame
    By vassil_zorev in forum AWT / Swing
    Replies: 0
    Last Post: 12-30-2007, 04:22 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
  •