PDA

View Full Version : Placing JButtons in a JPanel in a JScrollPane i.e. Scrollable buttons


BigV
02-26-04, 12:15 PM
Hi All

I have managed to create a scrollable list of buttons by placing the buttons in a JPanel and place the JPanel in a JScrollWindow.
I then have a GridBagLayout with the ScrollWindow at the top and Various Buttons underneath. Unfortunatley, as I insert more buttons in the JPanel, it expands over the top of the buttons at the bottom of the GridBagLayout pane.

Here is the code......

public class events extends Applet implements ActionListener, ItemListener
{
Button changeBtn, changeBtn1, changeBtn2, clearBtn; //Change and clear buttons
Label titleLabel, status, instructionLabel, resultLabel; //Label variables
TextArea originalTa, resultTa; //TextArea variables
Checkbox italicCb, boldCb; //Italic and bold checkboxes
ScrollPane arse;

void buildConstraints (GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) {
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}

public void init()
{
//Assign RGB colors to the foreground and background
Color color = new Color(150, 150, 150);
Color fColor = new Color(0, 0, 0);
Color red = new Color(255, 0, 0);
Color green = new Color(0, 255, 0);
setBackground(color);
setForeground(fColor);

titleLabel = new Label("Java Event List", Label.CENTER);
//titleLabel.setFont(new Font("Arial", Font.BOLD, 20));

status = new Label("Set the Severiity here", Label.CENTER);
//status.setFont(new Font("Monospaced", Font.PLAIN, 11));

instructionLabel = new Label(" Please enter your text message:", Label.LEFT);
//instructionLabel.setFont(new Font("Serif", Font.BOLD, 15));

resultLabel = new Label(" Here is your new font version:", Label.LEFT);
//resultLabel.setFont(new Font("Serif", Font.BOLD, 15));

//Create No Change and Clear All buttons
changeBtn = new Button("Button 0");
changeBtn1 = new Button("Button 1");
changeBtn2 = new Button("Button 2");
clearBtn = new Button("Clear All");

//Create Italic and Bold check boxes
italicCb = new Checkbox("Italic");
boldCb = new Checkbox("Bold");

setLayout(new BorderLayout());


//Create Panel top to hold the title, instruction, and editable text area
JPanel top = new JPanel();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
top.setLayout(gridbag);

Panel insideScroll = new Panel();
BoxLayout box = new BoxLayout(insideScroll, BoxLayout.PAGE_AXIS);
insideScroll.setLayout(box);
for(int i=0; i<20; i++)
{
//Label myLabel = new Label("arse");
insideScroll.add(new Button("This a very long button "));

}

buildConstraints(constraints, 0, 0, 3, 1, 0, 0);
JScrollPane scrollWindow = new JScrollPane(insideScroll);
Dimension pref = new Dimension(850,350);
scrollWindow.setPreferredSize(pref);
gridbag.setConstraints(scrollWindow,constraints);
top.add(scrollWindow, constraints);

constraints.fill = GridBagConstraints.NONE;
buildConstraints(constraints, 0, 1, 3, 1, 100, 100);
JLabel label1 = new JLabel(" ");
top.add(label1, constraints);

gridbag.setConstraints(label1,constraints);
buildConstraints(constraints, 0, 2, 3, 1, 100, 100);
JLabel label2 = new JLabel("Please select the events you would like to see");
gridbag.setConstraints(label2,constraints);
top.add(label2, constraints);

//Spacer
buildConstraints(constraints, 0, 3, 3, 1, 100, 100);
JLabel label3 = new JLabel(" ");
gridbag.setConstraints(label3,constraints);
top.add(label3, constraints);

buildConstraints(constraints, 0, 4, 1, 1, 100, 100);
JButton button1 = new JButton("Critical Events");
gridbag.setConstraints(button1,constraints);
top.add(button1, constraints);

buildConstraints(constraints, 1, 4, 1, 1, 100, 100);
JButton button2 = new JButton("Clear Events");
gridbag.setConstraints(button2,constraints);
top.add(button2, constraints);

buildConstraints(constraints, 2, 4, 1, 1, 100, 100);
JButton button3 = new JButton("All Events");
gridbag.setConstraints(button3,constraints);
top.add(button3, constraints);


/*Panel insideScroll = new Panel();
BoxLayout box = new BoxLayout(insideScroll, BoxLayout.PAGE_AXIS);
insideScroll.setLayout(box);
scrollWindow.add(insideScroll);*/


add(top, "Center"); //Add Panel top to the BorderLayout
scrollWindow.requestFocus(); //For the focus in the original text area

setVisible(true);
} //End of init


Any ideas where I'm going wrong!?

bigV

Rob_Darkins
06-14-04, 03:05 PM
Hmm.. I hate constraints. lol..
here goes an idea: (but I haven't tested it or anything, nor have I tested ur code, so.. :o| )
Perhaps.. for the last group of buttons, instead of having the x and y weights as 100, I think u need to set them to be proportional, so that, for example, set the x weight for the first button as 33. Then set the y weight for the first button as... well, u choose, but not 100..
then for all the buttons that follow, set there weights as 0.
Also,change the weights of the other components to something UNDER 100.. Actually, here's the "secret" to constraints:
Imagine that the "x weights" added up together must make 100 (so, like percentages.) So, for example, if you wanted to one of the buttons to take up 50% of the width of the JFrame, then you would set it's "x weight" to 50. Then all of the other components "x weights" on that same line must ADD UP TO 50 (ofcourse, to make 100).
It's same vertically. So, play around with them, also: if u set one of the weights to 0.. it doesn't mean set it's weight as 0 (coz you can't do that), instead: by setting it as "0", it sets itself to be the same "width or height" (dependant) as the previously initialized component.
So... I hope that makes sense. :D

Good Luck!

Rob Darkins.

PS: Am I correct to say that "your" code isn't completely yours? (Instead, you've simply modified and added some components to a previous script?) :P

