Question

i'm trying to get a JDialog to "pop up" when i press a JCheckBox (if its not true). I want to use is as a button. I hope i am posting this right, my first post here :)

I am getting a error that says i can't cast my JCheckBox to a JButton. Can you help me do this right?

package model;

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

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class FirstJDialog extends JDialog 
{
    private Controller controller;

    private HotelJDialog hotelJDialog;
    private PartnerJDialog partnerJDialog;

    private JLabel lblName, lblArrival, lblDeparture, lblLekture, lblHotel, lblPartner;
    private JTextField txfName, txfArrival, txfDeparture;
    private JCheckBox ckbLekture, ckbHotel, ckbPartner;

    public FirstJDialog()
    {
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.setTitle("Person informationer");
        this.setLayout(null);
        this.setSize(900, 650);
        this.setLocation(500, 200);
        this.setResizable(false);

        // Labels for TextField ######################################################################
        lblName = new JLabel("Navn:");
        this.add(lblName);
        this.lblName.setSize(100, 40);
        this.lblName.setLocation(25, 25);

        lblArrival = new JLabel("Ankomst:");
        this.add(lblArrival);
        this.lblArrival.setSize(100, 40);
        this.lblArrival.setLocation(25, 70);

        lblDeparture = new JLabel("Afgang:");
        this.add(lblDeparture);
        this.lblDeparture.setSize(100, 40);
        this.lblDeparture.setLocation(25, 115);

        // TextField ######################################################################
        txfName = new JTextField();
        this.add(txfName);
        this.txfName.setSize(200, 40);
        this.txfName.setLocation(180, 25);

        txfArrival = new JTextField();
        this.add(txfArrival);
        this.txfArrival.setSize(200, 40);
        this.txfArrival.setLocation(180, 70);

        txfDeparture = new JTextField();
        this.add(txfDeparture);
        this.txfDeparture.setSize(200, 40);
        this.txfDeparture.setLocation(180, 115);

        // CheckBox ######################################################################
        ckbLekture = new JCheckBox();
        this.add(ckbLekture);
        this.ckbLekture.setSize(25, 25);
        this.ckbLekture.setLocation(25, 200);

        ckbHotel = new JCheckBox();
        this.add(ckbHotel);
        this.ckbHotel.setSize(25, 25);
        this.ckbHotel.setLocation(25, 250);

        ckbPartner = new JCheckBox();
        this.add(ckbPartner);
        this.ckbPartner.setSize(25, 25);
        this.ckbPartner.setLocation(25, 300);

        // Labels for CheckBox ######################################################################
        lblLekture = new JLabel("Foredrag");
        this.add(lblLekture);
        this.lblLekture.setSize(100, 40);
        this.lblLekture.setLocation(125, 190);

        lblHotel = new JLabel("Hotel");
        this.add(lblHotel);
        this.lblHotel.setSize(100, 40);
        this.lblHotel.setLocation(125, 240);

        lblPartner = new JLabel("Ledsager");
        this.add(lblPartner);
        this.lblPartner.setSize(100, 40);
        this.lblPartner.setLocation(125, 290);

        // JDialogs ######################################################################
        hotelJDialog = new HotelJDialog();
        partnerJDialog = new PartnerJDialog();

        controller = new Controller();
        ckbHotel.addActionListener(controller);
    }

    private class Controller implements ActionListener
    {                   

        public void actionPerformed(ActionEvent e) 
        {
            JButton source = (JButton) e.getSource();
            JCheckBox sourxe = (JCheckBox) e.getSource();
            if(sourxe.equals(ckbHotel))
                hotelJDialog.setVisible(true);
        }   
    }
Was it helpful?

Solution

The error is a result of an attempt to cast JCheckBox to a JButton. This cast is unnecessary, as you never register this listener with any JButton, only with ckbHotel which is JCheckBox.

In the given example, there is no need to cast, you can simply check the source:

if(ckbHotel.equals(e.getSource()))
    hotelJDialog.setVisible(true);

You can also register a different action listener for this checkbox. There is no need to have a single action listener to serve all the controls in the container, for example:

ckbHotel.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        hotelJDialog.setVisible(true);
    }
});

OTHER TIPS

 JButton source = (JButton) e.getSource(); 

this line give this error. remove this ans add this

JCheckBox sourxe = (JCheckBox) e.getSource();
if(sourxe.isSelected())
      hotelJDialog.setVisible(true);

this is only show your dialog if sourxe is selected..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top