0

I'm creating a sort of OS simulator in Java using Swing. I want to add a status bar containing the date and time to my OS's main menu, but nothing I've tried works. The JFrame has a background that can be changed by the user, if that helps. Here's the code:

    class MainMenu implements ActionListener 
    {  
    //class variables here
     MainMenu(){  
    f= new JFrame("JavaOS");
    f.setExtendedState(f.MAXIMIZED_BOTH);
    file = new File("bgpath.txt");
    setBackground();

    JMenuBar mb=new JMenuBar();  
    menu1=new JMenu("Programs");
    menu2=new JMenu("<>"); 
    submenu = new JMenu("Games");
    //and so on...
    submenu.add(i6); submenu.add(i7);
    menu1.add(i1); menu1.add(i2); menu1.add(i3); menu1.add(i4); menu1.add(i5); menu1.add(submenu);
    menu2.add(j3); menu2.addSeparator(); menu2.add(j2); menu2.add(j1);
    mb.add(menu2);
    mb.add(menu1);  
    f.setJMenuBar(mb);
    chooser=new JFileChooser();
     //this was my last attempt
     statusBar=new JLabel("",JLabel.RIGHT);
     statusBar.setPreferredSize(new Dimension(100, 20));
  f.add(statusBar,BorderLayout.SOUTH);
    f.setSize(400,400);
    f.setLayout(null);
   
    try {
        // Significantly improves the look of the output in
        // terms of the file names returned by FileSystemView!
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch(Exception weTried) {
    }
    //f.setUndecorated(true);
    Runnable runnable = new Runnable() {//also my last attempt

    @Override
    public void run() {
      while (true) {
        Date date = getDate();
        String dateString = simpleDateFormat.format(date);
        statusBar.setText(dateString);
        f.repaint();
        try {
          Thread.sleep(1000);
        }
        catch (InterruptedException e) {
          e.printStackTrace();
        }
      }
    }
};
Thread t = new Thread(runnable);
  t.start();
    f.setVisible(true);
    f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
} 
public static java.util.Date getDate() {
  java.util.Date date = new java.util.Date();
  return date;
} //normal code now

public void actionPerformed(ActionEvent e) {    
    if(e.getSource()==i2)
        new Notepad();
    else if(e.getSource()==i1)
        FileBrowser.main(null);
    //and so on...
    }
    else if(e.getSource()==j3)
        openFile();

} 

void openFile()
{

    chooser.setDialogTitle("Change Background");
    chooser.setApproveButtonText("Open"); 
    chooser.setApproveButtonMnemonic(KeyEvent.VK_O);
    chooser.setApproveButtonToolTipText("Open");

    bg=null;
    do
    {
        if(chooser.showOpenDialog(f)!=JFileChooser.APPROVE_OPTION)
            return;
        bg=chooser.getSelectedFile();

        if(bg.exists()) break;

        JOptionPane.showMessageDialog(f,
            "<html>"+bg.getName()+"<br>file not found.<br>"+
            "Please verify the correct file name was given.<html>",
            "Open", JOptionPane.INFORMATION_MESSAGE);

    } while(true);

    changeBackground();

}
void changeBackground(){
    try 
    {
        file.delete();
        file = new File("bgpath.txt");
        fw = new FileWriter(file, true);
        fw.write((bg.getAbsolutePath()).toString());
        fw.close();
        setBackground();

    }
    catch(IOException d) 
    {
        d.printStackTrace();
    }
}

void setBackground(){
    try 
    {
        String path = (new Scanner(file).useDelimiter("\\Z").next()).replace("\\","\\\\");
        f.setContentPane(new JLabel(new ImageIcon(path)));

    }
    catch(IOException d) 
    {
        d.printStackTrace();
    }
}

Any tips? I'm using BlueJ and JDK 11, if that helps :)

EDIT: The issue is that the status bar doesn't appear at all. Should've mentioned this before, sorry.

  • 1
    _Any tips?_ you ask? Yes. Study the following tutorial: [Creating a GUI With JFC/Swing](https://docs.oracle.com/javase/tutorial/uiswing/index.html) – Abra Nov 04 '20 at 14:51
  • See: https://stackoverflow.com/questions/30417719/update-a-label-with-a-swing-timer/30417857#30417857 for an example that uses a Swing Timer to update the time every second. – camickr Nov 04 '20 at 15:33
  • 1) For better help sooner, [edit] to add a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! Most IDEs have a keyboard shortcut specifically for formatting code. – Andrew Thompson Nov 04 '20 at 22:43

1 Answers1

-1

I'm assuming your problem is that the date doesn't update by itself, if so you need to add a timer that will at a given interval update your date. This will create a timer that updates your statusBar every second.

    JLabel statusBar= new JLabel();
    timer = new Timer(1000, e -> {
        Date date = new Date();
        statusBar.setText(date.toString());
    });
    timer.setInitialDelay(0);
    timer.start();
  • 1
    Maybe you should explain what [Timer](https://docs.oracle.com/en/java/javase/11/docs/api/java.desktop/javax/swing/Timer.html) is? – Abra Nov 04 '20 at 14:46