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
}
});
Related
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?
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.
I am extremely new to programming with Java and I require assistance on how I may be be able to implement an ON button for my calculator.
I already have a user interface setup with all the numbers and plus/minus buttons using Netbeans. Everything's working as it should but how do I add an ON button?
Basically the calculator would not work till the ON button is pressed.
I've pasted the code here:
http://pastebin.com/1Tfmb7wN
Add a field
on=false;
then add
Button onButton = new Button();
//some formatting;
onButton.setOnAction(new EventHandler<ActionEvent>() {
public void handle(ActionEvent event) {
on=true;
}
});
finally an if statement surrounding the insides of your other handles
if(on){
//do something
}
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.
I am creating a GUI in which my home page has a button labelled "Welcome to the Panel"
The point is that when you press on this button, it will navigate to a new page where I will have other functions. My only problem is that I dont know the syntax or how that when clicking a button, it will navigate to new page.
JButton btn = new JButton("Welcome to the Panel");
btn.setActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
// Here you open the other window. You can use JFrame, JOptionPane or JDialog
}
});
button.addActionListener(new ActionListner()
{
public void actionPerformed(ActionEvent ae)
{
//code to show pane
}
});
You need to register an ActionListener on your button and inside that action listener you make that panel (the page) visible.
How you do that depends on your layout, i.e. with a CardLayout you'd show the corresponding card (here's the doc). Using other layouts you might have to replace a component, e.g. if you use a BorderLayout and your content is placed in the center, replace the center component with the panel you want to show.
Note that if you're not familiar with layout managers yet, you should first have a look at those before doing dynamic changes to the ui (like navigation etc.).