I have checked everwhere for a fix but nothing can work to make my checkbox appear. I added it to the panel and added the panel to the window. The button is appearing so it must be a problem with the checkbox. Here is my code:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainApplication {
public static Toolkit tk = Toolkit.getDefaultToolkit();
public static void main(String[] args) {
MainApplication instance = new MainApplication();
instance.start();
}
private JFrame window;
private JPanel mainPanel;
private JPanel contingencyPanel;
private JButton applyButton = new JButton("Apply Changes");
private JCheckBox autoRedLightBox = new JCheckBox("Red Light");
private JCheckBox autoYellowLightBox = new JCheckBox("Yellow Light");
private JCheckBox autoGreenLightBox = new JCheckBox("Green Light");
private JCheckBox autoBlueLightBox = new JCheckBox("Blue Light");
public void start() {
window = new JFrame("Main Control Window");
mainPanel = new JPanel();
contingencyPanel = new JPanel();
window.setSize(1280, 720);
window.setResizable(false);
window.setFocusable(true);
window.setFocusTraversalKeysEnabled(true);
int screenWidth = (int)tk.getScreenSize().getWidth();
int screenHeight = (int)tk.getScreenSize().getHeight();
window.setLocation((screenWidth/2)-(window.getWidth()/2), (screenHeight/2)-(window.getHeight()/2));
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel.setLayout(null);
contingencyPanel.setLayout(null);
applyButton.setToolTipText("Changes will be applied to the arduino.");
applyButton.setSize(new Dimension(120, 30));
applyButton.setLocation(new Point((1280-120)-10, (720-56)-10));
autoRedLightBox.setSelected(true);
autoRedLightBox.setLocation(new Point(30, 30));
autoRedLightBox.setMnemonic(KeyEvent.VK_R);
mainPanel.add(applyButton);
mainPanel.add(autoRedLightBox, BorderLayout.CENTER);
window.add(mainPanel);
window.setVisible(true);
}
}
Desired Outcome:
Read a bit about LayoutManagers in Swing. Every panel needs a layout to hold components, if you don't specify layout for JPanel it uses default one (FlowLayout). The problem was in this line, when you set null for layout
just comment it and you will see buttons on form. Also, for putting your form in center of screen you can call this method