Click to See Complete Forum and Search --> : Java question!
Marcel2008
09-10-2001, 12:57 PM
Hello, i have some problems with java components and removing them from a contentpane.
Vector buttons = new Vector();
I have a method that fills a vector with jbuttons:
for (int i=0; i<10; i++)
{
buttons.add(new JButton(i));
}
And a method that displays it:
for (int i=0; i<10; i++)
{
JButton button = JButton buttons.get(i);
getContentPane().add(button);
}
These methods work.
However this crashes:
A remove method:
for (int i=0; i<10; i++)
{
JButton button = (JButton) buttons.get(i);
getContentPane().remove(button);
}
What's the problem here?
Thanks ;)
----edited by Strike----
code tags are nice :)
[ 10 September 2001: Message edited by: Strike ]
nanode
09-10-2001, 03:56 PM
How does it "crash"?
What's the error?
Why are you "removing" the components? Why not set them invisible? -> button.setVisible(false);
nanode
09-10-2001, 03:58 PM
also, with that remove()...
That looks fishy with new JButton declaration. Why not do:
//...for loop stuff
getContentPane().remove((JButton)buttons.get(i));
Marcel2008
09-10-2001, 04:00 PM
Try and see for yourself, this code just freezes.
There is no exception thrown.
I don't want to use setvisible, i want to generate 10 buttons, and add / remove them in code.
getContentPane().remove seems to be a problem...
Marcel2008
09-10-2001, 04:05 PM
Originally posted by nanode:
<STRONG>also, with that remove()...
That looks fishy with new JButton declaration
I know, i just wanted to make a quick example of the problem.
//...for loop stuff
getContentPane().remove((JButton)buttons.get(i));
</STRONG>
Marcel2008
09-10-2001, 04:33 PM
Never mind, i got it working this way:
import javax.swing.JButton;
import java.awt.event.*;
import java.util.Vector;
import java.awt.*;
public class exampleFrame extends javax.swing.JFrame implements ActionListener
{
private Vector button = new Vector();
private JButton addButton = new JButton("Add");
private JButton removeButton = new JButton("Remove");
public exampleFrame()
{
getContentPane().setLayout(new FlowLayout());
addButton.addActionListener(this);
removeButton.addActionListener(this);
getContentPane().add(addButton);
getContentPane().add(removeButton);
generateButtons();
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == addButton)
addButtons();
else
removeButtons();
}
public void generateButtons()
{
for (int i=0; i<10; i++)
{
button.add(new JButton(String.valueOf(i)));
}
}
public void addButtons()
{
for (int i=0; i<10; i++)
{
getContentPane().add((JButton)button.get(i));
}
}
public void removeButtons()
{
for (int i=0; i<10; i++)
{
getContentPane().remove((JButton)button.get(i));
}
}
public static void main(String args[])
{
exampleFrame ef = new exampleFrame();
ef.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
ef.setSize(600,100);
ef.setVisible(true);
}
}
You need to maximize the frame to see the buttons being added and removed.
Thanks