Trying to add scrollbar to gui
Code :
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/v2...er567/good.png
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. :)!
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);
Re: Trying to add scrollbar to gui
Quote:
Originally Posted by
javapenguin
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);
http://img.photobucket.com/albums/v2...er567/yeah.png
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 :)!