0

So I have this class:

public class setting{
    JLabel label;
    Component main;
    JButton set, reset;

    setting(String s, Component b){
        this.label = new JLabel(s);
        this.main = b;
        this.set = new JButton("Set");
        this.reset = new JButton("Reset");
    }

    public void add(JPanel b) {
        b.add(this.label);
        b.add(this.main);
        b.add(this.set);
        b.add(this.reset);
    }
}

How would I be able to access this.main 's information? For example:

JSlider speed = new JSlider(0, 80); speed.setValue(0);
setting speedSett = new setting("speed", speed);
speedSett.main.getValue(); //This doesn't work

Is there anyway to access the information inside of the setting class the way I have this setup? I would like this class to accept different types of components, so simply setting it to JSlider won't do it for me. Thank you in advance.

1 Answer 1

1

If you know it's JSlider, cast it and use it:

JSlider speed = new JSlider(0, 80); speed.setValue(0);
setting speedSett = new setting("speed", speed);
if (speeSett.getMain() instanceof JSlider) {
  JSlider slider = (JSlider) speedSett.getMain(); // Recommend to access from getter instead of direct access
  slider.getValue();
}
Sign up to request clarification or add additional context in comments.

1 Comment

Glad it helps you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.