java - JSlider not updating? -
i'm quite beginner regarding whole drawing stuff in windows , i'm kinda stuck @ moment. @ moment i'm testing things out.
import javax.swing.*; import java.awt.*; import javax.swing.event.*; public class test extends jframe { jslider slider1; public test() { slider1 = new jslider(jslider.vertical, 0, 50, 0); setlayout(new flowlayout(flowlayout.trailing)); add(slider1); } public void changevalue () { slider1.setvalue(25); } public static void main(string args[]) { test gui = new test(); gui.setdefaultcloseoperation(jframe.exit_on_close); gui.setsize(550,250); gui.setvisible(true); } }
so create jslider call slider1, give orientation , values. when call changevalue method changes slider1 value. there's no change on gui. point me correct direction? have refreshing gui?
after initializing slider1
variable in test()
constructor, add these lines jslider tick values set , visible in gui:
slider1.setmajortickspacing( 5 ); slider1.setpaintlabels( true );
you can change tick spacing set 5. adding slider using add()
method not practice, use getcontentpane().add()
instead, constructor should this:
public test2() { slider1 = new jslider(jslider.vertical, 0, 50, 0); setlayout(new flowlayout(flowlayout.trailing)); slider1.setmajortickspacing( 5 ); slider1.setpaintlabels( true ); this.getcontentpane().add(slider1); }
i have noticed not calling changevalue()
method in main()
method. method name implies, seems setter, not give setting value parametrically, practice? in opinion, not. , changevalue()
same thing setvalue()
, why create redundant method?. anyway, can use this:
public void changevalue (int newvalue) { slider1.setvalue(newvalue); }
in main method, use these statements:
test2 gui = new test2(); gui.changevalue( 25 );
to see immediate effect of changing slider value, mean updating it, use button or other component, add actionlistener
can update slider value upon click, example.
button.addactionlistener(new actionlistener() { public void actionperformed(actionevent arg0) { changevalue( 25 ); // change 25 desired value. } });
Comments
Post a Comment