Skip to content

Instantly share code, notes, and snippets.

@tonetheman
Created June 14, 2013 04:47
Show Gist options
  • Select an option

  • Save tonetheman/5779528 to your computer and use it in GitHub Desktop.

Select an option

Save tonetheman/5779528 to your computer and use it in GitHub Desktop.
java program example to update menu on the fly
import javax.swing.*;
public class JL {
JFrame f = null;
JMenuBar mb = null;
JMenu fm = null;
JMenuItem mi = null;
private void sleep() {
try { Thread.sleep(1000); } catch(Exception e) {}
}
private void make_background_thread() {
Runnable r = new Runnable() { // make a runnable
public void run() {
while(true) { // infinite loop here
sleep(); // wait a second
if (fm!=null) { // if the file menu has been created
// odd java syntax here, create another runnable
// for the swing invoke later
SwingUtilities.invokeLater(
new Runnable() {
public void run() {
// fm is not null this is safe
fm.setText("file:" + System.currentTimeMillis());
}
}
);
}
}
}
};
// create the background thread
Thread t = new Thread(r);
// run forest run
t.start();
}
public void start() {
make_background_thread();
f = new JFrame("JL");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mb = new JMenuBar();
fm = new JMenu("File");
mb.add(fm);
mi = new JMenuItem("woohoo");
fm.add(mi);
f.setJMenuBar(mb);
f.setSize(200,200);
f.setVisible(true);
}
public static void main(String [] args) {
JL mainline = new JL();
mainline.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment