Adding ActionListeners to buttons in Java

user4839895

I am trying to understand how I can print out info after clicking the button. I have tried several things but I am learning on my own. I am currently on knowing the different Listeners so help me on the adding the button Listener.

I wanted when a combobox is selected it prints out info and shows some details. I have a button but I want once the info in the combobox is selected it prints out info.

import javax.swing.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.*;

public class ItemCheck extends JFrame{

    JComboBox PainandFeverReliever;
    JButton button1;
    JLabel lable;

    public static void main(String[] args){
        new ItemCheck();
    }

    public ItemCheck(){
        this.setSize(400,400);

        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        this.setTitle("Medicine Stock");

        JPanel thePanel = new JPanel();
        JLabel label = new JLabel (" Medicine");
        label.setToolTipText("This Shows you how many medicine you have left in the store.");
        thePanel.add(label);

        String[] shows = {"","Cetaminophen" , "ephedrine","menthol"};

        PainandFeverReliever = new JComboBox(shows);
        PainandFeverReliever.addItem("Pushing Daisies");

        thePanel.add(PainandFeverReliever);

        button1 = new JButton("Results");

        ListenForButton lForButton = new ListenForButton();

        button1.addActionListener(lForButton);
        thePanel.add(button1);

        this.add(thePanel);

        this.setVisible(true);

        PainandFeverReliever.insertItemAt("diphenhydramine", 1);

        PainandFeverReliever.setMaximumRowCount(20);
    }

    private void setForeground(int hsBtoRGB) {
        // TODO Auto-generated method stub
    }

    private class ListenForButton implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getSource() == button1){
            }
        }
    }
}
Madona Syombua

I think you need to introduce a string – remember you can call it whatever you want to, then after that you will need to add it to your button function and then that will print out the info you want.I am doing this since i understand you want to view info from a JComboBox and this is how it should be done.

call the string info.

try adding this info in your button and maybe you can learn afew from it. also i recommend you read more about JOptionPane.

 String info;
        PainandFeverReliever.setEditable(true);       
 info = "Item : " + PainandFeverReliever.getItemAt(0) + "\n";
 info  += "Count; " + PainandFeverReliever.getItemCount() + "\n";
 info += "Selected ID: " + PainandFeverReliever.getSelectedIndex() + "\n";
 info += "Selected : " + PainandFeverReliever.getSelectedItem() + "\n";
 info += "Combo Box Editable: " + PainandFeverReliever.isEditable() + "\n";
JOptionPane.showMessageDialog(ItemCheck.this, info, "Information", JOptionPane.INFORMATION_MESSAGE);
info = "";

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related