1

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);

        }
    });
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Paniz
  • 594
  • 6
  • 19
  • 1
    Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Jul 29 '13 at 01:56

1 Answers1

2

You should be using revalidate instead of repaint.

You should also be relying on a LayoutManager rather then using setSize and setLocation

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366