T
theneb
Hello everyone, I'm attempting to create JComponents from when a
button is clicked. However I'm assuming I'm missing something? (Or
it's not possible)
This is some quick prototype code which shows my intentions.
import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;
public class myApplet extends Applet implements ActionListener{
private JPanel buttonPanel;
private HashMap<String,testButton> buttons;
private testButton myButton, secondButton, thirdButton;
public void init(){
buttonPanel = new JPanel();
buttons = new HashMap<String,testButton>();
buttons.put(String.valueOf(buttons.size()),new testButton());
buttonPanel.add(buttons.get(String.valueOf(buttons.size()-1)));
add(buttonPanel);
}//end init
public void start(){
System.out.println("START");
}
public void actionPerformed(ActionEvent event) {
System.out.println("Action");
}
private class testButton extends JButton implements ActionListener{
public testButton(){
setText("Button "+buttons.size());
addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
System.out.println("Adding New Button");
buttons.put(String.valueOf(buttons.size()),new testButton());
buttonPanel.add(buttons.get(String.valueOf(buttons.size()-1)));
}
}
}//end myApplet
Example here: http://www.theneb.co.uk/applet-test/test.html
Cheers
button is clicked. However I'm assuming I'm missing something? (Or
it's not possible)
This is some quick prototype code which shows my intentions.
import java.awt.*;
import javax.swing.*;
import java.applet.*;
import java.awt.event.*;
import java.util.*;
public class myApplet extends Applet implements ActionListener{
private JPanel buttonPanel;
private HashMap<String,testButton> buttons;
private testButton myButton, secondButton, thirdButton;
public void init(){
buttonPanel = new JPanel();
buttons = new HashMap<String,testButton>();
buttons.put(String.valueOf(buttons.size()),new testButton());
buttonPanel.add(buttons.get(String.valueOf(buttons.size()-1)));
add(buttonPanel);
}//end init
public void start(){
System.out.println("START");
}
public void actionPerformed(ActionEvent event) {
System.out.println("Action");
}
private class testButton extends JButton implements ActionListener{
public testButton(){
setText("Button "+buttons.size());
addActionListener(this);
}
public void actionPerformed(ActionEvent event) {
System.out.println("Adding New Button");
buttons.put(String.valueOf(buttons.size()),new testButton());
buttonPanel.add(buttons.get(String.valueOf(buttons.size()-1)));
}
}
}//end myApplet
Example here: http://www.theneb.co.uk/applet-test/test.html
Cheers