Reputation:
I am trying to make my WidgetViewer GUI to do different tasks when the button is clicked. For example, the create button will create an Event based on the inputs and print out the Events (can be more than 1), the reset button will reset everything, etc. This is what I have so far (please note my create button doesn't work as intended, it doesn't increment my events for some reason)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.*;
public class GUI {
JLabel eventName;
JLabel eventVenue;
JTextField name;
JTextField venue;
JButton create;
JButton sell;
JButton reset;
JButton sortDate;
JButton sortProfit;
JTextArea text;
//similar for rest
public GUI() {
WidgetViewer wv = new WidgetViewer();
eventName = new JLabel("Event Name");
name = new JTextField();
eventVenue = new JLabel("Event Venue");
venue = new JTextField();
//similar code for other inputs
text = new JTextArea("");
wv.add(eventName, 10, 30, 300, 20);
wv.add(name, 10, 50, 300, 20 );
wv.add(eventVenue, 10, 70, 300, 20);
wv.add(venue, 10, 90, 300, 20);
// similar for the rest
wv.add(text, 10, 470, 900, 20);
create = new JButton("Create an Event");
reset = new JButton("Reset List");
sortDate = new JButton("Sort by Date");
sortProfit = new JButton("Sort by Profit");
wv.add(create, 10, 320, 300, 20);
wv.add(reset, 10, 380, 300, 20);
wv.add(sortDate, 10, 410, 300, 20);
wv.add(sortProfit, 10, 440, 300, 20);
Inner action = new Inner();
create.addActionListener(action);
sell.addActionListener(action);
reset.addActionListener(action);
sortDate.addActionListener(action);
sortProfit.addActionListener(action);
}
class Inner extends WidgetViewerActionEvent {
public Inner() {}
@Override
public void actionPerformed(ActionEvent e) {
String eventName = name.getText();
String eventVenue = venue.getText();
int venueCapacity = Integer.parseInt(capacity.getText());
String eventDate = date.getText();
int ticketsSold = Integer.parseInt(sold.getText());
int ticketPrice = Integer.parseInt(price.getText());
int overhead = Integer.parseInt(costs.getText());
ArrayList<Event> allEvents = new ArrayList<Event>();
Event event = new Event(eventName, eventVenue, venueCapacity, eventDate, ticketsSold, ticketPrice, overhead);
allEvents.add(event);
String eventsText = "";
for (int i = 0; i < allEvents.size(); i ++) {
eventsText = eventsText + allEvents.toString();
}
text.setText(eventsText);
name.setText("");
venue.setText("");
capacity.setText("");
date.setText("");
sold.setText("");
price.setText("");
costs.setText("");
if (e.getActionCommand().contains("Reset List")) {
text.setText("");
}
//else ifs if that's the right approach?
}
}
}
Upvotes: 0
Views: 155
Reputation: 1576
now there are several options:
you could get the calling object from which the event originated or use action commands.
personally i like to create a controller class that responds to the GUI in which i have several methods of the following structure:
public ActionListener getFirstButtonActionListener() {
return new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//do somethong
}
};
}
public ActionListener getSecondButtonActionListener() {
return new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//do somethong
}
};
}
now you just add each function to the correct button this way there is no confusion
create.addActionListener(getController().getFirstButtonActionListener());
reset.addActionListener(getController().getSecondButtonActionListener());
EDIT:
for those who have a hard time realizing where the getController() comes from and what it should do - we split the view and the actions performed when pressing buttons to 2 classes. the first is the original class WidgetViewer the second is the WidgetController class
i think i would get reed of most of the gui class but that is a suggestion (which i`m going to show here).
anyhow the WidgetController should accept a variable in the constructor for the WidgetViewer object or the gui object depending on what it should be able to change. so:
public class WidgetController
{
private WidgetViewer view;
public WidgetController (WidgetViewer view)
{
this.view=view;
}
//now the listener functions
public ActionListener getResetButtonActionListener()
{
return new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
//do somethong
}
};
}
}
inside the WidgetViewer (or in your code unless you change it inside the gui class) you have a class variable for the controller. like so:
//the widget handles itself its not just a panel otherwise its a useless class
public class WidgetViewer extends JPanel
{
JLabel eventName;
JLabel eventVenue;
JTextField name;
JTextField venue;
JButton create;
JButton sell;
JButton reset;
JButton sortDate;
JButton sortProfit;
JTextArea text;
//this would hold the controller object
WidgetController controller;
public WidgetViewer() {
//create a controller that knows this view
this.controller=new WidgetController(this);
initGui();
}
public WidgetController getController()
{
return this.controller;
}
public initGui()
{
eventName = new JLabel("Event Name");
name = new JTextField();
eventVenue = new JLabel("Event Venue");
venue = new JTextField();
//similar code for other inputs
text = new JTextArea("");
this.add(eventName, 10, 30, 300, 20);
this.add(name, 10, 50, 300, 20 );
this.add(eventVenue, 10, 70, 300, 20);
this.add(venue, 10, 90, 300, 20);
// similar for the rest
this.add(text, 10, 470, 900, 20);
create = new JButton("Create an Event");
reset = new JButton("Reset List");
sortDate = new JButton("Sort by Date");
sortProfit = new JButton("Sort by Profit");
wv.add(create, 10, 320, 300, 20);
wv.add(reset, 10, 380, 300, 20);
wv.add(sortDate, 10, 410, 300, 20);
wv.add(sortProfit, 10, 440, 300, 20);
//each of the methods should be created inside the widgetController class
create.addActionListener(getController().getCreateButtonActionListener());
sell.addActionListener(getController().getSellButtonActionListener());
reset.addActionListener(getController().getResetButtonActionListener());
sortDate.addActionListener(getController().getSortDataButtonActionListener());
sortProfit.addActionListener(getController().getSortProfitButtonActionListener());
}
}
last the GUi class turns into main class and isntantiate the frame and the widget:
public class Gui //turns into your main class only creates the frame and the widget
{
public static void main(String[] args) throws IOException {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
WidgetViewer view=new WidgetViewer();
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(view);
frame.setTitle("widget");
frame.pack();
frame.setVisible(true);
frame.setExtendedState(Frame.MAXIMIZED_BOTH);
}
});
}
}
Upvotes: 2