Reputation: 21
I've a class that extends JPanel
I use it as GUI to hold a worker information including a salary which is available in two options chosen by radio buttons.
When the first one is chosen nothing has to be done on the panel.
But if the second radio button is chosen we have to show some extra text fields, and *here comes the problem - my panel does not resize to fit the components and its goes totally in disorder.
I've tried to implement an observer pattern but it doesn't work or may have done it wrong here in follow the main parts of my class.
public class EmpDetails extends JPanel {
public EmpDetails() {
matriculeLab = new JLabel("MATRICULE :");
matriculeTxt = new JTextField(10);
nomLab = new JLabel("Nom :");
nomTxt = new JTextField(30);
serviceLab = new JLabel("SERVICE :");
serviceDrBx = new JComboBox<>();
fonctionLab = new JLabel("FONCTION :");
fonctionDrBx = new JComboBox<>();
dateEmbLab = new JLabel("DATE D'EMBAUCHE :");
dateEmb = DatePick.getDatePick();
/// This is the first radio button
salaireFxBtn = new JRadioButton("FIXE ");
salaireFxBtn.setSelected(true);
/// This is the second radio button that shows the hiden elements
salaireChngBtn = new JRadioButton("CHANGEANT ");
bg.add(salaireFxBtn);
bg.add(salaireChngBtn);
btnPanel.setLayout(new GridLayout(2, 1));
btnPanel.add(salaireFxBtn);
btnPanel.add(salaireChngBtn);
salaireLab = new JLabel("MONTANT DU SALAIRE:");
montatSalFix = new JFormattedTextField(NumberFormat.getIntegerInstance());
/// This is the panel holding the hidden elements its shown when radio button 2 is clicked
salPanel.setLayout(new GridLayout(3, 2));
salaire1 = new JLabel("MONTANT SALAIRE 1:");
montantSal1 = new JFormattedTextField(NumberFormat.getIntegerInstance());
salPanel.add(salaire1);
salPanel.add(montantSal1);
periodeLab1 = new JLabel("DUREE 1 :");
periode1 = new JFormattedTextField(NumberFormat.getIntegerInstance());
salPanel.add(periodeLab1);
salPanel.add(periode1);
salaire2 = new JLabel("MONTANT SALAIRE 2:");
montantSal2 = new JFormattedTextField(NumberFormat.getIntegerInstance());
salPanel.add(salaire2);
salPanel.add(montantSal2);
periodeLab2 = new JLabel("DUREE 2 :");
periode2 = new JFormattedTextField(NumberFormat.getIntegerInstance());
salPanel.add(periodeLab2);
salPanel.add(periode2);
salaire3 = new JLabel("MONTANT SALAIRE 3:");
montantSal3 = new JFormattedTextField(NumberFormat.getIntegerInstance());
salPanel.add(salaire3);
salPanel.add(montantSal3);
periodeLab3 = new JLabel("DUREE 3 :");
periode3 = new JFormattedTextField(NumberFormat.getIntegerInstance());
salPanel.add(periodeLab3);
salPanel.add(periode3);
salPanel.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY, 1));
salPanel.setVisible(false);
indemLab = new JLabel("INDEMNITE :");
indemnite = new JFormattedTextField(NumberFormat.getIntegerInstance());
salaireChngBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (salaireChngBtn.isSelected()) {
salPanel.setVisible(true);
}
}
});
salaireFxBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
if (salaireFxBtn.isSelected()) {
salPanel.setVisible(false);
}
}
});
layoutComponents();
setVisible(true);
}
private void layoutComponents() {
setLayout(new GridBagLayout());
GridBagConstraints gb = new GridBagConstraints();
// The rest is an implementation of the GridBagLayout to design the interface
}
}
Here is the Layout
private void layoutComponents() {
setLayout(new GridBagLayout());
GridBagConstraints gb = new GridBagConstraints();
gb.fill = GridBagConstraints.NONE;
gb.weightx = 1;
gb.weighty = 1;
gb.insets = new Insets(7, 7, 7, 7);
// LINE 01 //
gb.gridx = 0;
gb.gridy = 0;
add(matriculeLab, gb);
gb.gridx++;
add(matriculeTxt, gb);
gb.gridx++;
add(nomLab, gb);
gb.gridx++;
add(nomTxt, gb);
// LINE 02 //
gb.gridx = 0;
gb.gridy = 1;
add(serviceLab, gb);
gb.gridx++;
add(serviceDrBx, gb);
gb.gridx++;
add(fonctionLab, gb);
gb.gridx++;
add(fonctionDrBx, gb);
// LINE 03 //
gb.gridx = 0;
gb.gridy++;
add(dateEmbLab, gb);
gb.gridx++;
add(dateEmb, gb);
// LINE 04 //
gb.gridx = 0;
gb.gridy++;
add(btnPanel, gb);
// LINE 05 //
gb.gridx = 0;
gb.gridy++;
add(salaireLab, gb);
gb.gridx++;
add(montatSalFix, gb);
// LINE 0 //
gb.gridx = 0;
gb.gridy++;
add(salPanel, gb);
// LINE 09 //
gb.gridx = 0;
gb.gridy++;
add(indemLab, gb);
gb.gridx++;
add(indemnite, gb);
}
Upvotes: 1
Views: 464
Reputation: 21
I solved my issue using CardLayout
I created a JPanel for the CardLayout it self and two other JPanels for each type of the salary
I used two action listeners on each radio button to switch to the next card
Upvotes: 1