So im trying to make my program have 2 options in a JComboBox, and both options should have a (different) max amount it can reach before it tells you a message to start over.
But when i click the button that checks which JComboBox option is chosen and what the subtotal is, i get a javax.swing.JButton cannot be cast to javax.swing.JComboBox exception.
Im fairly new to progamming and Java, and using JComboBox is something that isnt explained in my school books so im basically stuck now for a couple of hours already...
Please ignore the Dutch comments between the lines of code :D
package applicatieschool;
// Opstartklasse creëren
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.border.*;
// Frame maken
public class ApplicatieSchool extends JFrame {
public static void main( String args[] ) {
JFrame frame = new ApplicatieSchool(); // JFrame-object aanmaken
frame.setSize( 1200, 500 ); // Grootte JFrame
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); // Hierdoor kan het JFrame afgesloten woorden
frame.setTitle( "School Applicatie "); // Title geven aan JFrame
frame.setContentPane( new Werkpaneel() ); // JPanel aan JFrame toevoegen
frame.setVisible( true ); // Object zichtbaar maken op het scherm
}
}
// JPanel aanmaken
class Werkpaneel extends JPanel {
// Declareren van 3 integers ( gehele getallen )
private JTextField invoerVak1, invoerVak2, uitvoerVak1, uitvoerVak2, uitvoerVak3;
private JLabel invoerLabel1, invoerLabel2, uitvoerLabel1, uitvoerLabel2;
private JButton berekenTotaalKnop, resetTotaalKnop;
private Pallet pallet;
private JComboBox dozenLijst;
private int a, b, c, d;
// Constructor creëren
public Werkpaneel() {
setLayout( new GridLayout( 20, 5 ) );
Border border = BorderFactory.createEmptyBorder( 5 ,5 ,5 ,5 );
setBorder( border );
a = 2376;
b = 2088;
c = 396;
d = 348;
String[] dozen = {"Doos Type 1", "Doos Type 2"};
dozenLijst = new JComboBox( dozen );
dozenLijst.addActionListener( new LijstHandler() );
pallet = new Pallet();
// 3 tekstvakken maken
invoerVak1 = new JTextField( 10 );
invoerVak1.addActionListener( new Invoervak1Handler() );
invoerVak2 = new JTextField( 10 );
invoerVak2.addActionListener( new Invoervak1Handler() );
uitvoerVak1 = new JTextField( 10 );
uitvoerVak1.addActionListener( new Invoervak1Handler() );
uitvoerVak1.setEditable( false );
uitvoerVak2 = new JTextField( 10 );
uitvoerVak2.addActionListener( new Invoervak1Handler() );
uitvoerVak2.setEditable( false );
uitvoerVak3 = new JTextField( 40 );
uitvoerVak3.setEditable( false );
// 3 labels maken
invoerLabel1 = new JLabel( "Aantal stuks totaal");
invoerLabel2 = new JLabel( "Aantal in een doos" );
uitvoerLabel1 = new JLabel( "Aantal dozen" );
uitvoerLabel2 = new JLabel( "Totaal aantal dozen " );
berekenTotaalKnop = new JButton( "Totaal");
berekenTotaalKnop.addActionListener( new BerekenTotaalHandler() );
resetTotaalKnop = new JButton( "Reset die shit ");
resetTotaalKnop.addActionListener( new ResetHandler() );
// Toevoegen aan het paneel
add( dozenLijst );
add( invoerLabel1 );
add( invoerVak1 );
add( invoerLabel2 );
add( invoerVak2 );
add( uitvoerLabel1 );
add( uitvoerVak1 );
add( uitvoerLabel2 );
add( uitvoerVak2 );
add( berekenTotaalKnop );
add( resetTotaalKnop );
add( uitvoerVak3 );
}
class LijstHandler implements ActionListener {
public void actionPerformed( ActionEvent e ) {
JComboBox<String> cb = (JComboBox<String>) e.getSource();
String selectie = (String) cb.getSelectedItem();
if ( selectie.equals("Doos Type 1") ) {
uitvoerVak3.setText( "U kunt maximaal 2376 stuks op een pallet stapelen ");
}
else if( selectie.equals("Doos Type 2" ) ) {
uitvoerVak3.setText( "U kunt maximaal 2088 stuks op een pallet stapelen " );
}
}
}
class Invoervak1Handler implements ActionListener {
public void actionPerformed( ActionEvent e ) {
String invoer1 = invoerVak1.getText();
double invoerGetal1 = Double.parseDouble( invoer1 );
String invoer2 = invoerVak2.getText();
double invoerGetal2 = Double.parseDouble( invoer2 );
double uitvoerGetal = ( invoerGetal1 / invoerGetal2 );
uitvoerVak1.setText( "" + uitvoerGetal );
pallet.telOp( uitvoerGetal );
double st = pallet.getSubtotaal();
}
}
// Totale aantal berekenen
class BerekenTotaalHandler implements ActionListener {
public void actionPerformed( ActionEvent e ) {
uitvoerVak2.setText( String.format( "%.2f", pallet.getSubtotaal() ) );
JComboBox<String> cb = (JComboBox<String>) e.getSource();
String selectie = (String) cb.getSelectedItem();
if ( selectie.equals("Doos Type 1") && pallet.getSubtotaal() > c ) {
uitvoerVak3.setText( "MAXIMUM HOOGTE BEREIKT, BEGIN MET STAPELEN OP EEN NIEUWE PALLET ");
}
else if( selectie.equals("Doos Type 2") && pallet.getSubtotaal() > d ) {
uitvoerVak3.setText( "MAXIMUM HOOGTE BEREIKT, BEGIN MET STAPELEN OP EEN NIEUWE PALLET ");
/* if ( pallet.getSubtotaal() > c ) {
uitvoerVak3.setText( "MAXIMUM HOOGTE BEREIKT, BEGIN MET STAPELEN OP EEN NIEUWE PALLET ");
*/
}
// else statement?5
}
}
// alle velden leegmaken
class ResetHandler implements ActionListener {
public void actionPerformed( ActionEvent e ) {
invoerVak1.setText( "" );
invoerVak2.setText( "" );
uitvoerVak1.setText( "" );
uitvoerVak2.setText( "" );
pallet.reset();
}
}
}
If your error is about casting viz i get a javax.swing.JButton cannot be cast to javax.swing.JComboBox exception., then the problem should point to
JComboBox<String> cb = (JComboBox<String>) e.getSource();
Probably the event listener is listening to your button clicks and you have to work on that logic.
The problem is with this line:
JComboBox<String> cb = (JComboBox<String>) e.getSource();
e.getSource()
returns a reference to the button that was clicked and fired the event. You cast this JButton
object to a JComboBox
reference which causes the error. To fix the problem, replace
JComboBox<String> cb = (JComboBox<String>) e.getSource();
String selectie = (String) cb.getSelectedItem();
with
String selectie = (String) dozenLijst.getSelectedItem();
In actionPerformed in BerekenTotaalHandler, you cast e.getSource() to a JComboBox. The source is actually the JButton berekenTotaalKnop, meaning that when you hit that button it will cause this exception. You'll need to access your combobox in a different way--it would probably be easiest to make the action listener a private internal class of the work panel class, and access it directly.
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.