-1

I asked a question similar to this one some months ago: here the problem was solved but as soon as I add my menuBar to this code the scrollPane disappeared again. this is my new code :

public class Question {

int count = 0;
ArrayList<JTextField>[] jt;

public Question() {
    final JFrame f = new JFrame();
    f.setLayout(new BorderLayout());
    f.setResizable(false);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    JMenuBar mnuBar = new JMenuBar();

    JMenu mnu1 = new JMenu("ثبت");
    mnu1.setMnemonic(KeyEvent.VK_E);

    JMenuItem menuItem = new JMenuItem("Insert Places", KeyEvent.VK_T);
    menuItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {

            count = 0;
            jt = new ArrayList[4];
            for (int i = 0; i < 4; i++)
                jt[i] = new ArrayList<JTextField>();


    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createLineBorder(Color.red));
    panel.setLayout(new BoxLayout(panel, 1));
    for (int i = 0; i < 100; i++) {
        panel.add(new JButton("kjdh"));
    }

    JScrollPane scrollPane = new JScrollPane(panel);
    f.getContentPane().add(scrollPane);
        }
    });

    mnu1.add(menuItem);
    mnuBar.add(mnu1);
    f.setJMenuBar(mnuBar);
    f.pack();
    f.setExtendedState(JFrame.MAXIMIZED_BOTH);
    f.setVisible(true);
}

public static void main(String[] args) {
    Runnable r = new Runnable() {
        @Override
        public void run() {
            new Question();
        }
    };
    SwingUtilities.invokeLater(r);
} 
}

thanks in regards

Community
  • 1
  • 1
Paniz
  • 594
  • 6
  • 19

2 Answers2

1

but as soon as I add my menuBar to this code the scrollPane disappeared again. this is my new code :

Because you are not adding the JScrollPane to content pane before any action is performed on the MenuItem.

  1. Declare JScrollPane and JPanel in your class context instead of in a function.
  2. Add the JScrollPane and the JPanel which is to contain your JButtons
  3. When an action event is performed on the MenuItem add the button to JPanel and call revalidate() on it.
  4. Calling validate()/revalidate() on JFrame's content pane is not recommended by me. call revalidate() only on the component whose layout got changed: in your context it is the panel which contains the JButton.
  5. And as a general rule don't forget to call repaint() too.

So, the complete solution of yours would become:

class Question {

int count = 0;
ArrayList<JTextField>[] jt;
JPanel buttonPanel;;
JScrollPane scrollPane;

public Question() {
    final JFrame f = new JFrame();
    f.setLayout(new BorderLayout());
    f.setResizable(false);
    f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    JMenuBar mnuBar = new JMenuBar();

    JMenu mnu1 = new JMenu("ثبت");
    mnu1.setMnemonic(KeyEvent.VK_E);

    buttonPanel = new JPanel();
    buttonPanel.setBorder(BorderFactory.createLineBorder(Color.red));
    buttonPanel.setLayout(new BoxLayout(buttonPanel, 1));

    scrollPane  = new JScrollPane(buttonPanel);
    f.getContentPane().add(scrollPane);

    JMenuItem menuItem = new JMenuItem("Insert Places", KeyEvent.VK_T);
    menuItem.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent arg0) {

            count = 0;
            jt = new ArrayList[4];
            for (int i = 0; i < 4; i++)
                jt[i] = new ArrayList<JTextField>();


           for (int i = 0; i < 100; i++) {
                buttonPanel.add(new JButton("kjdh"));
           }

           buttonPanel.revalidate(); // calling revalidate 
           buttonPanel.repaint();// calling repaint

        }
    });

    mnu1.add(menuItem);
    mnuBar.add(mnu1);
    f.setJMenuBar(mnuBar);
    f.pack();
    f.setExtendedState(JFrame.MAXIMIZED_BOTH);
    f.setVisible(true);
}

public static void main(String[] args) {
    Runnable r = new Runnable() {
        @Override
        public void run() {
            new Question();
        }
    };
    SwingUtilities.invokeLater(r);
} 
}
Sage
  • 15,290
  • 3
  • 33
  • 38
0

Add f.revalidate(); at ActionListener.

 menuItem.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
    .................

    JScrollPane scrollPane = new JScrollPane(panel);
    f.getContentPane().add(scrollPane);

    f.revalidate();  // Add this method with JFrame

    }

   });
Masudul
  • 21,823
  • 5
  • 43
  • 58
  • my java doesn't recognize `revalidate()` what about `validate()`? – Paniz Nov 28 '13 at 11:39
  • 1
    If you use less than JDK-1.7 than use `validate()`. – Masudul Nov 28 '13 at 11:41
  • in this code it works because it must add it from the beginnig but in my complete code where it must add jscrollpane when items do not fit in the screen it doesn't work yet :( – Paniz Nov 28 '13 at 11:53
  • @Paniz, I don't know why it is not work your complete code. Note that, scrollpane will appear to screen when components got bigger than screen. – Masudul Nov 28 '13 at 11:57
  • does it appear dynamically? I mean when I add some components one by one(for example by pressing a button), as soon as they got bigger it appears? – Paniz Nov 28 '13 at 12:02
  • 1
    @Paniz, Yes, it appear dynamically. – Masudul Nov 28 '13 at 12:03