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.