I am trying to implement some buttons in my JTable. I have been looking at
this example.
What I don't understand is this constructor:
public ButtonEditor(JCheckBox checkBox) {
super(checkBox);
button = new JButton();
button.setOpaque(true);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fireEditingStopped();
}
});
}
What does the JCheckBox have to do with anything? There is no JCheckBox displayed anywhere nor does it seem it is even relevant to the example. TIA.
The DefaultCellEditor usage here is more of a hack for using Buttons as it accepts only JCheckBox, JComboBox and JTextField.
If you really want to implement for JButton, you can also do like,
class ButtonEditor extends AbstractCellEditor
implements javax.swing.table.TableCellEditor,
javax.swing.tree.TreeCellEditor
Else you can update your implementation for using a constructor with JButton as parameter or default constructor,
Approach 1
public ButtonEditor() {
super(new JCheckBox());
button = new JButton();
button.setOpaque(true);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fireEditingStopped();
}
});
}
and can be accessed as,
table.getColumn("Button").setCellEditor(
new ButtonEditor());
Approach 2
public ButtonEditor(JButton button) {
super(new JCheckBox());
this.button = button;
button.setOpaque(true);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
fireEditingStopped();
}
});
}
This approach provides better clarity and usage of button component outside the cell editor too,
JButton button=new JButton();
table.getColumn("Button").setCellEditor(
new ButtonEditor(button));
It is because class ButtonEditor extends DefaultCellEditor, and constructor of DefaultCellEditor in your example looks like this DefaultCellEditor​(JCheckBox checkBox)
Related
i saw an example of ActionListener use in code by implementing ActionListener.
but here i wanna use functionality of ActionListener by using Ref.Var. of ActionListener.
JButton createButton(){
ActionListener al;
JButton button = new JButton();
button.setBounds(130, 100, 100, 40);
button.setText("aaa");
button.setSize(100, 40);
button.setLayout(null);
frame.add(button);
return button;
}
look at ActionListener reference here . how to use this ref.var on button to listen event on button
JButton createButton(){
ActionListener al = new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
// run code;
}
};
JButton button = new JButton();
button.setBounds(130, 100, 100, 40);
button.setText("aaa");
button.setSize(100, 40);
button.addActionListener(al);
frame.add(button);
return button;
}
OR
jButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
//run code;
}
} );
It's basically exactly the same as having the containing class implement ActionListener: you provide an implementation, and you configure your button to listen to it.
I want to create a number of labels dynamically, so I found this code:
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel.add(new JLabel("Label"));
panel.validate();
}
});
It works great, but I can't change the text it's showing because I can't call it. For example like: label.setText("Labeltext Changed!");
So my question is: How can I give each dynamically created label a name, so I can change their values?
Store your labels in a List<JLabel>.
private List<JLabel> labels = new ArrayList<>();
...
public void yourMethod() {
...
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JLabel newLabel = new JLabel("Label");
labels.add(newLabel);
panel.add(newLabel);
panel.validate();
}
});
...
}
Then to get it just do something like labels.get(0).setText("my text");.
Note that you can shorten the ActionListener code with a lambda expression:
button.addActionListener(arg0 -> {
JLabel newLabel = new JLabel("Label");
labels.add(newLabel);
panel.add(newLabel);
panel.validate();
});
My code is:
public FactoryWindow()
{
getPreferredSize();
setTitle("Bounce");
JPanel buttonPanel = new JPanel();
add(comp, BorderLayout.CENTER);
addButton(buttonPanel, "Circle", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
comp.addShape();
}
});
addButton(buttonPanel, "Machine", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
comp.addMachine();
}
});
addButton(buttonPanel, "Close", new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
});
add(buttonPanel, BorderLayout.SOUTH);
pack();
}
This is a constructor. the class extends JFrame
public void addButton(Container c, String title, ActionListener listener)
{
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);
}
I want to be able to disable the Shape button when I press the machine button
How would I go about doing that?
I know there is Something like buttonName.setEnabled(false); but I cannot figure out how to use it in this context.
You will need a reference to the button you are trying to disable, this will require you to change your code slightly...
First, you need your addButton method to return the button it created...
public JButton addButton(Container c, String title, ActionListener listener) {
JButton button = new JButton(title);
c.add(button);
button.addActionListener(listener);
return button;
}
Then you need to assign the result to a variable...
JButton cirlce = null;
JButton machine = null;
cirlce = addButton(buttonPanel, "Circle", new ActionListener() {
public void actionPerformed(ActionEvent event) {
comp.addShape();
}
});
Then you can access it from your ActionListener...
machine = addButton(buttonPanel, "Machine", new ActionListener() {
public void actionPerformed(ActionEvent event) {
comp.addMachine();
circle.setEnabled(false);
}
});
Now, if you're using Java 6 (and I think Java 7), it will complain that the button should be final, but this won't work based on the way you have your code set up. Instead, you will need to make circle and machine instance fields in order to be able to access them from within the ActionListener context
I am trying to transit from a UserAdminPanel to AdminLogin within the same JPanel when I press the Admin button.
UserAdmin Panel
transit to AdminLogin Panel
The problem I have now is that I am opening up a new panel instead of changing the current panel to the new panel.
This is my code for the UserAdminPanel
public class SelectAdminUserPanel extends JPanel
{
public SelectAdminUserPanel()
{
setLayout(new GridLayout(3,1));
JButton b1 = new JButton("User Login");
JButton b2 = new JButton("Admin Login");
JButton b3 = new JButton("Exit");
b1.addActionListener(new SelectUserButtonListener() );
b2.addActionListener(new SelectAdminButtonListener());
b3.addActionListener(new SelectExitButtonListener() );
add(b1);
add(b2);
add(b3);
}
private class SelectAdminButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
AdminModule am = new AdminModule();
am.run();
}
}
private class SelectUserButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
GameModule gm = new GameModule();
gm.run();
}
}
private class SelectExitButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
}
}
}
This is the code for the AdminLogin Panel
public class AdminLoginPanel extends JPanel
{
AdminLoginPanel()
{
JLabel pwlabel = new JLabel("Password");
JPasswordField pwfield = new JPasswordField(20);
JButton loginbutton = new JButton("Login");
add(pwlabel);
add(pwfield);
add(loginbutton);
}
}
I have looked at the following example and this example but it's not very applicable because it talks about CardLayout instead of like rewriting the current JPanel.
I think that you should have a reference to your main frame and just remove the components from it based on the button pressed and add only the required components. From what you say, UserAdminPanel is your main panel. I think it's added to a frame for which you can obtain a reference. When you click a button, you want to remove all the content shown on it and display only what the button clicked should show. I think it should look something like this:
private class SelectAdminButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
frame.getContentPane().removeAll();
AdminModule am = new AdminModule();
frame.add(am.getNewPanel());
frame.pack();
// am.run(); //it's not clear what does for you
}
}
Where the method getNewPanel() would return the underlying JPanel. I'm assuming that AdminModule has a reference to the AdminLoginPanel.
I have a JFrame with three JButtons on it. I have set txtSearch (a JTextField component) to have the focus when JFrame loads. One of the buttons is set as the default button. This is my code:
private void formWindowOpened(java.awt.event.WindowEvent evt)
{
// btnRefresh.setMnemonic(KeyEvent.VK_R); // Even if this line
// is not commented, but
// still the event wouldn't fire.
this.getRootPane().setDefaultButton(btnRefresh);
}
When it loads, the button is just selected, but it did nothing when the Enter key was being pressed. How do I correctly implement it?
btnRefresh.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnRefreshActionPerformed(evt);
}
});
private void btnRefreshActionPerformed(java.awt.event.ActionEvent evt) {
JOptionPane.showMessageDialog(this, "Pressed!");
// Other codes here (Replace by JOptionPane)
}
What component has focus when the JFrame comes up? I ask because some components "eat" the Enter key event. For example, a JEditorPane will do that.
Also, when you assign an ActionListener to JTextField, the ActionListener will be called instead of the DefaultButton for the root pane. You must choose either to have an ActionListener or a DefaultButton, but you can't have both fire for the same JTextField. I'm sure this applies to other components as well.
I don't see what you are doing incorrectly from what is posted. Here is a short example that works. Perhaps it will reveal something useful to you.
import java.awt.BorderLayout;
public class ExampleFrame extends JFrame
{
private JPanel m_contentPane;
private JTextField m_textField;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
ExampleFrame frame = new ExampleFrame();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ExampleFrame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
m_contentPane = new JPanel();
m_contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
m_contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(m_contentPane);
m_textField = new JTextField();
m_contentPane.add(m_textField, BorderLayout.NORTH);
m_textField.setColumns(10);
JButton btnNewButton = new JButton("Default");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(ExampleFrame.this, "Default.");
}
});
m_contentPane.add(btnNewButton, BorderLayout.CENTER);
JButton btnNewButton_1 = new JButton("Not default");
btnNewButton_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JOptionPane.showMessageDialog(ExampleFrame.this, "Not default.");
}
});
m_contentPane.add(btnNewButton_1, BorderLayout.WEST);
m_textField.requestFocus();
getRootPane().setDefaultButton(btnNewButton);
}
}