You should be using revalidate
instead of repaint
.
You should also be relying on a LayoutManager
rather then using setSize
and setLocation
Question
I have an actionlistener inside another actionlistener when i create my jspinner inside the outer one it works but in the inner one it doesn't. this is my code.what would the problem be?
this is the code which doesn't work and if I put sp and js out of the inner action listener it works.
menuItem = new JMenuItem("Insert Exams", KeyEvent.VK_E);
menuItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
count = 0;
jt = new ArrayList[3];
for (int i = 0; i < 3; i++)
jt[i] = new ArrayList<JTextField>();
panel = new JPanel();
panel.setLocation(0, 0);
panel.setSize(d.width, d.height);
panel.setLayout(null);
JButton add = new JButton("add Exam");
add.setSize(120, 80);
add.setLocation(250, 100);
add.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
sp = new SpinnerNumberModel(1, 1, 5, 1);
js = new JSpinner(sp);
js.setSize(100, 30);
js.setLocation(450, 80 + count * 50);
panel.add(js);
for (int i = 1; i < 3; i++) {
JTextField jt1 = new JTextField(20);
jt1.setSize(150, 30);
jt1.setLocation(450 + i * 200, 80 + count * 50);
jt[i].add(jt1);
panel.add(jt1);
}
count++;
repaint();
}
});
panel.add(add);
setContentPane(panel);
}
});
Solution
You should be using revalidate
instead of repaint
.
You should also be relying on a LayoutManager
rather then using setSize
and setLocation