Update GUI elements in java

I am new to GUI in java.

For example, I need to update only one item (e.g. JLabel ). In Tkinter, I would use something like root.update() or root.update_idletasks() . I wonder if there is a similar simple function for applications created using swing. I tried gui_element.SetVisible(false) and gui_element.SetVisible(true) and similar things, but not very successfully. I suspect something with javax.swing.Timer should work, but don't know how to do it.

EDIT Here is the code. Please let me know if you find other errors. thanks

 import java.awt.Color; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; public class sof_sample{ /** * @param args */ private static JFrame frame1 = new JFrame("Fractal Geometry"); public static JButton quit_button = new JButton("Quit"); private static JButton run_button = new JButton("Run"); private static JLabel label1 = new JLabel(); public static JLabel label_iter = new JLabel(); private static GridLayout layout1 = new GridLayout(2,20); private JOptionPane msg1 = new JOptionPane(); private static int counter; public static void main(String[] args) { // TODO Auto-generated method stub AuxClass1 inst1 = new AuxClass1(); quit_button.addActionListener(inst1); run_button.addActionListener(inst1); label1.setText("Iteration"); label1.setVerticalTextPosition(JLabel.CENTER); label1.setHorizontalAlignment(JLabel.CENTER); label_iter.setText("0"); label_iter.setBorder(BorderFactory.createLineBorder(Color.BLACK)); label_iter.setVerticalTextPosition(JLabel.CENTER); label_iter.setHorizontalAlignment(JLabel.CENTER); //add widgets to the frame frame1.add(label1); frame1.add(label_iter); frame1.add(run_button); frame1.add(quit_button); frame1.setLayout(layout1); frame1.setLocationRelativeTo(null); //frame1.pack(); frame1.setSize(250, 75); frame1.setVisible(true); } } class AuxClass1 implements ActionListener{ sof_sample inst2 = new sof_sample(); public void actionPerformed(ActionEvent event1){ if (event1.getSource()==inst2.quit_button){ System.exit(0); } else{ for (int i=0;i<20000;i++){ inst2.label_iter.setText(Integer.toString(i)); } //msg1.showMessageDialog(frame1, "Not yet!"); } } } 
+6
source share
2 answers

Swing is an event. All components work through the MVC pattern. You do not need to explicitly redraw it, hiding / showing it to refresh its presentation on the screen.

You just do yourLabel.setText("your new text") and the new text appears on the label.

Keep in mind that most GUI updates ( setText exception) should be run on EDT, so use SwingUtilities.invokeLater , etc. if you are being updated with, for example, a network message.

If you are revalidate() structural changes to the GUI, you'll have to revalidate() / repaint() though.

+7
source

Using Javax.swing.Timer, you can achieve your goal.

  timer = new Timer(100,new ActionListener() { @Override public void actionPerformed(ActionEvent e) { label.setText(String.valueOf(i)); if(i==100){ timer.stop(); } i++; } }); 

If you want to change the values ​​in JLabel, at this point just start the timer using timer.start() , so the values ​​on the label will be changed to 100. When the i value reaches 100, timer.stop() will stop the timer.

Here is the javax.swing.timer tutorial. Look at it! http://docs.oracle.com/javase/1.4.2/docs/api/javax/swing/Timer.html

Download a working example from the following link ...

https://docs.google.com/open?id=0B9U-BwYu62ZaQWZMWloxdHp0YWs

I think this will work for you.

0
source

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


All Articles