Kendra Cheatham
Kendra Cheatham

Reputation: 127

Adding a JPanel to an ActionListener

PortSettings Panel screenshot

I have created a JPanel that has all the JRadioButtons on it that I need (it is called PortSettings). I also have a button, called port settings, when the user clicks the button, I need the JPanel to come up and display the radio buttons. I have tried to add the JPanel to the actionlistener but it doesn't work. My code is below. I have deleted all other ActionListener's from the other buttons except for the portsettings buttons. If this question is confusing I'm sorry. It's really hard to explain what I need to do. I have uploaded a drawing of what the panel will look like as well as a screenshot of my program.

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;


public class TestApplication implements ActionListener {
    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(1000, 1000);
        frame.setTitle("RBA Test Application");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);


    JTextArea text = new JTextArea();
    JLabel logLabel = new JLabel("Input/Output Log");



    JRadioButton apprve = new JRadioButton("Approve");
    JRadioButton decline = new JRadioButton("Decline");
    JRadioButton ethernet = new JRadioButton("Ethernet");
    JRadioButton rs = new JRadioButton("RS232");
    JRadioButton usbcdc = new JRadioButton("USB_CDC");
    JRadioButton usbhid = new JRadioButton("USB_HID");

    JButton next = new JButton("Next");
    JButton ok = new JButton("OK");
    JButton cancel = new JButton("Cancel");

    JPanel PortSettings = new JPanel();
    PortSettings.add(ethernet);
    PortSettings.add(rs);
    PortSettings.add(usbcdc);
    PortSettings.add(usbhid);
    PortSettings.add(next);
    PortSettings.add(cancel);


    JButton initialize = new JButton("Initialize");

    JButton connect = new JButton("Connect");

    JButton disconnect = new JButton("Disconnect");

    JButton shutdown = new JButton("Shut Down");


    JButton portsettings = new JButton("Port Settings");
     portsettings.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {




            }
        });


    JButton online = new JButton("Go Online");

    JButton offline = new JButton("Go Offline");

    JButton status = new JButton("Status");

    JButton reboot = new JButton("Reboot");


    JButton account = new JButton("Account");


    JButton amount = new JButton("Amount");

    JButton reset = new JButton("Reset");


    JButton approvordecl = new JButton("Approve / Decline");

    JButton test = new JButton("Test Button #1");

    JButton testing = new JButton("Test Button #2");

    JRadioButton button = new JRadioButton("Radio Button");

    JRadioButton button2 = new JRadioButton("Radio Button");

    JCheckBox checkbox = new JCheckBox("Check Box");

    JCheckBox checkbox2 = new JCheckBox("Check Box");


    JPanel testPanel = new JPanel();
    testPanel.add(button);
    testPanel.add(button2);
    testPanel.add(checkbox2);

    JPanel posPanel = new JPanel();
    posPanel.add(test);
    posPanel.add(testing);
    posPanel.add(checkbox);

    JPanel llpPanel = new JPanel();
    llpPanel.add(online);
    llpPanel.add(offline);
    llpPanel.add(status);
    llpPanel.add(reboot);
    llpPanel.add(account);
    llpPanel.add(amount);
    llpPanel.add(reset);
    llpPanel.add(approvordecl);

    JPanel textPanel = new JPanel(new BorderLayout());
    textPanel.add(logLabel);
    frame.add(logLabel); 


    JPanel buttonPanel = new JPanel();
    buttonPanel.add(initialize);
    buttonPanel.add(connect);
    buttonPanel.add(disconnect);
    buttonPanel.add(shutdown);
    buttonPanel.add(portsettings);
    frame.add(buttonPanel);
    frame.add(buttonPanel, BorderLayout.NORTH);

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol");
    tabbedPane.addTab("POS",null, posPanel, "Point Of Sale");
    tabbedPane.addTab("Test", null, testPanel, "Test");

    JPanel tabsPanel = new JPanel(new BorderLayout());
    tabsPanel.add(tabbedPane);
    frame.add(tabsPanel, BorderLayout.CENTER);


    frame.pack();



}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}






} 

I have tried to add a JFrame to the ActionListener then add the JPanel to the JFrame but nothing happens when I click the Port Settings button. Also, when I tried to add the JPanel to the JFrame it told me to put final in front of JPanel PortSettings = new JPanel();. Here is the code.

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;


