Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Trying to add scrollbar to gui

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Trying to add scrollbar to gui

    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class Gui extends JFrame {
     
    	private static int WINDOW_WIDTH = 1024;
    	private static int WINDOW_HEIGHT = 768;
     
    	final int NUMBER_OF_GAMES = 13;
     
    	private String[] gameTitles = {
    			"Assassin's Creed II ", "Batman: Arkham Asylum ", "Cloudy With a Chance of Meatballs",
    			"Demon Soul's", "EyePet", "Final Fantasy XII", "God Of War III", "Heavy Rain", "Infamous II",
    			"James Bond: BloodStone", "Killzone 2", "Uncharted", "Warhawk",  };
    	private JList jlst = new JList(gameTitles); 
     
    	private ImageIcon[] gamebox = {
    			new ImageIcon("C:/gamebox/assassinscreedII.jpg"),
    			new ImageIcon("C:/gamebox/arkhamasylum.jpg"),
    			new ImageIcon("C:/gamebox/cloudy.jpg"),
    			new ImageIcon("C:/gamebox/demonsouls.jpg"),
    			new ImageIcon("C:/gamebox/eyepet.jpg"),
    			new ImageIcon("C:/gamebox/ff12.jpg"),
    			new ImageIcon("C:/gamebox/gow3.jpg"),
    			new ImageIcon("C:/gamebox/heavyrain.jpg"),
    			new ImageIcon("C:/gamebox/infamous2.jpg"),
    			new ImageIcon("C:/gamebox/bloodstone.jpg"),
    			new ImageIcon("C:/gamebox/killzone2.jpg"),
    			new ImageIcon("C:/gamebox/uncharted.jpg"),
    			new ImageIcon("C:/gamebox/warhawk.jpg"),
    	};
     
    	private JLabel[] jlblImageViewer = new JLabel[NUMBER_OF_GAMES];
     
    	public static void main(String [] args){
    		Gui frame = new Gui();
    		Scrollbar one = new Scrollbar();
    		frame.setTitle("Playstation 3");
    		frame.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
    		frame.setLocationRelativeTo(null);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
     
    	public Gui() {
     
    		JPanel p = new JPanel(new GridLayout(1,0));
    		for (int i=0; i < NUMBER_OF_GAMES-9; i++){
    			p.add(jlblImageViewer[i] = new JLabel());
    			jlblImageViewer[i].setHorizontalAlignment
    				(SwingConstants.CENTER);
    		}
     
    		add(p, BorderLayout.CENTER);
    		add(new JScrollPane(jlst), BorderLayout.WEST);
     
    		jlst.addListSelectionListener(new ListSelectionListener(){
    			public void valueChanged(ListSelectionEvent e){
    				int[] indices = jlst.getSelectedIndices();
    				int i;
     
     
    				for(i = 0; i< indices.length; i++){
    					jlblImageViewer[i].setIcon(gamebox[indices[i]]);
    				}					
     
    				for (; i<NUMBER_OF_GAMES-9; i++){
    					jlblImageViewer[i].setIcon(null);
    				}
    			}
    		});		
    	}// end constructor
     
     
    }


    http://img.photobucket.com/albums/v257/gamer567/bad.png




    Currently I have made a program that lists a bunch of games and when you click on the game, you see the box art. The imageviewer is making a jlabel for every single game and attempting to have the gridlayout fit them all accordingly to the amount of games. However it is being restricted to the preset size of the screen and is shrinking the images. This is shown in the bad image.

    What I want it to do is expand horizontally as far as it needs to in order to fit all the pictures. For instance in the good picture, I limited the amount of jlabels to form in the gridlayout to 4 which allows me to see all the pictures (but 4 max)

    Any way to add a scrollbar to make this possible to see all 13 games ? Or say 1000+ if I am insane and add them all.

    If I am asking for a huge rewrite of my code, a simple point in the right direction would help a lot =D!

    Ultimately I want to make the gridlayout have two horizontal rows so you see 8 games before scrolling (I could limit games to 8, change gridlayout to (2,0) to make it happen without scroll bar now ) and then add a scrollbar...but I can ignore that for now until I get the scrollbar. !
    Last edited by illidari; July 13th, 2011 at 09:52 PM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to add scrollbar to gui

    Add a JScrollPane to the screen itself.

    add(new JScrollPane(p, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));

    Actually, it might work better if you add the JScrollPane, with the JPanel inside of it, instead of adding the panel itself.

    Something like:

    add(new JScrollPane(p, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.WEST);
    Last edited by javapenguin; July 13th, 2011 at 10:35 PM.

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to add scrollbar to gui

    Quote Originally Posted by javapenguin View Post
    Add a JScrollPane to the screen itself.

    add(new JScrollPane(p, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED));

    Actually, it might work better if you add the JScrollPane, with the JPanel inside of it, instead of adding the panel itself.

    Something like:

    add(new JScrollPane(p, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.WEST);



    You sir are amazing. I made the pictures clickable to expand into more windows !

    The code worked , just needed to make it be in the center, not the west !

Similar Threads

  1. Need help on print , scrollbar for my applet
    By Anandprasad in forum Member Introductions
    Replies: 2
    Last Post: May 12th, 2011, 10:00 AM
  2. How to add scrollbar to a JPanel with Graphics
    By Tanguo in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2011, 09:53 PM
  3. problem in getting scrollbar in Jlist
    By sandeepben in forum AWT / Java Swing
    Replies: 1
    Last Post: March 27th, 2010, 04:51 AM