Hi All

I have managed to create a scrollable list of buttons by placing the buttons in a JPanel and place the JPanel in a JScrollWindow.
I then have a GridBagLayout with the ScrollWindow at the top and Various Buttons underneath. Unfortunatley, as I insert more buttons in the JPanel, it expands over the top of the buttons at the bottom of the GridBagLayout pane.

Here is the code......

public class events extends Applet implements ActionListener, ItemListener
{
Button changeBtn, changeBtn1, changeBtn2, clearBtn; //Change and clear buttons
Label titleLabel, status, instructionLabel, resultLabel; //Label variables
TextArea originalTa, resultTa; //TextArea variables
Checkbox italicCb, boldCb; //Italic and bold checkboxes
ScrollPane arse;

void buildConstraints (GridBagConstraints gbc, int gx, int gy, int gw, int gh, int wx, int wy) {
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}

public void init()
{
//Assign RGB colors to the foreground and background
Color color = new Color(150, 150, 150);
Color fColor = new Color(0, 0, 0);
Color red = new Color(255, 0, 0);
Color green = new Color(0, 255, 0);
setBackground(color);
setForeground(fColor);

titleLabel = new Label("Java Event List", Label.CENTER);
//titleLabel.setFont(new Font("Arial", Font.BOLD, 20));

status = new Label("Set the Severiity here", Label.CENTER);
//status.setFont(new Font("Monospaced", Font.PLAIN, 11));

instructionLabel = new Label(" Please enter your text message:", Label.LEFT);
//instructionLabel.setFont(new Font("Serif", Font.BOLD, 15));

resultLabel = new Label(" Here is your new font version:", Label.LEFT);
//resultLabel.setFont(new Font("Serif", Font.BOLD, 15));

//Create No Change and Clear All buttons
changeBtn = new Button("Button 0");
changeBtn1 = new Button("Button 1");
changeBtn2 = new Button("Button 2");
clearBtn = new Button("Clear All");

//Create Italic and Bold check boxes
italicCb = new Checkbox("Italic");
boldCb = new Checkbox("Bold");

setLayout(new BorderLayout());


//Create Panel top to hold the title, instruction, and editable text area
JPanel top = new JPanel();
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints constraints = new GridBagConstraints();
top.setLayout(gridbag);

Panel insideScroll = new Panel();
BoxLayout box = new BoxLayout(insideScroll, BoxLayout.PAGE_AXIS);
insideScroll.setLayout(box);
for(int i=0; i<20; i++)
{
//Label myLabel = new Label("arse");
insideScroll.add(new Button("This a very long button "));

}

buildConstraints(constraints, 0, 0, 3, 1, 0, 0);
JScrollPane scrollWindow = new JScrollPane(insideScroll);
Dimension pref = new Dimension(850,350);
scrollWindow.setPreferredSize(pref);
gridbag.setConstraints(scrollWindow,constraints);
top.add(scrollWindow, constraints);

constraints.fill = GridBagConstraints.NONE;
buildConstraints(constraints, 0, 1, 3, 1, 100, 100);
JLabel label1 = new JLabel(" ");
top.add(label1, constraints);

gridbag.setConstraints(label1,constraints);
buildConstraints(constraints, 0, 2, 3, 1, 100, 100);
JLabel label2 = new JLabel("Please select the events you would like to see");
gridbag.setConstraints(label2,constraints);
top.add(label2, constraints);

//Spacer
buildConstraints(constraints, 0, 3, 3, 1, 100, 100);
JLabel label3 = new JLabel(" ");
gridbag.setConstraints(label3,constraints);
top.add(label3, constraints);

buildConstraints(constraints, 0, 4, 1, 1, 100, 100);
JButton button1 = new JButton("Critical Events");
gridbag.setConstraints(button1,constraints);
top.add(button1, constraints);

buildConstraints(constraints, 1, 4, 1, 1, 100, 100);
JButton button2 = new JButton("Clear Events");
gridbag.setConstraints(button2,constraints);
top.add(button2, constraints);

buildConstraints(constraints, 2, 4, 1, 1, 100, 100);
JButton button3 = new JButton("All Events");
gridbag.setConstraints(button3,constraints);
top.add(button3, constraints);


/*Panel insideScroll = new Panel();
BoxLayout box = new BoxLayout(insideScroll, BoxLayout.PAGE_AXIS);
insideScroll.setLayout(box);
scrollWindow.add(insideScroll);*/


add(top, "Center"); //Add Panel top to the BorderLayout
scrollWindow.requestFocus(); //For the focus in the original text area

setVisible(true);
} //End of init


Any ideas where I'm going wrong!?

bigV

BigV
05-26-05, 12:29 PM
This all my code. I have just been through a weeks course in Java and I am now trying stuff out.

Thank You
;-)

bigV