简体   繁体   中英

Java Swing: how do I use an unnamed button in another class?

I am currently trying to add functionality to some buttons as part of an assignment. the starter code for the assignment uses unnamed buttons and when trying to give these buttons a name results in an error.

Normally that wouldn't be too difficult to do as I understand how to use an unnamed button provided its in the same class.

However this is the tricky part - I am not allowed to use the same class to add functionality. Trying to add functionality to the buttons when they are unnamed and in a different class is what I am struggling to figure out. Its divided into two seperate classes - View for the design itself (This includes the button) and controller (This is were I want the button handling to go)

Here is the code for the view class:

package clock;

import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.util.Observer;
import java.util.Observable;

public class View implements Observer {

    ClockPanel panel;
    ActionListener listener;

    public View(Model model) {
        JFrame frame = new JFrame();
        panel = new ClockPanel(model);
        //frame.setContentPane(panel);
        frame.setTitle("Java Clock");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Start of border layout code

        Container pane = frame.getContentPane();

        JButton button = new JButton("Button 1");
        pane.add(button, BorderLayout.PAGE_START);

        panel.setPreferredSize(new Dimension(200, 200));
        pane.add(panel, BorderLayout.CENTER);

        //This is the button I want to use
        button = new JButton("Create Alarm");
        pane.add(button, BorderLayout.LINE_START);


        button = new JButton("View Alarms");
        pane.add(button, BorderLayout.PAGE_END);

        button = new JButton("5 (LINE_END)");
        pane.add(button, BorderLayout.LINE_END);

        // End of borderlayout code

        frame.pack();
        frame.setVisible(true);
    }

    public void update(Observable o, Object arg) {
        panel.repaint();
    }
}

And here is the code for the controller class:

package clock;

import java.awt.event.*;
import javax.swing.Timer;

public class Controller {

    ActionListener listener;
    Timer timer;

    AddAlarm AddAlarm;
    Model model;
    View view;

    public Controller(Model m, View v, AddAlarm a) {
        model = m;
        view = v;
        AddAlarm = a;

        listener = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                model.update();

            }
            // and this is what I want the button to do.
//        button.addActionListener(listener);    
//        private void ButtonActionPerformed(java.awt.event.ActionEvent evt) {
//        new AddAlarm().setVisible(true);
//        }
      };


        timer = new Timer(100, listener);
        timer.start();
    }
}

I'm really new to Swing myself, so if someone could show me how to accomplish this, it would be most appreciated.

Pass a reference of Continer pane to your controller or a getter method in view to return the pane -> iterate trough all the component and get all the buttons-> down cast them back to Jbuttons and attach a event listener -> something like this...

//get a container reference from view
Container pane = view.getPane();

Component[] components = pane.getComponents();
ActionListener actionListener = new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        //do something with the button click;
        String txt = ((Button)e.getSource()).getText();
        if(txt.equals("bla"){ 
            //do bla
        }
    }
};

for (Component component : components) {
    if (component instanceof JButton) {
        ((JButton) component).addActionListener(actionListener);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM