How to take a value from a private method

1.1k Views Asked by At

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

4

There are 4 best solutions below

2
On BEST ANSWER

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.

2
On

Java Reflection to the rescue! Use getDeclaredField on the class, and then setAccessible(true); lastly reflectively get the value through field.get(instance)! Tuh-duh.

Alternatively, and much more complexly, implement a ClassLoader and modify the "modifiers" for said field/method to make it public...

Overall, reflection has major implications and impact. Please read into it.

1
On

From what I understand, can't you make String val a global variable outside the scope rather than a local variable and then work with it?

0
On

A temporary solution can be :

If you want to make changes to the restricted area, copy paste the code in a new .java file and edit.

If it is only about the value, create a public variable and assign the JSlider value.