1

I try Select a case from JComboBox then write a value and open that website "case+value". But when I click button get this error message:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.awt.Button cannot be cast to javax.swing.JComboBox
    at Deneme1.actionPerformed(Deneme1.java:52)
    at java.awt.Button.processActionEvent(Button.java:409)
    at java.awt.Button.processEvent(Button.java:377)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

There is my source code:

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;
import java.net.URI;
import javax.swing.*;
import java.net.URISyntaxException;

public class Deneme1 extends Frame implements WindowListener, ActionListener {

    JTextField Value = new JTextField(20);
    Button Try;

    public static void main(String[] args) {
        Deneme1 Windows = new Deneme1("Deneme1 ");
        Windows.setSize(400, 200);
        Windows.setLocation(430, 100);
        Windows.setVisible(true);
        Windows.setTitle("Title");
    }

    public Deneme1(String title) {

        JLabel Text2 = new JLabel("Pick one: ");
        add(Text2);
        JComboBox Select = new JComboBox();
        Select.addItem("A");
        Select.addItem("B");
        add(Select);
        JLabel Text1 = new JLabel("Enter Value:");
        setLayout(new FlowLayout());
        addWindowListener(this);
        Try = new Button("Try");
        add(Text1);
        add(Value);
        add(Try);
        Try.addActionListener(this);
    }

    public void actionPerformed(ActionEvent E) {

        JComboBox<String> Select = (JComboBox<String>) E.getSource();
        String Selected = (String) Select.getSelectedItem();
        switch (Selected) {
            case "A":
                try {
                    URI AcilacakSite = new URI("https://A" + Value.getText());
                    Desktop.getDesktop().browse(AcilacakSite);
                } catch (URISyntaxException | IOException ex) {
                    JOptionPane.showMessageDialog(null, "ERR");
                }
                break;
            case "B":
                try {
                    URI AcilacakSite = new URI("https://B" + Value.getText());
                    Desktop.getDesktop().browse(AcilacakSite);
                } catch (URISyntaxException | IOException ex) {
                    JOptionPane.showMessageDialog(null, "ERR");
                }
                break;
            default:
                JOptionPane.showMessageDialog(null, "ERR");
                break;
        }
    }

    @Override
    public void windowOpened(WindowEvent e) {
    //throw new UnsupportedOperationException("Not supported yet."); //To     change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void windowClosing(WindowEvent e) {
        dispose();
        System.exit(0);

    }

    @Override
    public void windowClosed(WindowEvent e) {
    //throw new UnsupportedOperationException("Not supported yet."); //To     change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void windowIconified(WindowEvent e) {
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void windowActivated(WindowEvent e) {
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
    //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Atakan Akbulut
  • 620
  • 1
  • 9
  • 15
  • 1) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 2) See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) 3) Please learn common Java nomenclature (naming conventions - e.g. `EachWordUpperCaseClass`, `firstWordLowerCaseMethod()`, `firstWordLowerCaseAttribute` unless it is an `UPPER_CASE_CONSTANT`) and use it consistently. – Andrew Thompson Jun 11 '18 at 19:00
  • 1
    .. 4) Don't mix Swing (e.g. `JComboBox`) and AWT (e.g. `Button`) components without good reason. There is no good reason here. Use a `JButton` instead. 5) **The source of the *event* is the `Button`** Instead of trying to get access to the combo box via the event source, declare it as an attribute of the class and access it directly by name. Either that or add the action listener to the combo box & remove the button completely, then it **will** be the source of the event. 6) Replace `extends Frame` with `extends JFrame` and the etnire `WindowListener` and the associated methods .. – Andrew Thompson Jun 11 '18 at 19:09
  • .. can be replaced with `Windows.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);` – Andrew Thompson Jun 11 '18 at 19:10

1 Answers1

3

This code fixes the ClassCastException by declaring the combo box as a class attribute and accessing it directly in the action performed method.

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import javax.swing.*;
import java.io.IOException;

public class Deneme1 extends JFrame implements ActionListener {

    JTextField value = new JTextField(20);
    JButton tryFetch;
    // declared as a class attribute, this can be accessed 
    // in the action performed method
    JComboBox select; 

    public static void main(String[] args) {
        Deneme1 windows = new Deneme1("Deneme1 ");
        windows.setSize(400, 200);
        windows.setLocation(430, 100);
        windows.setVisible(true);
        windows.setTitle("Title");
    }

    public Deneme1(String title) {
        JLabel text2 = new JLabel("Pick one: ");
        add(text2);
        select = new JComboBox();
        select.addItem("A");
        select.addItem("B");
        add(select);
        JLabel text1 = new JLabel("Enter value:");
        setLayout(new FlowLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        tryFetch = new JButton("Try");
        add(text1);
        add(value);
        add(tryFetch);
        tryFetch.addActionListener(this);
    }

    public void actionPerformed(ActionEvent E) {
        String selected = (String) select.getSelectedItem();
        switch (selected) {
            case "A":
                try {
                    URI AcilacakSite = new URI("https://A" + value.getText());
                    Desktop.getDesktop().browse(AcilacakSite);
                } catch (URISyntaxException | IOException ex) {
                    JOptionPane.showMessageDialog(null, "ERR");
                }
                break;
            case "B":
                try {
                    URI AcilacakSite = new URI("https://B" + value.getText());
                    Desktop.getDesktop().browse(AcilacakSite);
                } catch (URISyntaxException | IOException ex) {
                    JOptionPane.showMessageDialog(null, "ERR");
                }
                break;
            default:
                JOptionPane.showMessageDialog(null, "ERR");
                break;
        }
    }
}

Note: I fixed most of the things mentioned in comments on the question. There are still problems with this code.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433