In my project I have two classes that generate Panels. I am using Inheritance:
The parent class is calledGodPanel
package Trivia.Secciones;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import net.miginfocom.swing.MigLayout;
public class GodPanel extends JPanel {
private boolean end;
private int i;
private String paneles[], seccion[];
public GodPanel(){
setBackground(Color.BLACK);
setLayout(new CardLayout());
end = false;
}
public void showCard(String val){
CardLayout cl = (CardLayout)this.getLayout();
cl.show(this, val);
}
public void addCard(Component obj,String name){add(obj,name);}
public void removeAll(String[] name){removeAll();}
public void inicializarPaneles(String[] obj){
paneles = null;
paneles = obj;
}
public void inicializarSecciones(String[] obj){
seccion = null;
seccion = obj;
}
public JPanel createSlide(String url) throws IOException{
JPanel panel = new JPanel(new MigLayout()){
Image image = ImageIO.read(getClass().getResourceAsStream(url));
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
};
panel.setBackground(Color.BLACK);
panel.setPreferredSize(new Dimension(965,600));
return panel;
}
public Component getBasicComponents(int op){
switch (op) {
case 1:
JButton Bnext = new JButton(">>>");
Bnext.setPreferredSize(new Dimension(50,20));
Bnext.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if(i != paneles.length - 1){
i++;
showCard(paneles[i]);
if(i == paneles.length - 1){
end = true;
}
}
}
});
return Bnext;
case 2:
JButton Bant = new JButton("<<<");
Bant.setPreferredSize(new Dimension(50,20));
Bant.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
if(i != 0){
i--;
showCard(paneles[i]);
}
}
});
return Bant;
case 3:
JComboBox secciones = new JComboBox();
secciones.setPreferredSize(new Dimension(515,28));
for(String sec: seccion){secciones.addItem(sec);}
return secciones;
default:
break;
}
return null;
}
public void enableComponents(Container container, boolean enable, int option) {
Component[] components = container.getComponents();
for (Component component : components) {
switch(option){
case 1:
component.setEnabled(enable);
break;
case 2:
component.setVisible(enable);
break;
}
if (component instanceof Container) {
enableComponents((Container)component, enable, option);
}
}
}
public void setPanelInicial(){showCard(paneles[0]); i=0;}
public void setEnd(boolean v){end = v;}
public boolean getEnd(){return end;}
}
And this is a child class calledNivel3
package Trivia.Secciones;
import java.awt.Dimension;
import java.awt.Image;
import java.io.IOException;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Nivel3 extends GodPanel{
private String[] nombres = {"A1","P2"};
private JPanel[] paneles;
private String[] sec = {"Centros Educativos","Situación Estructural y Equipamento de Planes",
"Cantidad de Estudiantes por Niveles","Cantidad de Educadores por Niveles y Relacion Alumno/Educador",
"Centros de Promocion Cultural","Grupos Culturales Representativos",
"Instalaciones Deportivas y Condiciones"};
public Nivel3() throws IOException{
paneles = new JPanel[2];
inicializarPaneles(nombres);
inicializarSecciones(sec);
for(int i=0;i<paneles.length-1;i++){
paneles[i] = createSlide("/assets/FondoEdu.png");
paneles[i].add(getBasicComponents(2), "split 4");
paneles[i].add(getBasicComponents(1),"gapleft 10");
paneles[i].add(new JLabel("Acceso Rapido: "),"gapleft 200");
paneles[i].add(getBasicComponents(3),"wrap 20");
if(i == (paneles.length - 1)){
enableComponents(paneles[i],false,1);
}
for(String names: nombres){
add(paneles[i],names);
}
}
}
}
Ok, my "problem" (I put it in quotes, because the method works fine) is in the enableComponents
. In the child class you notice that I create panels using loops for
. Well, I'm trying to always make the last panel have ALL components disabled. I inserted a condition that, for unknown reasons, does not want to work:
if(i == (paneles.length - 1)) {
enableComponents(paneles[i],false,1);
}
Could you give me ideas on how to disable everything only in the last panel if this cannot be fixed?
The real problem with it all... was the for loop statement itself (especially in the loop condition):
1) I made a bigger JPanels array in this test.
2) When using the function
.length
, it returned me, as expected, the number 4. As is already known, the arrays go from 0 onwards... so my "number 4" would be 3.The for loop as I initially wrote it counted up to 2, since the condition says "Run the loop while the count variable ( i ) is less than 3":
The only thing I had to do was simply add an equal to the condition (<=) or remove the - 1 from the condition... and things change, now it tells the for loop "Run the loop while the counter variable (i) is less than 4 ":
And now if you grab the condition:
At first glance the code looks fine, although calling
setEnabled(false)
inContainer
has no effect other than ensuring that itgetEnabled()
results infalse
, if I remember correctly.Try the following: