Results 1 to 8 of 8
  1. #1
    it2512 is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default adjusting bar in java (GUI)

    hei, good day everyone.

    I want to draw an GUI that can let the user input the value by adjusting the bar. The bar is similiar to normal volume bar (adjusting the value by draging the pointer to the specific scale.), anyone know the name of the GUI component ? Somemore what is the name of the Listener that should be used to register with the component?

    Thanks in advance.

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Sounds like you want a JSlider. Read the section from the Swing tutorial on How to Use Sliders for an example.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    what is the name of the Listener that should be used to register with the component
    Different components have different listeners. Read the API doc for the component to see which one to use.

  4. #4
    it2512 is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    To camickr: thanks, JSlider is the component that i want.

    Now, i know how to use JSlider already. But one things make me so curious:

    Java Code:
    slider.addChangeListener(new ChangeListener() {
      public void stateChanged(ChangeEvent e) {
         JSlider source = (JSlider)e.getSource();  // get the slider
         if (!source.getValueIsAdjusting()) {
            val = source.getValue();  // get slider value
            
         }
      }
    });

    The code above, there is one statement like:
    Java Code:
     JSlider source = (JSlider)e.getSource();  // get the slider

    Why we need to cast it to JSlider? ( I have compiled it, without it i get error)
    But when i do the checking i get no error, like

    Java Code:
     if( e.getSource == slider)

    hmmmm, so strange...
    Last edited by it2512; 06-27-2010 at 03:17 PM.

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,458
    Rep Power
    25

    Default

    Why we need to cast it to JSlider
    You don't have to cast it for: e.getSource == slider
    because that statement is comparing if two object references are the same.
    You do have to cast it for:
    JSlider source = (JSlider)e.getSource(); // get the slider
    if (!source.getValueIsAdjusting()) {
    val = source.getValue(); // get slider value
    Because source needs to be a reference to a class that has a getValue() method.

  6. #6
    it2512 is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    Sorry previous post i forgot declare:


    Java Code:
     JSlider slider = new JSlider();
    So, slider should be JSlider type. Since we can compare them e.getSource == slider, this should mean they are same type?

    Once after i wrote the statement:
    Java Code:
     JSlider source = e.getSource();
    , and i compile i got the error. Error happen before i call the getValue() function.

    Is e.getSouce() don't return JSlider type object? hmmm...... it should return it.........

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    per the API (which you should probably read), getSource returns an object of Object type which you will need to cast to a JSlider -- if you know for certain that it is in fact a JSlider object.
    Last edited by Fubarable; 06-28-2010 at 03:21 AM.

  8. #8
    it2512 is offline Member
    Join Date
    Jun 2010
    Posts
    4
    Rep Power
    0

    Default

    how can i change this thread to "solved". The title cannot be modified anymore.

Similar Threads

  1. Adjusting function
    By rootpi in forum New To Java
    Replies: 1
    Last Post: 02-02-2010, 12:40 PM
  2. Replies: 4
    Last Post: 01-21-2010, 05:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •