简体   繁体   中英

Java: JSlider and taking its value by JTextArea

i need help with my code. I wanted to take value of slider by taking it from TextArea that show value of current position of slider but it takes starting value of it. This is my code:

JSlider ThicknessSlider = new JSlider(0,100,50);
JTextArea text4 = new JTextArea("0");
ThicknessSlider.addChangeListener(new ChangeListener(){

    @Override
    public void stateChanged(ChangeEvent e) {
        // TODO Auto-generated method stub
        JSlider source=(JSlider)e.getSource();
        int value1 = (int)source.getValue();
        text4.setText(Integer.toString(value1));

        //String thickness1 = text4.getText();
    }});
String thickness1 = text4.getText();
this.thickness = Float.parseFloat(thickness1);

You need to move setText and the next line into the ChangeListener.

The problem is that this refers to the ChangeListener in that context. You can add a setter setThickness to your class and call that, or you can use YourClassName.this.thickness to specify which this you mean.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM