Reputation: 1
I've tried checking out about 5 different tutorials for WindowBuilder, and while I can get an application window to appear when I test the code, I cannot get the buttons to work. This is the code I've made based on the tutorials I've seen.
package guiTest;
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class GuiTest {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GuiTest window = new GuiTest();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public GuiTest() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
frame.getContentPane().setLayout(gridBagLayout);
JButton btnLabelAdd = new JButton("Label Add");
GridBagConstraints gbc_btnLabelAdd = new GridBagConstraints();
gbc_btnLabelAdd.insets = new Insets(0, 0, 5, 0);
gbc_btnLabelAdd.gridx = 6;
gbc_btnLabelAdd.gridy = 1;
frame.getContentPane().add(btnLabelAdd, gbc_btnLabelAdd);
btnLabelAdd.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
makeLabel(evt);
}
});
}
private void makeLabel(ActionEvent evt) {
JLabel lblHereLabel = new JLabel("Here Label");
GridBagConstraints gbc_lblHereLabel = new GridBagConstraints();
gbc_lblHereLabel.gridx = 6;
gbc_lblHereLabel.gridy = 4;
frame.getContentPane().add(lblHereLabel, gbc_lblHereLabel);
}
}
Does anyone have any tips to get this to work? Or some example code I could use to make sure the problem is not with Eclipse? I'm assuming here that the button should make the label when the button is left clicked; is that correct?
Upvotes: 0
Views: 97
Reputation: 10945
Your button works fine. You can watch the code jump into makeLabel()
in a debugger. You just aren't making the new label visible.
As suggested by @MadProgrammer, once you've added the new label, you can tell the enclosing Container
to update itself by calling revalidate()
, and then repaint()
to have it redraw itself:
private void makeLabel(ActionEvent evt) {
JLabel lblHereLabel = new JLabel("Here Label");
GridBagConstraints gbc_lblHereLabel = new GridBagConstraints();
gbc_lblHereLabel.gridx = 6;
gbc_lblHereLabel.gridy = 4;
frame.getContentPane().add(lblHereLabel, gbc_lblHereLabel);
frame.getContentPane().revalidate();
frame.getContentPane().repaint();
}
Doing this means that you've divorced yourself from needing to access frame
directly. This means that you can rewrite your handler to be "frame agnostic", by querying the button that fired the event for the Container
it belongs to, and just add the new label to it, like so:
private void makeLabel(ActionEvent evt) {
JLabel lblHereLabel = new JLabel("Here Label");
GridBagConstraints gbc_lblHereLabel = new GridBagConstraints();
gbc_lblHereLabel.gridx = 6;
gbc_lblHereLabel.gridy = 4;
Container displayArea = ((JButton) evt.getSource()).getParent();
displayArea.add(lblHereLabel, gbc_lblHereLabel);
displayArea.revalidate();
displayArea.repaint();
}
Upvotes: 1