I am trying to have my interface dynamically generate a customized button when I click a button. I searched several answers like this, but somehow it does not work. Is there any mistake with my current code below?
public class MainWindow {
private JFrame frame;
private JPanel panel;
private JPanel panel_1;
private JPanel panel_2;
private JSplitPane splitPane;
private JButton btnSearch;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
MainWindow window = new MainWindow();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public MainWindow() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 645, 438);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel = new JPanel();
frame.getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
splitPane = new JSplitPane();
panel.add(splitPane);
panel_1 = new JPanel();
splitPane.setLeftComponent(panel_1);
btnSearch = new JButton("Search");
GridBagConstraints gbc_btnSearch = new GridBagConstraints();
gbc_btnSearch.gridx = 0;
gbc_btnSearch.gridy = 10;
panel_1.add(btnSearch, gbc_btnSearch);
panel_2 = new JPanel();
splitPane.setRightComponent(panel_2);
btnSearch.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
addButton();
}
});
}
protected void addButton() {
MyButton hahaButton = new MyButton("haha");
panel_2.add(hahaButton);
panel_2.add(new JButton());
panel_2.revalidate();
panel_2.repaint();
}
And this is the definition of the MyButton:
public class MyButton extends JButton {
private static final long serialVersionUID = 1L;
private Color circleColor = Color.BLACK;
public MyButton(String label) {
super(label);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Dimension originalSize = super.getPreferredSize();
int gap = (int) (originalSize.height * 0.2);
int x = originalSize.width + gap;
int y = gap;
int diameter = originalSize.height - (gap * 2);
g.setColor(circleColor);
g.fillOval(x, y, diameter, diameter);
}
@Override
public Dimension getPreferredSize() {
Dimension size = super.getPreferredSize();
size.width += size.height;
return size;
}
}
I just tried your sourcecode and it works as expected: everytime I klick the search button on the left side of the split-pane, 2 buttons are added to the panel on the right side of the panel (one with the black filled circle and a button without a label).
What doesn't work for you? I'm using java 1.6 on Mac OSX, but this should work with earlier versions on other platforms as well......
As Dieter Rehbein pointed out, the code you have does compile and run.
However, it was rather sloppy and convoluted, as if you copied and pasted different sources together.
I took a few minutes and cleaned it up some, hope it helps.
public class MainWindow { public static void main( String[] args ) { new MainWindow(); } public MainWindow() { // Create the split pane JSplitPane jSplitPane = new JSplitPane(); final JPanel leftPanel = new JPanel(); final JPanel rightPanel = new JPanel(); jSplitPane.setLeftComponent( leftPanel ); jSplitPane.setRightComponent( rightPanel ); // Create the button JButton jButton = new JButton( "Generate" ); leftPanel.add( jButton ); jButton.addMouseListener( new MouseAdapter() { @Override public void mouseClicked( MouseEvent e ) { addButtons( rightPanel ); } } ); // Create the panel JPanel jPanel = new JPanel(); jPanel.setLayout( new BoxLayout( jPanel , BoxLayout.X_AXIS ) ); jPanel.add( jSplitPane ); // Create the frame JFrame jFrame = new JFrame(); jFrame.setBounds( 100 , 100 , 645 , 438 ); jFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); jFrame.getContentPane().add( jPanel , BorderLayout.CENTER ); // Show the frame jFrame.setVisible( true ); } private void addButtons( JPanel jPanel ) { addButton( jPanel , "Default" , null ); addButton( jPanel , "Red" , Color.RED ); addButton( jPanel , "Yellow" , Color.YELLOW ); addButton( jPanel , "Blue" , Color.BLUE ); addButton( jPanel , "Green" , Color.GREEN ); } protected void addButton( JPanel jPanel , String label , Color color ) { jPanel.add( new MyButton( label , color ) ); jPanel.revalidate(); jPanel.repaint(); } public class MyButton extends JButton { private static final long serialVersionUID = 1L; private Color color = null; public MyButton( String label , Color color ) { super( label ); this.color = color; } @Override protected void paintComponent( Graphics graphics ) { super.paintComponent( graphics ); if ( color != null ) { Dimension dimension = super.getPreferredSize(); int gap = ( int ) ( dimension.height * 0.2 ); int diameter = dimension.height - ( gap * 2 ); graphics.setColor( color ); graphics.fillOval( dimension.width + gap , gap , diameter , diameter ); } } @Override public Dimension getPreferredSize() { Dimension size = super.getPreferredSize(); size.width += size.height; return size; } } }