Search code examples
javaswinglayout-managerspringlayout

Issue setting constraints on a JButton from a Panel Array


I am creating a calendar and I create a separate JPanels for each day. All of these panels are put into an ArrayList. However, when I try to add constraints to a JButton, using the SpringLayout, it just doesn't seem to be working from being called from the ArrayList.

I do call validate(); and repaint();

However I still have no luck.

    private void addEvents(int currentMonth) {
    for (int i = 0; i < eventArrayList.size(); i++) {
        JButton eventButton = new JButton();
        eventButtonArray.add(eventButton);
        String[] eventListSplit = eventArrayList.get(i).split(", ");
        if (Integer.parseInt(eventListSplit[2]) == currentMonth && eventListSplit[4].equalsIgnoreCase(initialFrame.getInitialPanel().getRightPanel().getNorthPanel().getYear().getText())) {
            eventButton.setText(eventListSplit[0]);
            eventButton.setBackground(Color.decode("#FF9D00"));
            eventButton.setFocusPainted(false);
            eventButton.setFont(new Font("Times New Roman", Font.BOLD, screenSize.width / 100));
            initialFrame.getInitialPanel().getRightPanel().getCenterPanel().getDayPanelArray().get(Integer.parseInt(eventListSplit[3]) + dateHashMap.get(currentMonth - 1).get(0) - 2).add(eventButton);
            initialFrame.getInitialPanel().getRightPanel().getCenterPanel().getDayPanel().getLayout().putConstraint(SpringLayout.HORIZONTAL_CENTER, eventButton, 0, SpringLayout.HORIZONTAL_CENTER, (DayPanel)initialFrame.getInitialPanel().getRightPanel().getCenterPanel().getDayPanelArray().get(Integer.parseInt(eventListSplit[3]) + dateHashMap.get(currentMonth - 1).get(0) - 2));
            initialFrame.getInitialPanel().getRightPanel().getCenterPanel().getDayPanel().getLayout().putConstraint(SpringLayout.VERTICAL_CENTER, eventButton, 0, SpringLayout.VERTICAL_CENTER, initialFrame.getInitialPanel().getRightPanel().getCenterPanel().getDayPanelArray().get(Integer.parseInt(eventListSplit[3]) + dateHashMap.get(currentMonth - 1).get(0) - 2));
            eventButton.addActionListener((ActionEvent e) -> {
                try {
                    newEvent = new NewEvent(screenSize);
                } catch (ParseException ex) {
                    System.out.println(ex.getMessage());
                }

                for (int k = 0; k < eventArrayList.size(); k++) {
                    String[] eventListSplit2 = eventArrayList.get(k).split(", ");
                    if (eventButton.getText().equalsIgnoreCase(eventListSplit2[0])) {
                        newEvent.getNewEventInitialPanel().getEventName().setText(eventListSplit2[0]);
                        newEvent.getNewEventInitialPanel().getLocationBox().setText(eventListSplit2[1]);
                        newEvent.getNewEventInitialPanel().getStartDate().setText(eventListSplit2[2] + "/" + eventListSplit2[3] + "/" + eventListSplit2[4]);
                        newEvent.getNewEventInitialPanel().getStartTime().setText(eventListSplit2[5] + ":" + eventListSplit2[6] + ":" + eventListSplit2[7]);
                        newEvent.getNewEventInitialPanel().getEndDate().setText(eventListSplit2[8] + "/" + eventListSplit2[9] + "/" + eventListSplit2[10]);
                        newEvent.getNewEventInitialPanel().getEndTime().setText(eventListSplit2[11] + ":" + eventListSplit2[12] + ":" + eventListSplit2[13]);
                        if (eventListSplit2[14].equalsIgnoreCase("true")) {
                            newEvent.getNewEventInitialPanel().getAllDay().setSelected(true);
                        }
                        newEvent.getNewEventInitialPanel().getDescription().setText(eventListSplit2[15]);
                    }
                }

                newEvent.setSize(screenSize.height / 2, screenSize.width / 2);
                newEvent.setLocationRelativeTo(null);
                newEvent.setVisible(true);

                newEvent.getNewEventInitialPanel().getSave().addActionListener((ActionEvent e1) -> {
                    JOptionPane.showMessageDialog(newEvent.getNewEventInitialPanel(), "Option not yet supported!");
                });

                newEvent.getNewEventInitialPanel().getDelete().addActionListener((ActionEvent e1) -> {
                    JOptionPane.showMessageDialog(newEvent.getNewEventInitialPanel(), "Option not yet supported!");
                });
            });
        }
    }
    initialFrame.validate();
    initialFrame.repaint();
}    

While I tested to make sure that I am getting the correct panel I still have no luck in moving the JButton. It should be centered based off of the code.

enter image description here

Thank you for your help!


Solution

  • According to Java's SpringLayout references (https://docs.oracle.com/javase/tutorial/uiswing/layout/spring.html)

    It appears that you might be missing the west/east and north/south constraints.

    Unlike many layout managers, SpringLayout does not automatically set the location of the components it manages. If you hand-code a GUI that uses SpringLayout, remember to initialize component locations by constraining the west/east and north/south locations. Depending on the constraints you use, you may also need to set the size of the container explicitly.

    Examples of how to set the West/East and North/South locations in SpringDemo2.java and SpringDemo3.java (linked on that page).