examples
Class Example6
java.lang.Object
examples.Example6
- All Implemented Interfaces:
- java.awt.event.ActionListener, java.util.EventListener
public class Example6
- extends java.lang.Object
- implements java.awt.event.ActionListener
An example that illustrates how to add or remove components. On launching the first image appears with two buttons. When the Remove Cancel
button is pressed, the Cancel button disappears as is shown in the second image, and the text in the first button changes to Add Cancel. The window changes to the first image when the Add Cancel button is pressed.
private JButton cancel;
private JButton mutate;
private Cell cancelCell;
private boolean cancelShown;
private Container container;
private Row mainRow;
private PageLayout lay=null;
public Example6()
{
// The main frame.
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
container =frame.getContentPane();
// The components
mutate=new JButton("Remove Cancel");
cancel=new JButton("Cancel");
mutate.addActionListener(this);
// The main row.
cancelCell=new ComponentCell(cancel);
mainRow=new Row(mutate);
mainRow.add(cancelCell);
cancelShown=true;
// Construct the layout
lay=mainRow.createLayout(container);
frame.pack();
frame.setLocation(300,300);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(cancelShown)
{
//Remove the Cancel button and change text.
mainRow.removeComponent(cancel);
mutate.setText("Add Cancel");
}
else
{
//Add the Cancel button and change text.
mainRow.add(cancelCell);
cancelCell.addComponentsToContainer(container);
mutate.setText("Remove Cancel");
}
mainRow.invalidate();
cancelShown=!cancelShown;
container.validate();
container.repaint();
}
Method Summary |
void |
actionPerformed(java.awt.event.ActionEvent e)
|
static void |
main(java.lang.String[] args)
|
static void |
run()
|
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Example6
public Example6()
actionPerformed
public void actionPerformed(java.awt.event.ActionEvent e)
- Specified by:
actionPerformed
in interface java.awt.event.ActionListener
main
public static void main(java.lang.String[] args)
run
public static void run()