4 Replies Latest reply: May 28, 2010 2:19 AM by Darryl Burke RSS

    Error Scrollbar AWT java.

    843807
      The "Scrollbar" not working properly, because the maximum value of the bar can not be accessed from visual part of the component. It can only be edited by their method "setValue ()."
      Could you please check, thanks.
      This is my example:

      import java.awt.BorderLayout;
      import java.awt.Color;
      import java.awt.Frame;
      import java.awt.GridLayout;
      import java.awt.Label;
      import java.awt.Panel;
      import java.awt.Scrollbar;
      import java.awt.event.AdjustmentEvent;
      import java.awt.event.AdjustmentListener;
      import java.awt.event.WindowEvent;
      import java.awt.event.WindowListener;

      /**
      *
      * @author Oscar Uribe Brenes
      */
      public class Ejemplo_011_Scrollbar {

      static int r=0,g=0,b=0;
      /**
      * @param args the command line arguments
      */
      public static void main(String[] args) {

      Frame frame = new Frame();

      Panel panel = new Panel();

      final Label label = new Label();
      final Label label1 = new Label();
      final Label label2 = new Label();

      final Label label3 = new Label();

      Scrollbar scrollbar = new Scrollbar();
      Scrollbar scrollbar1 = new Scrollbar();
      Scrollbar scrollbar2 = new Scrollbar();

      BorderLayout borderLayout = new BorderLayout();
      GridLayout gridLayout = new GridLayout();

      frame.setTitle("Ejemplo_011_Scrollbar");
      frame.setSize(300, 100);

      label.setText("Rojo: 0");
      label.setAlignment(Label.RIGHT);
      label.setSize(50, label.getHeight());

      label1.setText("Verde: 0");
      label1.setAlignment(Label.RIGHT);
      label1.setSize(50, label.getHeight());

      label2.setText("Azul: 0");
      label2.setAlignment(Label.RIGHT);
      label2.setSize(50, label.getHeight());

      label3.setText("");

      scrollbar.setMinimum(0);
      scrollbar.setMaximum(255);
      scrollbar.setValue(0);
      scrollbar.setName("scrollbar");
      scrollbar.setOrientation(Scrollbar.HORIZONTAL);

      scrollbar1.setMinimum(0);
      scrollbar1.setMaximum(255);
      scrollbar1.setValue(0);
      scrollbar1.setName("scrollbar1");
      scrollbar1.setOrientation(Scrollbar.HORIZONTAL);

      scrollbar2.setMinimum(0);
      scrollbar2.setMaximum(255);
      scrollbar2.setValue(0);
      scrollbar2.setName("scrollbar2");
      scrollbar2.setOrientation(Scrollbar.HORIZONTAL);

      gridLayout.setColumns(2);
      gridLayout.setRows(3);
      gridLayout.setHgap(1);
      gridLayout.setVgap(2);

      frame.setLayout(borderLayout);
      panel.setLayout(gridLayout);

      WindowListener windowListener = new WindowListener() {

      @Override
      public void windowOpened(WindowEvent e) {
      }

      @Override
      public void windowClosing(WindowEvent e) {

      Frame frame;

      if (e.getSource() instanceof Frame) {

      frame = (Frame) e.getSource();
      frame.dispose();

      }

      }

      @Override
      public void windowClosed(WindowEvent e) {
      System.exit(0);
      }

      @Override
      public void windowIconified(WindowEvent e) {
      }

      @Override
      public void windowDeiconified(WindowEvent e) {
      }

      @Override
      public void windowActivated(WindowEvent e) {
      }

      @Override
      public void windowDeactivated(WindowEvent e) {
      }
      };
      frame.addWindowListener(windowListener);

      AdjustmentListener adjustmentListener = new AdjustmentListener() {

      @Override
      public void adjustmentValueChanged(AdjustmentEvent e) {

      Scrollbar scrollbar;

      Color color = null;

      if(e.getSource()instanceof Scrollbar){
      scrollbar = (Scrollbar) e.getSource();
      if(scrollbar.getName().equals("scrollbar")){
      r = scrollbar.getValue();
      label.setText("Rojo: " + r);
      }
      if (scrollbar.getName().equals("scrollbar1")) {
      g = scrollbar.getValue();
      label1.setText("Verde: " + g);
      }
      if (scrollbar.getName().equals("scrollbar2")) {
      b = scrollbar.getValue();
      label2.setText("Azul: " + b);
      }
      color = new Color(r, g, b);
      }
      label3.setBackground(color);
      }
      };

      scrollbar.addAdjustmentListener(adjustmentListener);
      scrollbar1.addAdjustmentListener(adjustmentListener);
      scrollbar2.addAdjustmentListener(adjustmentListener);

      panel.add(label);
      panel.add(scrollbar);
      panel.add(label1);
      panel.add(scrollbar1);
      panel.add(label2);
      panel.add(scrollbar2);

      frame.add(panel, BorderLayout.CENTER);
      frame.add(label3, BorderLayout.SOUTH);

      frame.setLocation(200, 200);
      frame.setResizable(false);
      frame.setVisible(true);

      }

      }
        • 1. Re: Error Scrollbar AWT java.
          Darryl Burke
          Use code tags to post codes -- [code]CODE[/code] will display as
          CODE
          Or click the CODE button and paste your code between the {code} tags that appear.

          db
          • 2. Re: Error Scrollbar AWT java.
            843807
            Sorry, I could not find the edit button, so I post it again, with the format you said.
            import java.awt.BorderLayout;
            import java.awt.Color;
            import java.awt.Frame;
            import java.awt.GridLayout;
            import java.awt.Label;
            import java.awt.Panel;
            import java.awt.Scrollbar;
            import java.awt.event.AdjustmentEvent;
            import java.awt.event.AdjustmentListener;
            import java.awt.event.WindowEvent;
            import java.awt.event.WindowListener;
            
            public class Ejemplo_011_Scrollbar {
            
                static int r = 0, g = 0, b = 0;
            
                public static void main(String[] args) {
            
                    Frame frame = new Frame();
            
                    Panel panel = new Panel();
            
                    final Label label = new Label();
                    final Label label1 = new Label();
                    final Label label2 = new Label();
            
                    final Label label3 = new Label();
            
                    Scrollbar scrollbar = new Scrollbar();
                    Scrollbar scrollbar1 = new Scrollbar();
                    Scrollbar scrollbar2 = new Scrollbar();
            
                    BorderLayout borderLayout = new BorderLayout();
                    GridLayout gridLayout = new GridLayout();
            
                    frame.setTitle("Ejemplo_011_Scrollbar");
                    frame.setSize(300, 100);
            
                    label.setText("Rojo: 0");
                    label.setAlignment(Label.RIGHT);
                    label.setSize(50, label.getHeight());
            
                    label1.setText("Verde: 0");
                    label1.setAlignment(Label.RIGHT);
                    label1.setSize(50, label.getHeight());
            
                    label2.setText("Azul: 0");
                    label2.setAlignment(Label.RIGHT);
                    label2.setSize(50, label.getHeight());
            
                    label3.setText("");
            
                    scrollbar.setMinimum(0);
                    scrollbar.setMaximum(255);
                    scrollbar.setValue(0);
                    scrollbar.setName("scrollbar");
                    scrollbar.setOrientation(Scrollbar.HORIZONTAL);
            
                    scrollbar1.setMinimum(0);
                    scrollbar1.setMaximum(255);
                    scrollbar1.setValue(0);
                    scrollbar1.setName("scrollbar1");
                    scrollbar1.setOrientation(Scrollbar.HORIZONTAL);
            
                    scrollbar2.setMinimum(0);
                    scrollbar2.setMaximum(255);
                    scrollbar2.setValue(0);
                    scrollbar2.setName("scrollbar2");
                    scrollbar2.setOrientation(Scrollbar.HORIZONTAL);
            
                    gridLayout.setColumns(2);
                    gridLayout.setRows(3);
                    gridLayout.setHgap(1);
                    gridLayout.setVgap(2);
            
                    frame.setLayout(borderLayout);
                    panel.setLayout(gridLayout);
            
                    WindowListener windowListener = new WindowListener() {
            
                        @Override
                        public void windowOpened(WindowEvent e) {
                        }
            
                        @Override
                        public void windowClosing(WindowEvent e) {
            
                            Frame frame;
            
                            if (e.getSource() instanceof Frame) {
            
                                frame = (Frame) e.getSource();
                                frame.dispose();
            
                            }
            
                        }
            
                        @Override
                        public void windowClosed(WindowEvent e) {
                            System.exit(0);
                        }
            
                        @Override
                        public void windowIconified(WindowEvent e) {
                        }
            
                        @Override
                        public void windowDeiconified(WindowEvent e) {
                        }
            
                        @Override
                        public void windowActivated(WindowEvent e) {
                        }
            
                        @Override
                        public void windowDeactivated(WindowEvent e) {
                        }
                    };
                    frame.addWindowListener(windowListener);
            
                    AdjustmentListener adjustmentListener = new AdjustmentListener() {
            
                        @Override
                        public void adjustmentValueChanged(AdjustmentEvent e) {
            
                            Scrollbar scrollbar;
            
                            Color color = null;
            
                            if (e.getSource() instanceof Scrollbar) {
                                scrollbar = (Scrollbar) e.getSource();
                                if (scrollbar.getName().equals("scrollbar")) {
                                    r = scrollbar.getValue();
                                    label.setText("Rojo: " + r);
                                }
                                if (scrollbar.getName().equals("scrollbar1")) {
                                    g = scrollbar.getValue();
                                    label1.setText("Verde: " + g);
                                }
                                if (scrollbar.getName().equals("scrollbar2")) {
                                    b = scrollbar.getValue();
                                    label2.setText("Azul: " + b);
                                }
                                color = new Color(r, g, b);
                            }
                            label3.setBackground(color);
                        }
                    };
            
                    scrollbar.addAdjustmentListener(adjustmentListener);
                    scrollbar1.addAdjustmentListener(adjustmentListener);
                    scrollbar2.addAdjustmentListener(adjustmentListener);
            
                    panel.add(label);
                    panel.add(scrollbar);
                    panel.add(label1);
                    panel.add(scrollbar1);
                    panel.add(label2);
                    panel.add(scrollbar2);
            
                    frame.add(panel, BorderLayout.CENTER);
                    frame.add(label3, BorderLayout.SOUTH);
            
                    frame.setLocation(200, 200);
                    frame.setResizable(false);
                    frame.setVisible(true);
            
                }
            }
            Edited by: screeen on May 28, 2010 1:45 AM
            • 3. Re: Error Scrollbar AWT java.
              843807
              Although I set the ceiling in the constructor of the "Scrollbar", when the program is executed and I try to set the limit through the bar, visually, it is not possible to reach the ceiling was set in the constructor. For example, the fixed ceiling is 300, visually, just come to 299.
              new Scrollbar(Scrollbar.HORIZONTAL, 0, 0, 0, 300);
              There is a way in which I can set the maximum limit, and that the visual component can reach this limit correctly?
              Thanks.

              Edited by: screeen on May 27, 2010 11:59 PM
              • 4. Re: Error Scrollbar AWT java.
                Darryl Burke
                Please edit your post #2 (click the pencil icon to the right of the post header) and paste valid, correctly indented code.

                db