public class TestApplication implements ActionListener {
    public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setSize(1000, 1000);
    frame.setTitle("RBA Test Application");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);


    JTextArea text = new JTextArea();
    JLabel logLabel = new JLabel("Input/Output Log");



    JRadioButton apprve = new JRadioButton("Approve");
    JRadioButton decline = new JRadioButton("Decline");
    JRadioButton ethernet = new JRadioButton("Ethernet");
    JRadioButton rs = new JRadioButton("RS232");
    JRadioButton usbcdc = new JRadioButton("USB_CDC");
    JRadioButton usbhid = new JRadioButton("USB_HID");

    JButton next = new JButton("Next");
    JButton ok = new JButton("OK");
    JButton cancel = new JButton("Cancel");

    final JPanel PortSettings = new JPanel();
    PortSettings.add(ethernet);
    PortSettings.add(rs);
    PortSettings.add(usbcdc);
    PortSettings.add(usbhid);
    PortSettings.add(next);
    PortSettings.add(cancel);

    JPanel accountButton = new JPanel();
    accountButton.add(ok);
    accountButton.add(cancel);

    JPanel apprvordecl = new JPanel();
    apprvordecl.add(apprve);
    apprvordecl.add(decline);

    JPanel amountButton = new JPanel();
    amountButton.add(ok);
    amountButton.add(cancel);



    JButton initialize = new JButton("Initialize");

    JButton connect = new JButton("Connect");

    JButton disconnect = new JButton("Disconnect");

    JButton shutdown = new JButton("Shut Down");


    JButton portsettings = new JButton("Port Settings");
     portsettings.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JFrame port = new JFrame("Port Settings");
                port.add(PortSettings);
                frame.setVisible(true);





            }
        });

    JButton online = new JButton("Go Online");

    JButton offline = new JButton("Go Offline");

    JButton status = new JButton("Status");

    JButton reboot = new JButton("Reboot");


    JButton account = new JButton("Account");


    JButton amount = new JButton("Amount");

    JButton reset = new JButton("Reset");


    JButton approvordecl = new JButton("Approve / Decline");

    JButton test = new JButton("Test Button #1");

    JButton testing = new JButton("Test Button #2");

    JRadioButton button = new JRadioButton("Radio Button");

    JRadioButton button2 = new JRadioButton("Radio Button");

    JCheckBox checkbox = new JCheckBox("Check Box");

    JCheckBox checkbox2 = new JCheckBox("Check Box");


    JPanel testPanel = new JPanel();
    testPanel.add(button);
    testPanel.add(button2);
    testPanel.add(checkbox2);

    JPanel posPanel = new JPanel();
    posPanel.add(test);
    posPanel.add(testing);
    posPanel.add(checkbox);

    JPanel llpPanel = new JPanel();
    llpPanel.add(online);
    llpPanel.add(offline);
    llpPanel.add(status);
    llpPanel.add(reboot);
    llpPanel.add(account);
    llpPanel.add(amount);
    llpPanel.add(reset);
    llpPanel.add(approvordecl);

    JPanel textPanel = new JPanel(new BorderLayout());
    textPanel.add(logLabel);
    frame.add(logLabel); 


    JPanel buttonPanel = new JPanel();
    buttonPanel.add(initialize);
    buttonPanel.add(connect);
    buttonPanel.add(disconnect);
    buttonPanel.add(shutdown);
    buttonPanel.add(portsettings);
    frame.add(buttonPanel);
    frame.add(buttonPanel, BorderLayout.NORTH);

    JTabbedPane tabbedPane = new JTabbedPane();
    tabbedPane.addTab("LLP", null, llpPanel, "Low Level Protocol");
    tabbedPane.addTab("POS",null, posPanel, "Point Of Sale");
    tabbedPane.addTab("Test", null, testPanel, "Test");

    JPanel tabsPanel = new JPanel(new BorderLayout());
    tabsPanel.add(tabbedPane);
    frame.add(tabsPanel, BorderLayout.CENTER);


    frame.pack();



}

@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}






}

Upvotes: 0

Views: 6753

Answers (2)

Arend
Arend

Reputation: 2423

You´re on the right track, but you do not want to add your PortSettings panel to a new JFrame but somewhere on your previously built one, assigned to the local variable frame. So your action listener should rather be

portsettings.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
           frame.add(PortSettings, BorderLayout.SOUTH);
           frame.pack();
       }
   });

(This is assuming that you actually want to add it to the frame at that instant and not add it invisibly right from the start and turn it visible, like @Aleksei suggested.)

The error message about final is because you use PortSettings in an (anonymous) inner class - viz., the ActionListener. In my proposed modification the same goes for frame, so you need to adapt its declaration as well:

final JFrame frame = new JFrame();

The reason why is quite technical and beside the point right now: just do it.

If instead you want the panel to appear in a separate window, you need a JDialog for that, not a second JFrame:

portsettings.addActionListener(new ActionListener() {
       public void actionPerformed(ActionEvent e) {
           JDialog dialog = new JDialog(frame);
           dialog.add(PortSettings);
           dialog.pack();
           dialog.setVisible(true);
       }
   });

Take a look at the JOptionPane class for a rich choice of ways to get more functionality out of dialogs.

Upvotes: 1

Alex Orzechowski
Alex Orzechowski

Reputation: 377

Just add the action listener to all your buttons. like this:

yourButton.addActionListener(this);

Do that for all the buttons.

Then take your TestPalication class's actionPreformed method and do whatever:

@Override
public void actionPerformed(ActionEvent arg0) {
    ((JRadioButton) arg0.getSource()).setTitle("Clicked!");
}

Your question was a little bit confusing but I hope this clarifies a little bit.

Upvotes: 0

Related Questions