I reworked my project but im running into a problem with the lines below. For some reason im getting an output of nine components when there are infact eleven components contained within the JLayeredPane. These two lines are just for temporary
purposes to see if in fact i am getting the right component count.
Code:
int num = jlp.getComponentCountInLayer(jlp.highestLayer());
System.out.println(num);
Now for these two lines. The first one i am getting an incompatible type error which i don't seem to understand at all since all i am doing is setting the size of an array to what a method is returning. For the second line i am getting an Incovertible type error which i don't understand either since JComponent is a subclass of Component i should be able to cast up the inheritance hierarchy.
Code:
Component comps = new Component[jlp.getComponentCountInLayer(jlp.highestLayer())];
comps = (Component)jlp.getComponentsInLayer(jlp.highestLayer());
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class DataForm {
public static void main(String[] args){
int height = 40;
int width = 100;
int space = 15;
int x = 0;
int y = 10;
JTextField[] jtc = new JTextField[10];
JFrame jf = new JFrame("Data Form");
JButton jb = new JButton("Check Fields");
final JLayeredPane jlp = jf.getLayeredPane();
jf.setSize(500,400);
jb.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
int num = jlp.getComponentCountInLayer(jlp.highestLayer());
System.out.println(num);
// Component comps = new Component[jlp.getComponentCountInLayer(jlp.highestLayer())];
// comps = (Component)jlp.getComponentsInLayer(jlp.highestLayer());
}
});
for(int i = 0; i < jtc.length; ++i){
jtc[i] = new JTextField();
}
for(int i = 0; i < jtc.length; ++i){
if(x == 0){
x = 10;
jtc[i].setBounds(new Rectangle(x, y, width, height));
jlp.add(jtc[i]);
continue;
}
x += (width + space);
jtc[i].setBounds(new Rectangle(x, y, width, height));
jlp.add(jtc[i]);
if(x > (jf.getWidth() - (width + space))){
x = 10;
y += (height + space);
jtc[i].setBounds(new Rectangle(x, y, width, height));
jlp.add(jtc[i]);
}
}
jb.setBounds(new Rectangle((x + width + 10),y, width, height));
jlp.add(jb);
jf.setVisible(true);
}
}