The constructor of MainPanel class is not being accessed when I run the app in the full-screen mode. It is only being accessed when I click on any item from the menubar.
The app runs fine in windowed mode or when I manually set the frame's width and height.
app running in windowed-mode or when I set width and height (working fine)
app running in full-screen mode but the MainPanel() constructor is not being called
Main.java
public class Main {
public static void main(String[] args) {
AppFrame mainFrame = new AppFrame("Algorithm Visualizer");
mainFrame.add(new MainPanel());
}
}
MainPanel.java
public class MainPanel extends JPanel {
MainPanel() {
this.setBackground(Color.BLACK);
}
}
AppFrame.java
class AppFrame extends JFrame implements ActionListener {
private JMenuBar menuBar;
private JMenu fileMenu, sortingAlgoMenu, searchingAlgoMenu;
private JMenuItem exitItem, bubbleSortItem;
// constructor with frame_title and auto app resolution
AppFrame(String frameTitle) {
// sets the app theme
setTheme();
// Frame Properties
this.setTitle(frameTitle);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
GraphicsDevice myDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
// this is the code that makes the frame full-screen
if(myDevice.isFullScreenSupported()) {
this.setUndecorated(true);
myDevice.setFullScreenWindow(this);
}
else {
// windowed mode (title bar present)
int deviceWidth = myDevice.getDisplayMode().getWidth();
int deviceHeight = myDevice.getDisplayMode().getHeight();
this.setSize(deviceWidth, deviceHeight);
this.setLocationRelativeTo(null);
}
// adds the MenuBar
addMenuBar();
// makes the JFrame visible
this.setVisible(true);
}
// constructor passed with app title, width and height
AppFrame(String frameTitle, int frameWidth, int frameHeight) {
// sets the app theme
setTheme();
// Frame Properties
this.setTitle(frameTitle);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(frameWidth, frameHeight);
this.setLocationRelativeTo(null);
// adds the MenuBar
addMenuBar();
// makes the JFrame visible
this.setVisible(true);
}
// sets the theme of the application
private static void setTheme() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
catch (InstantiationException e) {
e.printStackTrace();
}
catch (IllegalAccessException e) {
e.printStackTrace();
}
catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
// setting the JMenuBar
private void addMenuBar() {
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
menuBar.add(fileMenu);
sortingAlgoMenu = new JMenu("Sorting Algorithms");
searchingAlgoMenu = new JMenu("Searching Algorithms");
menuBar.add(sortingAlgoMenu);
menuBar.add(searchingAlgoMenu);
exitItem = new JMenuItem("Exit");
bubbleSortItem = new JMenuItem("Bubble Sort");
fileMenu.add(exitItem);
sortingAlgoMenu.add(bubbleSortItem);
// on-click of "Exit"
exitItem.addActionListener(this);
this.setJMenuBar(menuBar);
}
// handle action events (on-click listeners)
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == exitItem)
System.exit(0);
}
}
CodePudding user response:
Just add the MainPanel in the app frame right before addMenuBar(). The constructor is called anyways but the MainPanel is not visible the other way.
CodePudding user response:
I found what I was doing wrong. I was making the JFrame visible before adding the JPanel. So, I just removed the frame visibility from the AppFrame class and set its visibility(true) from Main.class after adding the JPanel.