0

[Thanks for the answers. This comes for you http://www.youtube.com/watch?v=Vo0Cazxj_yc ] This might and should be a very easy question, but i could not find a solution.

I have a java applet, and i want a vertical scrollbar so that i can load thousands of buttons into the applet and use the scrollbar to see buttons down on the appletrightnow it looks like this.

Buttons are used to select items. if button is pressed, the item is selected.

When i load buttons, all of them are shown on one screen, squeezed together to fit the screen in width and height (~1000px,~1000px). Below code is a portion of my program. Please comment.

    JFrame frame = new JFrame();
    NameClassifier nameClassifier = new NameClassifier();
    JScrollPane scrollPane = new JScrollPane(nameClassifier);
     scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
    frame.add(scrollPane);
    frame.getContentPane().add(nameClassifier);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
    System.out.println("exiting");
5
  • 1
    For better help sooner. post an SSCCE. Note that I completed an SSCCE from your snippets in around 35 lines of code. Commented May 2, 2011 at 3:33
  • BTW - why do you make continual references to 'applet' when the code uses a 'frame'? Commented May 2, 2011 at 3:37
  • "thousands of buttons .. used to select items." That sounds like it is better suited to a JList component with multiple selection enabled. Commented May 2, 2011 at 6:04
  • Andrew You are right, I should have added the NameClassifier part. Also, This is the first time I had to use Applets, I do not know the difference between them and frames Commented May 2, 2011 at 8:44
  • 1
    An applet is (usually) embedded in a web page. A Frame or JFrame is free floating on the desktop. I strongly suggest you avoid applets if at all possible. Commented May 2, 2011 at 8:59

3 Answers 3

1
import java.awt.*;
import javax.swing.*;

class ManyButtons {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JFrame frame = new JFrame();
                NameClassifier nameClassifier = new NameClassifier();
                JScrollPane scrollPane = new JScrollPane(nameClassifier);
                scrollPane.setVerticalScrollBarPolicy(
                    ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
                frame.add(scrollPane);
                // nameClassifier has already been added to the scroll pane.
                //frame.getContentPane().add(nameClassifier);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setVisible(true);
                System.out.println("exiting");
            }
        });
    }
}

class NameClassifier extends JPanel {

    NameClassifier() {
        super(new GridLayout(0,10,2,2));
        for (int ii=1; ii<=1000; ii++) {
            add(new JButton("Button " + ii));
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

I think you want to use a Wrap Layout.

Comments

0

Don't add anything directly to the frame, so

frame.add(scrollPane);

is wrong.

Add things to the content pane. probably

scrollPane.add(nameClassifier);
frame.getContentPane().add(scrollPane);

btw, that is one pretty gui design. :)

2 Comments

-1, it is not wrong to add components directly to the frame. This has been supported since JDK5.
MeBigFatGuy, I would love to share this great design with the world, but alas only I will use it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.