我在选择 ComboBox 中的值时遇到问题。选择一个值后,我得到一个 IndexOutOfBoundsException。
我试过更改 for 循环但没有用。组合框中的值似乎也重复多次。
有人可以提出问题是什么吗?
package edu.unlv.mis768.project;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.ToggleGroup;
import javafx.stage.Stage;
public class AddOption1Controller {
@FXML
private Button CancelButton;
@FXML
private RadioButton ExistingCragRadioButton;
@FXML
private RadioButton NewCragRadioButton;
@FXML
private ToggleGroup NewExistingCrag;
@FXML
private Button NextButton;
@FXML
private TableView<ClimbingCrag> cragTableView;
@FXML
private ComboBox<ClimbingCrag> existingCragComboBox;
/**
* this method will initialize the scene to have the correct values for the crag choice list
*/
public void initialize() {
existingCragComboBox.setDisable(true);
// Create an instance of the DAO implementation class
CragDAO cragDAO = new CragDAOImpl();
// Call the getAllCragNames() method to retrieve the list of crags
List<ClimbingCrag> cragList = cragDAO.getAllCragNames();
// Clear the existing items in the choice box
existingCragComboBox.getItems().clear();
// Loop through the cragList and add the string representation of each object to the choice box
System.out.print(cragList.size());
for (int i = 0; i < cragList.size(); i++) {
System.out.println("here");
existingCragComboBox.getItems().addAll(cragList);
}
}
package edu.unlv.mis768.project;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class CragDAOImpl implements CragDAO {
@Override
public List<ClimbingCrag> getAllCragNames() {
// Create a array list for the data.//defined as the superclass List so we can display on GUI like observable list
List<ClimbingCrag> cragList = new ArrayList<ClimbingCrag>();
try {
Connection conn = RockClimbingUtil.getDBConnection();
Statement stmt = conn.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
//String sql = "SELECT * FROM " + DBConstants.CRAG_TABLE_NAME;
String sql = "SELECT "+ DBConstants.CRAG_PK_NAME+ " FROM " + DBConstants.CRAG_TABLE_NAME;
//Execute the query.
ResultSet result = stmt.executeQuery(sql);
//Get the number of rows.
result.last(); // Move to last row
int numRows = result.getRow(); // Get row number
result.first(); // Move to first row
for (int row = 0; row < numRows; row++) {
// create a new object and fill the field with the values from the result set.
ClimbingCrag aCrag = new ClimbingCrag(result.getString("Name"));
//Add the object to the list
cragList.add(aCrag);
// Go to the next row in the ResultSet.
result.next();
}
// close the database connection
stmt.close();
RockClimbingUtil.closeDBConnection(conn);
} catch (Exception ex) {
System.out.println("ERROR: " + ex.getMessage());
}
// return the result
return cragList;
}
Exception in thread "JavaFX Application Thread" java.lang.IndexOutOfBoundsException: [ fromIndex: 0, toIndex: 1, size: 0 ]
at [email protected]/com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.subList(ReadOnlyUnbackedObservableList.java:171)
at [email protected]/javafx.collections.ListChangeListener$Change.getAddedSubList(ListChangeListener.java:243)
at [email protected]/com.sun.javafx.scene.control.behavior.ListViewBehavior.lambda$new$57(ListViewBehavior.java:292)
at [email protected]/javafx.collections.WeakListChangeListener.onChanged(WeakListChangeListener.java:88)
at [email protected]/com.sun.javafx.collections.ListListenerHelper$Generic.fireValueChangedEvent(ListListenerHelper.java:329)
at [email protected]/com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
at [email protected]/javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:239)
at [email protected]/com.sun.javafx.scene.control.ReadOnlyUnbackedObservableList.callObservers(ReadOnlyUnbackedObservableList.java:110)
at [email protected]/javafx.scene.control.MultipleSelectionModelBase.clearAndSelect(MultipleSelectionModelBase.java:394)
at [email protected]/javafx.scene.control.ListView$ListViewBitSetSelectionModel.clearAndSelect(ListView.java:1452)
at [email protected]/com.sun.javafx.scene.control.behavior.CellBehaviorBase.simpleSelect(CellBehaviorBase.java:282)
at [email protected]/com.sun.javafx.scene.control.behavior.CellBehaviorBase.doSelect(CellBehaviorBase.java:246)
at [email protected]/com.sun.javafx.scene.control.behavior.CellBehaviorBase.mousePressed(CellBehaviorBase.java:176)
at [email protected]/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at [email protected]/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at [email protected]/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at [email protected]/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at [email protected]/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at [email protected]/javafx.event.Event.fireEvent(Event.java:198)
at [email protected]/javafx.scene.Scene$MouseHandler.process(Scene.java:3894)
at [email protected]/javafx.scene.Scene.processMouseEvent(Scene.java:1887)
at [email protected]/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2620)
at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at [email protected]/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at [email protected]/com.sun.glass.ui.View.handleMouseEvent(View.java:551)
at [email protected]/com.sun.glass.ui.View.notifyMouse(View.java:937)
at [email protected]/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at [email protected]/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:833)
FXML 代码
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.RadioButton?>
<?import javafx.scene.control.ToggleGroup?>
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="600.0" prefWidth="502.0" xmlns="http://javafx.com/javafx/19" xmlns:fx="http://javafx.com/fxml/1" fx:controller="edu.unlv.mis768.project.AddOption1Controller">
<children>
<ImageView fitHeight="200.0" fitWidth="502.0" layoutY="-1.0">
<image>
<Image url="@Half_Dome.jpg" />
</image>
</ImageView>
<Label layoutX="38.0" layoutY="234.0" text="Please Select New or Existing Crag:" />
<RadioButton fx:id="NewCragRadioButton" layoutX="38.0" layoutY="272.0" mnemonicParsing="false" onAction="#radioButtonListener" text="New Crag">
<toggleGroup>
<ToggleGroup fx:id="NewExistingCrag" />
</toggleGroup>
</RadioButton>
<RadioButton fx:id="ExistingCragRadioButton" layoutX="141.0" layoutY="272.0" mnemonicParsing="false" onAction="#radioButtonListener" text="Existing Crag" toggleGroup="$NewExistingCrag" />
<Button fx:id="CancelButton" layoutX="336.0" layoutY="532.0" mnemonicParsing="false" onAction="#changeSceneToMainMenu" text="Cancel" />
<Button fx:id="NextButton" layoutX="420.0" layoutY="532.0" mnemonicParsing="false" onAction="#nextButtonListener" text="Next" />
<Label layoutX="190.0" layoutY="64.0" prefHeight="69.0" prefWidth="122.0" text="Crux" textFill="WHITE">
<font>
<Font name="System Bold" size="48.0" />
</font>
</Label>
<ComboBox fx:id="existingCragComboBox" layoutX="261.0" layoutY="268.0" onAction="#initialize" prefWidth="150.0" />
</children>
</AnchorPane>
我们尝试更改 for 循环,但我们得到了同样的错误