ActionListener from controller doesn't trigger - java

I have an MVC with swing project with two views, a model and a controller.
The controller has as fields a list of models, and a list of views.
I tried to add a listener on a button from one of my views:
private void initializeNewOrderListeners() {
NewOrderView view = (NewOrderView) views.get(0);
JButton addOrderBtn = view.getAddOrderBtn();
addOrderBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("test");
}
});
}
I made sure that I don't have other order listeners in my NewOrderView class, however, the action is not getting triggered when I press the button.
I moved the code from the controller to the NewOrderView class, where I created the button and everything works normal when I press it.
What am I missing? Why the ActionListener is not getting registred from the Controller?

Related

Setting Actor To Listen For Button Click In LibGDX

Background Information: I am currently working in a Dialog class I have extended for my game. Inside of this dialog's content table I have both an Image and a Table (lets call it ioTable). Inside of ioTable I have a combination of both Labels and TextFields. The idea is that the dialog becomes a sort of form for the use to fill out.
Next, inside of the Dialog's button table, I want to include a "Clear" TextButton (clearButton). The idea that clearButton will clear any values written to the TextFields of ioTable.
My Question: Is is possible to add a listener to each of the TextFields of ioTable that will trigger when clearButton is pressed. As always, any other creative solution is more than welcome.
You could just give the EventListener a reference to the table you want to clear:
// Assuming getSkin() and ioTable are defined elsewhere and ioTable is final
TextButton clearButton = new TextButton("Clear", getSkin());
clearButton.addListener(new EventListener() {
#Override
public boolean handle(Event event) {
for(Actor potentialField : table.getChildren()) {
if(potentialField instanceof TextField) {
((TextField)potentialField).setText("");
}
}
return true;
}
});
// Add clearButton to your dialog
If you see yourself creating multiple clearButtons, you could easily wrap this in a helper method or extend TextButton.

How to create a general action that changes tabs in GUI?

I have several actionListeners throughout my code for when I push a button, it changes tabs between the ones I have.
However, I would like to create a general action that depending on which button was pressed (through an int), it changed to a different tab. This is the current actionListener I have.
JButton btnSaveAddESS = new JButton("Save");
btnSaveAddESS.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
tabbedBackground.setSelectedIndex(0);
tabbedBackground.setEnabledAt(1, false);
}
});
With this, I would like to create a general action, however , while creating the action as a different class, I am not able to access the TabbedPane (tabbedBackground) component.
How can I implement this, avoiding actionListeners?
Thanks,
Nhekas
changeTab(int i){
tabbedBackground.setSelectedIndex(i);
tabbedBackground.setEnabledAt(i, false);
}
public void actionPerformed(ActionEvent e) {
int i = Integer.parseInt(Jbutton.getText());
changeTab(int i);
}
what you need is actually a method which handles the operation pass an int to the method changetab and the method will change the selectedTab

How to restart a Java Application launching a specific class of my application?

I have the following situatation:
I have a Java Swing application.
In the class that implement my GUI I have a button named Log Out tath is binding to an event listener that handle the click event, something like it:
JButton logOutButton = new JButton("LogOut");
header.add(logOutButton);
Then in the same class that implement my GUI I have declared the ActionListener that handle this event using an inner class:
logOutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("logOutButton clicked !!!");
System.exit(0);
}
});
In this moment when I click the logOutButton button the program end. I would that instead exit it is restarted by running a specific class called LoginForm (the class that implement the login form GUI)
What can I do to do this thing?
Tnx
Andrea
You don't really need to close/open window junky approach at all. Just use Card Layout:
set Frame's content pane's layout to card layout.
getContentPane().setLayout(new CardLayout());
Put your different Form's content code inside different panel and
add them to the content pane with their corresponding name, for example:
getContetnPane().add(logInFormPanel, "logIn Form");
Now you can simulate the card to appear whenever necessary by calling CardLayout.show(Container parent, String name). For example:
logOutButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.out.println("logOutButton clicked !!!");
CardLayout cl = (CardLayout)(getContentPane().getLayout());
cl.show(getContentPane(), "logIn Form");
}
});
Check out a CardLayout demo from my another answer.

one button and two differents views

I'm building my application applying MVC pattern.Following this guide mvc guide, I would make an application made of a button.when I press button appear me another view when I repress the button appear me the previously view.how can I made ?some advices?
Well Button will act as the Controller here........
If you want always to show the same View again and again, by repressing the Button, use Singleton Principle
If not, you can initialize a new View again, from within the onClick() method of ActionListener...
Edited:
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Execute when button is pressed
new Frame(); // Creates a new frame
}
});

Java - JButton text disappears if actionPerformed defined afterwards

This has been bugging me for a while. If I define setText on a JButton before defining setAction, the text disappears:
JButton test = new JButton();
test.setText("test"); // Before - disappears!
test.setAction(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
// do something
}
});
this.add(test);
If it's after, no problems.
JButton test = new JButton();
test.setAction(new AbstractAction() {
public void actionPerformed(ActionEvent e) {
// do something
}
});
test.setText("test"); // After - no problem!
this.add(test);
Furthermore, if I set the text in the JButton constructor, it's fine! Yarghh!
Why does this happen?
As described in the documentation:
Setting the Action results in immediately changing all the properties
described in Swing Components Supporting Action.
Those properties are described here, and include text.
Have a look at
private void setTextFromAction(Action a, boolean propertyChange)
in AbstractButton. You can see it's calling setText() based on the action.
It looks like you can call setHideActionText(true); to sort out your problem.
This is because Action has name for the control as well. Since you are not setting any name in the Action it is getting set to empty string.
1) Listeners put all Events to the EDT,
2) all events are waiting in EDT and output to the screen would be done in one moment
3) you have to split that to the two separate Action inside Listener
setText()
invoke javax.swing.Timer with Action that provide rest of events inside your original ActionListener
If you only want to handle the event, you don't need Action. You can add an ActionListener:
JButton test = new JButton();
test.setText("test");
test.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// do something
}
});
this.add(test);
Calling setAction overrides pre-set text.

Categories