2

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:

DesiredOutcome

almightyGOSU
  • 3,731
  • 6
  • 31
  • 41
  • 2
    `mainPanel.setLayout(null);` There's the problem. Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). Provide ASCII art or a simple drawing of the layout of the GUI at minimum size, and if resizable, with more width and height. – Andrew Thompson Jun 10 '15 at 13:22
  • `window.setLocation((screenWidth/2)-(window.getWidth()/2), (screenHeight/2)-(window.getHeight()/2));` try instead.. `window.setLocationRelativeTo(null);` – Andrew Thompson Jun 10 '15 at 13:24
  • @AndrewThompson Thanks alot! It works perfectly now that I changed it to a box layout, but is there any layout that doesnt mess up the location of the button, as that is why i was using null layout manager in the first place? – Joey Kangaroo Jun 10 '15 at 13:26
  • First error in your code: you use a Null layout as pointed out by Andrfew Thompson. Second error: `mainPanel.add(autoRedLightBox, BorderLayout.CENTER)` only makes sense for `BorderLayout`. – Axel Jun 10 '15 at 13:27
  • 2
    @AndrewThompson Oh thanks, I never knew centering the window was THAT EASY! Thanks Alot! – Joey Kangaroo Jun 10 '15 at 13:27
  • *"..is there any layout that doesnt mess up the location of the button"* Probably. For anything more definite, post a (link to a) drawing or ASCII art that shows where it is supposed to be laid out, like I suggested at the end of the first comment. – Andrew Thompson Jun 10 '15 at 13:28
  • @Axel I used the BorderLayout thing there for testing if it made a difference when i was googleing how to fix this, I know it wouldnt make a difference unless i used border layout – Joey Kangaroo Jun 10 '15 at 13:29
  • Here is a drawing of roughly what i want it to look like, if you know a layout to fit this it would be helpful. The spacing is a bit more specific than in this drawing, as it appears to all be aligned in columns and rows in this drawing. http://i.imgur.com/fVMcesi.png – Joey Kangaroo Jun 10 '15 at 13:36
  • @JoeyKangaroo Please edit your post instead of extending with a comment – Binkan Salaryman Jun 10 '15 at 13:38
  • @BinkanSalaryman I don't understand which comment i shouldve edited into my post? – Joey Kangaroo Jun 10 '15 at 13:39
  • @JoeyKangaroo The previous one with the image draft should be in your post, you may prefer a grid-orientated layout here, like [GridBagLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html) – Binkan Salaryman Jun 10 '15 at 13:42
  • 2
    @BinkanSalaryman Thanks, and i just found a website with list of layouts, and i thought ot myself, gridbag would probably work with what i want, and then i saw your comment, you read my mind – Joey Kangaroo Jun 10 '15 at 13:45

2 Answers2

5

That's well suited to a GridLayout.

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class ButtonsAndChecks {

    private JComponent ui = null;

    ButtonsAndChecks() {
        initUI();
    }

    public void initUI() {
        if (ui!=null) return;

        // adjust last two numbers to need..
        ui = new JPanel(new GridLayout(0,5,20,20));
        ui.setBorder(new EmptyBorder(4,4,4,4));

        // adjust numbers to need..
        for (int i=1; i<26; i++) {
            ui.add(new JButton("Button " + i));
        }
        // adjust numbers to need..
        for (int i=1; i<26; i++) {
            ui.add(new JCheckBox("Check " + i, i%2==0));
        }
    }

    public JComponent getUI() {
        return ui;
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (Exception useDefault) {
                }
                ButtonsAndChecks o = new ButtonsAndChecks();

                JFrame f = new JFrame(o.getClass().getSimpleName());
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);

                f.setContentPane(o.getUI());
                f.pack();
                f.setMinimumSize(f.getSize());

                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

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

    mainPanel.setLayout(null);

just comment it and you will see buttons on form. Also, for putting your form in center of screen you can call this method

    window.setLocationRelativeTo(null);