Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Jslider Help (http://www.programmingforums.org/showthread.php?t=1137)

T3FLoN Nov 14th, 2004 12:39 PM

Hi guyz im relatively new to the java seen but i kno the basics...well im making my self a telephone interface got my keypad and display field to interact with eachother.
Also ive implemented a JSlider into the mix.

Well my problem is that i cant get my slider to adjust my JLabel witch is called ("Volume:"+changeVar) changeVar ranges from 0 to 100 and changes in realtime as the slider is moved. Thats my goal.

Ive looked on many sites but most of them have a bunch of unneeded code and use a textField and not a label.

Was hopeing to find a simplified example to use on my program.
Any input is appreciated. TX in advance.
:banana:

T3FLoN Nov 14th, 2004 1:12 PM

just a little update on my progress.
i got my change listener working i added a System println to see if the value is changing, and yes it is but....

for some reason its not changing the value in my label.
Heres my code to maybe help you out on a suggestion.
:

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

class Telephone extends JFrame
        implements ActionListener{
              //ChangeListener{

        private JTextField numBox;// keypad's text field
        private JSlider volume;// slider
        private JLabel low;//belongs to volume slider
        private JLabel high;//belongs to volume slider
        private JLabel volLabel;//belongs to volme slider
        private JTextField volField;//slider's text field

//variable to be stored for JSlider number position
        private static int value;

//button declerations
        private JButton b1;
        private JButton b2;
        private JButton b3;
        private JButton b4;
        private JButton b5;
        private JButton b6;
        private JButton b7;
        private JButton b8;
        private JButton b9;
        private JButton b10;
        private JButton b11;
        private JButton b12;
        private JButton bclr;

//fonts to be used
        Font buttonFont = new Font("Arial",Font.BOLD,32);
        Font textFieldFont = new Font("Arial",Font.BOLD,20);

        private Container c;//declaring container

        public static void main(String args[]){
 Telephone myPhone = new Telephone();
        }

        public Telephone(){

        //my JFrame's properties
 this.setBounds(100,80,630,350);
 this.setResizable(false);
 this.setTitle("Telephone");

  // textfield used by the keypad
 numBox = new JTextField();
 numBox.setBounds(50,52,260,40);
 numBox.setFont(textFieldFont);
 numBox.setBackground(Color.orange);

  // my slider aka volume bar also its components
// ChangeListener volumeListener = new volumeListener();
 volume = new JSlider();
 volume.setBounds(50,230,260,30);
 volume.setBackground(Color.orange);
// volume.addChangeListener(this);

    volume.addChangeListener(new ChangeListener() {
    // This method is called whenever the slider's value is changed
      public void stateChanged(ChangeEvent evt) {
        JSlider volume = (JSlider)evt.getSource();

        if (!volume.getValueIsAdjusting()) {
        // Get new value
          value = volume.getValue();
          System.out.println(value);
        }
      }
    });

 low = new JLabel("Low");
 low.setBounds(50,210,40,20);

 high = new JLabel("High");
 high.setSize(40,20);
 high.setLocation(290,210);

 volLabel = new JLabel("Volume:"+value);
 volLabel.setBounds(125,260,55,20);

// volField = new JTextField();
// volField.setBounds(190,260,30,20);
// volField.setBackground(Color.orange);
        // end of slider components


drunkenCoder Nov 15th, 2004 3:12 AM

Quote:


volume.addChangeListener(new ChangeListener() {
// This method is called whenever the slider's value is changed
public void stateChanged(ChangeEvent evt) {
JSlider volume = (JSlider)evt.getSource();

if (!volume.getValueIsAdjusting()) {
// Get new value
value = volume.getValue();
System.out.println(value);
}
}
});

You have to update the JLabel text yourself inside of the if statement in the ChangeListener.
like this:
:

volLabel.setText("Volume:"+value);


All times are GMT -5. The time now is 1:05 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC