I have created 3 classes (code given below).
BasicDetails - base class - frame created here along with a panel GradeSubjectList - derived class - another panel created here and loaded in the frame created in the base class SubjectList - derived class - another panel created here and loaded in the frame created in the base class
Base class has a panel of its own and on click of the button loads the panel created in GradeSubjectList. A button created in this panel calls a function in base class which inturn loads the panel created in SubjectList in the frame created in the base class.
The panel created in BasicDetails and GradeSubjectList load with any issues, and when the panel created in SubjectList is being loaded in the frame created in the base class displays only in a blank frame
public class BasicDetails{
static int lec_no;
static int sub_no;
JFrame frame = new JFrame("Time Table");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
JPanel panel_bd = new JPanel();
public static void main(String[] args) {
BasicDetails bd = new BasicDetails();
bd.main_method_bd();
}
public void main_method_bd() {
panel_bd.setBackground(Color.white);
frame.setExtendedState(java.awt.Frame.MAXIMIZED_BOTH);
panel_bd.setLayout(null);
JLabel lec_no_lbl = new JLabel("Enter the number of lectures required per day:");
JTextField lec_no_txt = new JTextField(5);
lec_no_txt.setText("5");
JButton next_btn_1 = new JButton("Next");
panel_bd.add(lec_no_lbl);
panel_bd.add(lec_no_txt);
panel_bd.add(next_btn_1);
lec_no_lbl.setBounds(5,100,300,20);
lec_no_txt.setBounds(320,100,50,20);
next_btn_1.setBounds(520,350,100,20);
next_btn_1.addActionListener(e->
{
lec_no=Integer.parseInt(lec_no_txt.getText());
panel_bd.setVisible(false);
gsl(); //calls function to load the next panel
});
frame.setSize(screenSize.width,screenSize.height);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.add(panel_bd);
}
//----this function loads the panel from GradeSubjectList.java
public void gsl() {
GradeSubjectList GSL = new GradeSubjectList();
JPanel gsl_panel = GSL.main_method_gsl();
frame.add(gsl_panel);
}
//-----this function loads the panel from SubjectList.java (this function gets called from GradeSubjectList.java but the panel does not load)
public void sl() {
SubjectList sl = new SubjectList();
JPanel sl_panel = sl.main_mehod_sl();
frame.add(sl_panel);
}
}
public class GradeSubjectList extends BasicDetails{
JComboBox grd_cb = new JComboBox();
JComboBox sub_cb_1 = new JComboBox();
public JPanel main_method_gsl() {
//-------Panel creation and loading components------------
JPanel panel_gsl = new JPanel();
panel_gsl.setBackground(Color.blue);
panel_gsl.setLayout(null);
JLabel tec_details_txt = new JLabel("Select the Grades and Subjects");
JButton next_btn_5 = new JButton("Next");
next_btn_5.setBounds(600,500,150,20);
tec_details_txt.setBounds(100,50,250,20);
grd_cb.setBounds(10,100,100,20);
sub_cb_1.setBounds(130,100,100,20);
panel_gsl.add(tec_details_txt);
panel_gsl.add(next_btn_5);
panel_gsl.add(grd_cb);
panel_gsl.add(sub_cb_1);
//On click perform actions
next_btn_5.addActionListener(e->
{
panel_gsl.setVisible(false);
sl(); //---------calls function in BasicDetails.java to load the next panel
});
return(panel_gsl); ////------returns panel to BasicDetails.java to load it in the frame
}
}
public class SubjectList extends BasicDetails{
public JPanel main_mehod_sl() {
//-------Panel creation and loading components------------
JPanel panel_sl = new JPanel();
panel_sl.setBackground(Color.white);
panel_sl.setLayout(null);
JLabel sub_list_lbl = new JLabel("Enter Subject Name");
JLabel max_day_lbl = new JLabel("Max Lectures Per Day");
JLabel max_week_lbl = new JLabel("Max Lectures Per Week");
sub_list_lbl.setBounds(25,50,150,20);
max_day_lbl.setBounds(200,50,150,20);
max_week_lbl.setBounds(375,50,150,20);
JButton next_btn_2 = new JButton("Next");
next_btn_2.setBounds(600,500,150,20);
JTextField sub_name_txt = new JTextField();
JTextField max_day_txt = new JTextField();
JTextField max_week_txt = new JTextField();
sub_name_txt.setBounds(25,100,150,20);
max_day_txt.setBounds(200,100,105,20);
max_week_txt.setBounds(375,100,150,20);
panel_sl.add(sub_list_lbl);
panel_sl.add(max_day_lbl);
panel_sl.add(max_week_lbl);
panel_sl.add(next_btn_2);
panel_sl.add(sub_name_txt);
panel_sl.add(max_day_txt);
panel_sl.add(max_week_txt);
//On click action
next_btn_2.addActionListener(e->
{
panel_sl.setVisible(false);
});
return(panel_sl); //------returns panel to BasicDetails.java to load it in the frame
}
}
I have changed the order in which the panels are loaded from other classes. I noticed that the panel which is being loaded first (from the other class) does load without any problem while the other panel does not.
for e.g. if the order is
- GradeSubjectList - panel loads properly
- SubjectList - panel does not show
and is the order is changed
- SubjectList - panel loads properly
- GradeSubjectList - panel does not show
So my guess is the most of the code is working fine...cannot just figure out where it is going wrong...
CodePudding user response:
The main problem that the third panel in the sequence is not visible, is because it is added to a new instance of JFrame
each time. Because SubjectList
inherits from BasicDetails
(same for GradeSubjectList
) and because frame
reference is not static
, each new BasicDetails
object (including any new instance of GradeSubjectList
and SubjectList
) gets a new instance of JFrame
where the next panel in the sequence (when pressing the respective Next
button) is added to that new JFrame
.
A solution would be to make the frame
a static
reference and/or revisit your class diagram.
Other than that, always try to use a LayoutManager
(instead of setLayout(null);
). In your case a CardLayout
would handle the job of setting [in]visible each panel in the sequence when needed, so that you wouldn't have to do it manually. You can also use a LayoutManager
for each panel you add to the frame
(such as a GridBagLayout
for example).