Question

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

Was it helpful?

Solution

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

OTHER TIPS

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

    }

   });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top