1

When a particular button is clicked I want another set of buttons to be added to the Panel, however at the moment when I do this, I can add them as many times as I want, I need this to be only possible once. Would the best way to do this be set the adding of the buttons and fields in a while loop?

   if(e.getSource() == selectScript){

                while(scriptB < 1 ){
                imageID = new JTextField("INT");
                imageDescription = new JTextField("imgDescription");
                imagePath = new JTextField("imagePath");
                manageImageTab.add(imageID);
                manageImageTab.add(imageDescription);
                manageImageTab.add(imagePath);
                insertImage = new JButton("Add an Image");
                insertImage.addActionListener(new dbaccess());
                manageImageTab.add(insertImage);

                manageImageTab.revalidate();
                validate();
                scriptB++;

                }
}
Peddler
  • 6,045
  • 4
  • 18
  • 22

2 Answers2

3

Perhaps rather than add and remove the JButtons, you could add the buttons once at the code start, just don't make them visible until you need them, or perhaps better place them all on a JPanel that is not visible and then made visible when desired. Just don't forget to call revalidate() and repaint() on the container that holds the buttons and their panel.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • I like that idea of adding once, and setting visible when required. I take it, keeping them in a while loop is bad idea/not good practice? – Peddler Mar 10 '12 at 22:58
  • @Peddler: I have no idea of "keeping them in a while loop" is a bad idea as I don't have much of a clue as to what you mean by this. – Hovercraft Full Of Eels Mar 10 '12 at 23:06
  • Sorry, poorly phrased. The adding of the buttons is in a while loop, which is not accessed once the button has been clicked once. – Peddler Mar 10 '12 at 23:09
  • @Peddler: I'm still unsure about what you mean. If you still need help with this, you might want to edit your question and show what it is exactly that you mean. – Hovercraft Full Of Eels Mar 10 '12 at 23:10
  • I updated question to include the while loop I was talking about, hopefully it's clear now. It works, but whether it's good practice Im not so sure, – Peddler Mar 10 '12 at 23:17
  • 1
    Ah, I see what you mean. That could work, but seems to me that it would be easy to mess up. Better to have the components as class fields, have them created just once in the application and either adding them or making them visible. – Hovercraft Full Of Eels Mar 10 '12 at 23:20
  • Thanks for your advice, It seems nicer to do it by setting them visible. Seems easier to manage! Thanks – Peddler Mar 10 '12 at 23:21
1

If I understand you correctly, I would use a flag alreadyAdded that starts out false, gets set to true after the controls have been added, then don't allow it to add after that.

vextorspace
  • 934
  • 2
  • 10
  • 25