title: Highlight user input label in swing
body: <p>I need your help,
  I am currently working on a small program using java and swing as a GUI. The logic is as follows:</p>
  <ol>
  <li>Program is painting a 31 array of labels that holds ints, 1 to 31
  (days of the month).</li>
  <li>User inserts a number from a a text field, 1 to 31. If above/lower/not number, program does nothing (will handle exceptions later).</li>
  <li>If user inserts, for example, number 5, program should highlight that specific JLabel from the array and change it’s background or something. It’s important to highlight what user selected</li>
  </ol>
  <p>Problem is, I can’t get it to repaint only that particular label in the array. It always either repaints the whole array of labels or it adds the label at the end of the array, I don’t want it to do that.</p>
  <p>How can I access that particular number in the array and change only that label?</p>
  <pre><code>import java.awt.Color;
  import java.awt.Dimension;
  import java.awt.FlowLayout;
  import java.awt.Toolkit;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.text.SimpleDateFormat;
  import java.util.Calendar;
  import java.util.Date;
  import java.util.Scanner;
  
  import javax.swing.BorderFactory;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JPanel;
  import javax.swing.JSpinner;
  import javax.swing.JTextField;
  import javax.swing.SpinnerDateModel;
  import javax.swing.SpinnerModel;
  import javax.swing.SwingUtilities;
  
  public class BirthDayCalendar {
      JFrame frame;
      Toolkit toolkit;
      Dimension size;
      JPanel monthJPanel, inputTestPanel;
      JLabel dayLabel;
      JLabel[] daysLabelArray;
      String[] userBDayNamesStringArray;
      int[] userBDayDaysArray; // stores the day of the BDay
      int[] userBDayIntMonthArray; // stores the month
      int[] userBDayIntYearArray; // stores the year
  
      JTextField insertNameOfBDayPerson;
      JButton addData, saveButton;
  
      int userInputNumber;
      String userDateStoredString, daysSaved, monthsSaved, yearsSaved;
  
      java.util.Date date = new Date();
      Calendar calendar1; // REQUEST SPINNERS: calendar1 --&gt; initial JSpinner;
  
      Date userInputDate;// gets the request date value
      JSpinner requestDateSpinner;
      SpinnerModel model1; // REQUEST SPINNERS: Model 1 --&gt; initial JSpinner;
      DateDefinition dateData = new DateDefinition();
      JSpinner.DateEditor editor;
      String format;
      Scanner in;
      
      String directory = System.getProperty(&quot;user.home&quot;);
      String fileName = &quot;data.txt&quot;;
      String absolutePath = directory + File.separator + fileName;
      
      
      
      
      
      
      
      
  
      BirthDayCalendar() {
  
          frame = new JFrame(&quot;BirthDay Calendar&quot;);
          frame.setLayout(new FlowLayout());
  
          monthJPanel = new JPanel();
  
          // October test
          // System.out.println(dateData.currentYear + &quot; &quot; + dateData.monthOfOctober + &quot; &quot;
          // + dateData.daysInMonth); //
          // just for checking
  
          // adding the month days on the screen
  
          daysLabelArray = new JLabel[dateData.daysInMonth + 1];
          for (int i = 1; i &lt;= dateData.daysInMonth; i++) {
              dayLabel = new JLabel(i + &quot;&quot;);
              setLabelSizes(dayLabel);
              daysLabelArray[i] = dayLabel;
              monthJPanel.add(daysLabelArray[i]);
          }
  
          inputTestPanel = new JPanel();
  
          insertNameOfBDayPerson = new JTextField(10);
  
          userBDayDaysArray = new int[32];
          userBDayNamesStringArray = new String[32];
          userBDayIntMonthArray = new int[32];
          userBDayIntYearArray = new int[32];
  
          calendar1 = Calendar.getInstance();
          Date now = calendar1.getTime();
          calendar1.add(Calendar.YEAR, -100);
  
          Date startDate = calendar1.getTime();
          calendar1.add(Calendar.YEAR, 100);
  
          Date endDate = calendar1.getTime();
  
          model1 = new SpinnerDateModel(now, startDate, endDate, Calendar.YEAR);
  
          requestDateSpinner = new JSpinner(model1); // creating Visual Spinner
          format = &quot;dd MMM yy&quot;; // formatting Visual Spinner
  
          editor = new JSpinner.DateEditor(requestDateSpinner, format);
          requestDateSpinner.setEditor(editor); // applying the formatting to the visual spinner
  
          // System.out.println(&quot;DateDefinition is: &quot; + october.monthOfOctober + &quot; and
          // year is: &quot; + october.currentYear);
          
          
  
      
          
          
          
          
          
          
          
          
          
          
          
  
          addData = new JButton(&quot;Register Date and Name&quot;);
          addData.addActionListener(new ActionListener() {
  
              @Override
              public void actionPerformed(ActionEvent arg0) {
  
                  addUserData();
  
              }
  
          });
  
          saveButton = new JButton(&quot;Save&quot;);
  
          saveButton.addActionListener(new ActionListener() {
  
              @Override
              public void actionPerformed(ActionEvent arg0) {
  
                  insertNameOfBDayPerson.setText(&quot;&quot;);
  
              }
  
          });
  
          inputTestPanel.add(requestDateSpinner);
          inputTestPanel.add(insertNameOfBDayPerson);
          inputTestPanel.add(addData);
          inputTestPanel.add(saveButton);
  
          setPanelSizes(monthJPanel);
          setPanelSizes(inputTestPanel);
  
          frame.add(monthJPanel);
          frame.add(inputTestPanel);
          setSize(frame);
          frame.setVisible(true);
      }
  
      void addUserData() {
  
          SimpleDateFormat formater = new SimpleDateFormat(&quot;dd MM yyyy&quot;);
          userDateStoredString = formater.format(requestDateSpinner.getValue());
          in = new Scanner(userDateStoredString);
          daysSaved = in.next();
          monthsSaved = in.next();
          yearsSaved = in.next();
          in.close();
  
          userInputNumber = Integer.parseInt(daysSaved);
  
          System.out.println(&quot;Day is: &quot; + daysSaved);
          System.out.println(&quot;Month is: &quot; + monthsSaved);
          System.out.println(&quot;Year is: &quot; + yearsSaved);
  
          for (int i = 1; i &lt;= dateData.daysInMonth; i++) {
  
              if (userInputNumber == i) {
  
                  daysLabelArray[userInputNumber]
                          .setText(userInputNumber + &quot; &quot; + insertNameOfBDayPerson.getText().toString());
  
                  monthJPanel.repaint();
                  monthJPanel.revalidate();
  
                  userBDayDaysArray[i] = userInputNumber; // gets the day 
                  userBDayNamesStringArray[i] = insertNameOfBDayPerson.getText().toString(); // stores the name
                  userBDayIntMonthArray[i] = Integer.parseInt(monthsSaved); // stores the month
                  userBDayIntYearArray[i] = Integer.parseInt(yearsSaved); // stores the year
  
              }
              
              // for testing purpose only         
              System.out.println(&quot;day stored: &quot; + userBDayDaysArray[i] + &quot; and the name is: &quot;
                      + userBDayNamesStringArray[i] + &quot; and the month is: &quot; + userBDayIntMonthArray[i]
                      + &quot; and the year is: &quot; + userBDayIntYearArray[i]);
  
          }
          
          // System.out.println(&quot;Length of Days Label Array: &quot; + daysLabelArray.length);
  
          
          // write content to file
          try (FileOutputStream fileOutputStream = new FileOutputStream(absolutePath)) {
              
              String fileContent = &quot;This is a smple text&quot;;
              fileOutputStream.write(fileContent.getBytes());
              
          } catch (FileNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          
          
          
          
          
          
      }
  
      void setSize(JFrame frame) {
          frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
          // frame.setPreferredSize(new Dimension(1000, 700));
          frame.setMinimumSize(new Dimension(1000, 700));
          toolkit = frame.getToolkit();
          size = toolkit.getScreenSize();
          frame.getContentPane().setBackground(Color.gray);
          frame.setExtendedState(frame.MAXIMIZED_BOTH);
  
          // frame.setLocation((size.width - frame.getWidth()) / 2, (size.height -
          // frame.getHeight()) / 2);
  
      }
  
      void setLabelSizes(JLabel label) {
          label.setPreferredSize(new Dimension(70, 50));
          label.setBorder(BorderFactory.createLineBorder(Color.red));
      }
  
      void setPanelSizes(JPanel panel) {
          panel.setLayout(new FlowLayout());
          panel.setPreferredSize(new Dimension(490, 350));
          panel.setMaximumSize(new Dimension(490, 350));
          panel.setMinimumSize(new Dimension(490, 350));
          panel.setBorder(BorderFactory.createLineBorder(Color.black));
      }
  
      public static void main(String[] args) {
          SwingUtilities.invokeLater(() -&gt; {
              BirthDayCalendar calendar = new BirthDayCalendar();
          });
  
      }
  
  }
  </code></pre>
  <p><strong>EDIT: CODE IS NOW FIXED. PROBLEM SOLVED</strong></p>
  <p>Any help is most welcomed.</p>