-1

I'm writing a GUI Java program for student registration which will retrieve available classes from database, give the user an option to choose classes and then store this in DB.

What I'm trying to do and have so far achieved partial success, is this - I created a combo box with the available majors (got that from DB), retrieved the available classes for that major and displayed check boxes for these classes.

There are two issues with this code.

  1. After selecting the major from combo box, the check boxes aren't displayed. They appear one by one only when I hover my cursor.
  2. Once I change my major in the combo box, the check boxes are not updated, even though the console in eclipse says check boxes for newly selected major has been created.

ArrayList<JCheckBox> checkBoxes=new ArrayList<JCheckBox>();
    //combox action listener below
    public void actionPerformed(ActionEvent e) 
    {
        //get all available classes for the selected major
        avail_class = new String[count_class];

        //get all available class ids
        avail_classid = new String[count_class];

        JCheckBox checkbox;
        int xdim = 75;
        for (int i = 0; i < count_class; i++) 
            {
                checkbox = new JCheckBox(avail_classid[i] + "-" + avail_class[i]);
                checkbox.setBackground(new Color(0, 255, 255));
                checkbox.setBounds(183, xdim, 289, 23);
                contentPane.add(checkbox);
                checkBoxes.add(checkbox);
                checkbox.setEnabled(true);

                xdim = xdim + 50;
            }
    }

EDIT

For my second problem, I called repaint() and it worked. For the first one, I did the following:

if(flag < 0)

//flag will be raised whenever there is a change in the selected major. For ex, from web dev to data analytics

        for(int i = 0; i < checkBoxes.size(); i++)
            {
                checkBoxes.get(i).setVisible(false);
                System.out.println("old Checkboxes invisible!" + i);
            }
  • Are you doing anything to invalidate the frame or panel the new checkboxes are in? You need to tell the container to lay out and display the children whenever you change them. – sprinter Nov 29 '16 at 06:46
  • `checkbox.setBounds(183, xdim, 289, 23);` 1) 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). 2) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Nov 29 '16 at 07:04

1 Answers1

1

you need to call repaint and revalidate function to the container that holds your check boxes to redraw it with the new check boxes .

Bassem Salama
  • 467
  • 5
  • 15
  • You need to call `revalidate()` and `repaint()`. Doing the repaint() first won't help since the components will still have a size of (0, 0). The `revalidate()` invokes the layout manager to give the components a size before they are painted. – camickr Nov 29 '16 at 18:15
  • Thanks! repaint did the job. – Afaque Hussain Nov 30 '16 at 00:28