0

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

Iulia
  • 3
  • 5
  • val is a scope variable not an private member, It could not be access through reflection. Can u assign val value in any private member and later can access that member using reflection – Naveen Ramawat Sep 04 '14 at 12:08
  • What do you mean by "to have %" – JamesB Sep 04 '14 at 12:09
  • field.setText(val + "%"); - sorry I forgot to put it, I will edit my post. I'm trying to fix the code, thanks – Iulia Sep 04 '14 at 12:22

4 Answers4

0

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.

Alex
  • 24
  • 3
  • Reflection? Seriously? – JamesB Sep 04 '14 at 12:09
  • Reflection works, and quite a lot of frameworks use it behind the scenes, especially XML to aid in de/serialization. JNI for example uses the same plugs as the Reflection API; but I agree, Reflection is ugly/crap. – Alex Sep 04 '14 at 14:09
0

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?

Mayank_Thapliyal
  • 351
  • 2
  • 7
  • 16
  • I made the String val global variable, and I added jTextField.setText(val); I also added this.val = val; to the method, but still not working, and I have no errors, don't know what to do else... thanks – Iulia Sep 04 '14 at 18:22
0

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.

joey rohan
  • 3,505
  • 5
  • 33
  • 70
0

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.

mirmdasif
  • 6,014
  • 2
  • 22
  • 28
  • I did what you said but still not taking the value to JTextField. Clean and build without errors... Is there something I'm doing wrong? Thanks – Iulia Sep 04 '14 at 18:17
  • Follow my edited answer. If you still got any problem comment else accept my answer. – mirmdasif Sep 06 '14 at 10:46