Search code examples
javanetbeansslidertrygetvalue

How to take a value from a private method


I have created using NetBeans a jSlider ChangeEvent, with the following code:

public class Slider extends javax.swing.JFrame {
    public Slider() {
         initComponents;
         field.getText();
         String fieldVal = field.getText();
         jtextField1.setText(fieldVal):
    }

    public JTextField getField() {
         return field;
    }

    public void setField(JTextField field) {
          this.field = field;
    }

    private void sliderStateChanged(javax.swing.event.ChangeEvent evt) { 
          int value = slider.getValue();
          String val = value + "";
          field.setText(val + "%");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new Slider().setVisible(true);
        }
    });
    }
}

So, I have a JTextField (field) who is receiving values from jSpiner. Everything works fine, but I want to take the "val" and make some computations with it.

But I can't do that, because it's in a private method, and I've tried to make it public, with Refactor -> Change Method Parameters, but it gives me the following warning: Read-only block of text cannot be refactored., and it doesn't work.

I also have tried to make getters and setters, but still not working. All I want is to take that value. I could take it directly from JTextField (field) but I want to have "%" also in it so I can't do computations... Can someone please help me with an idea? I know that I'm wrong with something, but don't know where is the mistake. Or is a possibility to put "%" in the text field in other way? I need some help, thanks!

Best regards, Iulia


Solution

  • From your edited question I can see that all you need is a text box showing value of a slider with % appended.

    Here is a code example showing this. https://gist.github.com/anonymous/96e898ec41cd2ed0f821

    Create a file name NewJFrame.java copy and pest the code and run to see the result.

    As per your code I think theres something wrong with text field may be it's not initialized. If you share your whole code my be I can find the bug.