JSlider is not updated?

I am new to all the painting material in windows, and now I'm a bit stuck. For the moment, I'm just checking this.

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, I create a JSlider that I call slider1, I give it its orientation and values. When I call the changeValue method, it explicitly changes the value of slider1. But there are no changes in the graphical interface. Can someone point me in the right direction? Does he have something with a GUI update?

+4
source share
3 answers

After initializing the slider1 variable in the test() constructor, add these lines so that the jslider tick values ​​can be set and visible in the graphical interface:

 slider1.setMajorTickSpacing( 5 ); slider1.setPaintLabels( true ); 

You can change the tick step that is set to 5. Adding a slider using the add() method is not good practice, use getContentPane().add() instead, so your constructor should look like 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 noticed that you are not calling changeValue() in the main() method. As your method name implies, it seems to be a setter, however you are not setting a parametric value parametrically, is this good practice? In my opinion, this is not so. And also changeValue() does the same with setValue() , why create a redundant method ?. Anyway, you can use this:

 public void changeValue (int newValue) { slider1.setValue(newValue); } 

In your main method, use the following instructions:

 test2 gui = new test2(); gui.changeValue( 25 ); 

To see the direct effect of changing the value of the slider, I mean updating it, use a button or some other component, add an ActionListener to it ActionListener that you can, for example, update the value of the slider when pressed.

 button.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { changeValue( 25 ); // change 25 to desired value. } }); 
+5
source

You need to somehow call changeValue:

I just added a JButton with an ActionListener to do this, and I managed to get it to work with the following code:

 package testswing; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class JSliderTest extends JFrame { JSlider slider1; JButton button1; public JSliderTest() { setLayout(new FlowLayout()); slider1 = new JSlider(JSlider.VERTICAL, 0, 50, 0); add(slider1); button1 = new JButton("Centre JSlider!"); button1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { changeValue(); } }); add(button1); } public void changeValue () { slider1.setValue(25); } public static void main(String args[]) { JSliderTest gui = new JSliderTest(); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setSize(550,250); gui.setVisible(true); } } 
+3
source

You do not call changeValue() in your main method. Make the changeValue () method and slider1 static , and then call changeValue() on main ...

 static JSlider slider1; public Test() { slider1 = new JSlider(JSlider.VERTICAL, 0, 50, 0); setLayout(new FlowLayout(FlowLayout.TRAILING)); add(slider1); } public static void changeValue () { slider1.setValue(35); } public static void main(String args[]) { Test gui = new Test(); gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); gui.setSize(550,250); gui.setVisible(true); changeValue(); } 

And it’s best to make the first letter of each word capitalized for Java class names.

+1
source

Source: https://habr.com/ru/post/1400134/


All Articles