Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

Swing Question

  Asked By: Clifford    Date: Jun 14    Category: Java    Views: 655

I want to add rows of images, 5 per row. The code below sort of
works, but it resizes past the edge of the screen and the scroll bar
does not scroll the rows and the exit button that should show at the
bottom of the screen is not visible.

What do I need to do to fix this so that I can add as many rows of
images I want to and be able to scroll them all and also see the exit

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;
* Opens a window showing the used cards ordered by suit & value
*@author john sayre
*@created March 21, 2003
class doUsedGui extends JFrame {

Vector Used;
//Contains all of the hands
Vector Rows = new Vector();
//will contain panels of 5 cards each

//will contain the card images

JPanel p1;
JPanel p;
//panel of 5 images
GridLayout g;
//for the panel to set up the # of rows it
//needs based on 5 cards per row
JPanel bp = new JPanel(new GridLayout(1, 1, 4, 4));

//Exit button goes here

* Constructor for the doUsedGui object
*@param pUsed Cards already used, sorted by suit then value
doUsedGui(Vector pUsed) {
Used = pUsed;

* Plugs card image paths into vector of panels
public void getRows() {
setTitle("Used Cards");
setSize(400, 500);
int rows = ((int) Math.ceil(Used.size() / 5));
//as many rows as needed

int image = 0;
//get each image from the vector
System.out.println("Used size " + Used.size());
for (int panels = 0; panels < rows; panels++) {
//add panels to row vector
p = new JPanel(new GridLayout(1, 5, 4, 8));

for (int iconNo = 0; iconNo < 5; iconNo++) {
//5 icons per row
System.out.println("Row " + panels
+ " column " + iconNo);
if (image < Used.size()) {
//images < total in used card
Card card = new Card();
card = (Card) Used.get
p.add(new JLabel(new ImageIcon

"C:\\java\\dev\\pokerg\\Cards\\" + card.cName +

//add panel to vector

//add the panels to the frame

* creates new frame with panels of cards
private void doFrame() {

JButton exitButton = new JButton(" Exit ");
JPanel p2 = new JPanel();
p2.setLayout(new BoxLayout(p2, BoxLayout.Y_AXIS));

p2.setMaximumSize(new Dimension(200,200));
p1 = (JPanel) getContentPane();
p1.setLayout(new BorderLayout());

//p1 = new JPanel(new BoxLayout(p1,
new ActionListener() {
public void actionPerformed
(ActionEvent e) {
System.out.println("Rows size " + Rows.size());
for (int i = 0; i < Rows.size(); i++) {
p2.add((JPanel) Rows.get(i));
Row " + i);
JScrollBar jsc = new JScrollBar();
p1.add(p2, BorderLayout.CENTER);
p1.add(jsc, BorderLayout.EAST);
p1.add(bp, BorderLayout.SOUTH);





1 Answer Found

Answer #1    Answered By: Milind Mishra     Answered On: Jun 14

Just thinking out loud... you probably could use a
GridLayout within a ScrollPane ?

Didn't find what you were looking for? Find more on Swing Question Or get search suggestion and latest updates.