简体   繁体   中英

ClassCastException: javax.swing.plaf.FontUIResource cannot be cast to javax.swing.InputMap

Running a swing application in java, and I got this exception in my program. It's worth noting that this does not show up every time I run the program.

Full stack trace :

Exception in thread "main" java.lang.ExceptionInInitializerError
    at org.backend.utils.importing.Loading$1.run(Loading.java:54)
    at org.backend.utils.importing.Loading.loadEverything(Loading.java:61)
    at org.main.BishopCarrollSchoolTracker.main(BishopCarrollSchoolTracker.java:70)
Caused by: java.lang.ClassCastException: javax.swing.plaf.FontUIResource cannot be cast to javax.swing.InputMap
    at javax.swing.plaf.basic.BasicButtonListener.getInputMap(BasicButtonListener.java:102)
    at javax.swing.plaf.basic.BasicButtonListener.installKeyboardActions(BasicButtonListener.java:78)
    at javax.swing.plaf.basic.BasicButtonUI.installKeyboardActions(BasicButtonUI.java:121)
    at javax.swing.plaf.basic.BasicButtonUI.installUI(BasicButtonUI.java:73)
    at javax.swing.JComponent.setUI(JComponent.java:662)
    at javax.swing.AbstractButton.setUI(AbstractButton.java:1782)
    at javax.swing.plaf.synth.SynthArrowButton.updateUI(SynthArrowButton.java:34)
    at javax.swing.AbstractButton.init(AbstractButton.java:2149)
    at javax.swing.JButton.<init>(JButton.java:118)
    at javax.swing.JButton.<init>(JButton.java:73)
    at javax.swing.plaf.synth.SynthArrowButton.<init>(SynthArrowButton.java:23)
    at javax.swing.plaf.synth.SynthScrollBarUI$2.<init>(SynthScrollBarUI.java:325)
    at javax.swing.plaf.synth.SynthScrollBarUI.createIncreaseButton(SynthScrollBarUI.java:325)
    at javax.swing.plaf.basic.BasicScrollBarUI.installComponents(BasicScrollBarUI.java:225)
    at javax.swing.plaf.basic.BasicScrollBarUI.installUI(BasicScrollBarUI.java:147)
    at javax.swing.JComponent.setUI(JComponent.java:662)
    at javax.swing.JScrollBar.setUI(JScrollBar.java:190)
    at javax.swing.JScrollBar.updateUI(JScrollBar.java:210)
    at javax.swing.JScrollBar.<init>(JScrollBar.java:144)
    at javax.swing.JScrollBar.<init>(JScrollBar.java:159)
    at javax.swing.JScrollPane$ScrollBar.<init>(JScrollPane.java:698)
    at javax.swing.JScrollPane.createHorizontalScrollBar(JScrollPane.java:794)
    at javax.swing.JScrollPane.<init>(JScrollPane.java:282)
    at javax.swing.JScrollPane.<init>(JScrollPane.java:305)
    at org.gui.base.generic.panels.ListTablePanel.<init>(ListTablePanel.java:44)
    at org.gui.base.main.internal.WorkPanel.<init>(WorkPanel.java:28)
    at org.gui.base.main.internal.InternalPanel.<clinit>(InternalPanel.java:38)
    ... 3 more

Lines causing exception (In my source code) :

public static final WorkPanel WORK = new WorkPanel();

Goes to

super(new WorkTable(AllWork.getElements(), true, true, true, true, true, true, true),
            new WorkTable(AllWork.getElements(), true, true, true, true, true, true, true, true));

To

public ListTablePanel(RefreshableTable m, RefreshableTable t) {
    this.main = m;
    this.totals = t;

    setLayout(LayoutFactory.createLayout());

    JScrollPane pane = new JScrollPane(main);

    main.setAutoCreateRowSorter(false);

    totals.setFont(totals.getFont().deriveFont(Font.BOLD));
    totals.setEnabled(false);

    pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    add(pane, LayoutFactory.newFactory().setFill(GridBagConstraints.BOTH).
            setY(0).setWeightX(1).setWeightY(1));
    add(totals, LayoutFactory.newFactory().setFill(GridBagConstraints.BOTH).
            setY(1).setWeightX(1).setInsets(new Insets(0, 2, 0, 17)));
}

The last piece of code in my source code is the construction of JScrollPane .

I don't understand why this exception is thrown.

Any time I see this:

Running a swing application in java, and I got this exception in my program. It's worth noting that this does not show up every time I run the program.

Alarm bells go off in my head, and I think "am I handling Swing threading correctly"? Because it is not uncommon for Swing thread mishaps to cause strange errors that don't always occur.

So, are you handing Swing threading correctly, by first of all initiating and displaying the Swing GUI on the event thread? ie,

private static void createAndShowGui() {

  // create and display my GUI here

}

public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {
     public void run() {
        createAndShowGui();
     }
  });
}

By the way, you're not using Nimbus or another look and feel?

Fixed the error. Found out I was doing GUI stuff simultaneously in the EDT. Constructors took too long to use invokeLater() , started using invokeAndWait() .

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM