Java Programming Home Page: Archive: Message #73

Date: Apr 20 2000 13:34:03 EDT
From: "Java Programming" <javaProgramming-owner@listbot.com>
Subject: GOSLAB

Hi All,
Here is how you do a grid of scrollbars,labels and buttons.
I call this, GOSLAB!
package graphics;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class GOSLAB
        extends Frame
        implements ActionListener {
        String labels[]={ "red","green","blue"};
        LabelControlPanel  lcp=new LabelControlPanel(labels);
        ScrollbarPanel     sbp = new ScrollbarPanel(labels.length);
        Panel panels[] ={sbp,lcp};
      
        ButtonControlPanel bcp = new ButtonControlPanel(this);
 public GOSLAB() {
  
        
	GridBagLayout ma=new GridBagLayout();
	GridBagConstraints cp=new GridBagConstraints();
	cp.gridx=0;
	cp.gridy=0;
	cp.fill=GridBagConstraints.HORIZONTAL;
	cp.weightx=200;
	cp.weighty=200;
	setLayout(ma);
	ma.setConstraints(sbp,cp);
	
	add(sbp);
	cp.gridx=GridBagConstraints.RELATIVE;
	cp.gridwidth=GridBagConstraints.REMAINDER;
        add(lcp);
	
	cp.gridy=GridBagConstraints.BOTH;
	cp.weightx=100;
	
	ma.setConstraints(bcp,cp);
       add(bcp);
        setSize(200,200);
        pack();
        setVisible(true);

  }
  public static void main(
        String args[]) {
        new GOSLAB();
  }
  public void actionPerformed( ActionEvent e) {
    Object o=e.getSource();
    if (o == bcp.cancelButton) {
        setVisible(false);
        return;
    }
    if (o==bcp.okButton) {
        sbp.printValues();
        return;
    }
  }
}

 class LabelControlPanel
        extends Panel {
   Label lba[];
   LabelControlPanel(
        String labels[]) {
        setLayout(new GridLayout(0,1));
        lba =
                new Label[labels.length];
         for (int i=0;
                i < labels.length; i++) {
          lba[i] = new Label(
                labels[i]);
          add(lba[i]);
         }
   }
}
class ScrollbarPanel
        extends Panel {
 Scrollbar sba[];
 public int[] getValues(){
        int values[] = new int[sba.length];
        for (int i=0;
                i<sba.length;
                i++)
                values[i] =
                sba[i].getValue();

  return values;
 }
 public void printValues() {
        int values[] =getValues();
        for (int i=0; i < values.length; i++)
        System.out.println(values[i]);
 }
 ScrollbarPanel(int N) {
   sba =
        new Scrollbar[N];
	GridBagLayout sbp=new GridBagLayout();
	GridBagConstraints sb=new GridBagConstraints();
	
	setLayout(sbp);
	sb.gridx=0;
	sb.fill=GridBagConstraints.HORIZONTAL;
	sb.weightx=200;
	sb.weighty=200;
	
	int j=20;
           for (int i=0; i < sba.length;i++) {
        sba[i] =
                new Scrollbar(
                  Scrollbar.HORIZONTAL);
        sba[i].setValue(50);
	sbp.setConstraints(sba[i],sb);
        add(sba[i]);
	sb.gridy=GridBagConstraints.RELATIVE;
	sb.fill=GridBagConstraints.HORIZONTAL;
	sb.weightx=100;
	sb.weighty=100;
	
   }
 }
}
 class ButtonControlPanel extends
        Panel {
Button okButton= new Button("ok");
Button cancelButton= new Button("cancel");
ButtonControlPanel(
        ActionListener al) {
        setLayout(new FlowLayout());
        add(okButton);
        add(cancelButton);
        okButton.addActionListener(al);
        cancelButton.addActionListener(al);
  }
}