Java JComboBox 接受 ArrayList,但不显示 ArrayList 的项目



我正在尝试用数据库中的记录填充组合框。在我的控制器中,我正在使用MVC,在我的模型中,我有一个方法getAllRoutes((,它将数据库中路由的所有名称放入ArrayList中。此方法按预期工作,因为我在其中添加了一个 System.out.println((,它表明 ArrayList 确实被数据库值填充。

在我的控制器中,我创建了一个名为"routes"的新ArrayList,并使用我放入getAllRoutes((方法返回的ArrayList对其进行初始化。这也可以工作,因为我添加了一个System.out.println((并且它可以打印。

在我看来,我创建了一个新的JComboBox,一个新的ArrayList comboBoxValues和一个新的方法setComboBoxValues((,它只填充ArrayList comboBoxValues。回到控制器中,我调用setComboBoxValues((方法并用"routes"ArrayList填充它。

但是由于某种原因,当我运行程序时,JComboBox保持为空(见下图(。我做错了什么?

我的模型:

package main.java.models;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import main.java.controllers.MapController;
import main.resources.ConnectionManager;
import javax.swing.*;
public class MapModel {
List<String> coordinateList = new ArrayList<String>(); //deze moet naar fillCoordinateListArray
public List<String> fillCoordinateListArray(int selected){
try (Connection connection = ConnectionManager.getConnection()) {
String query = "SELECT RouteLocationID, RouteLocations.RouteID, X, Y FROM RouteLocations WHERE RouteID = ?";
PreparedStatement stmt = connection.prepareStatement(query);
stmt.setInt(1, 1);
ResultSet results = stmt.executeQuery();
while (results.next()) {
double X = results.getDouble(3);
double Y = results.getDouble(4);
coordinateList.add(X + "," + Y);
}
} catch (SQLException e) {
System.out.println("Er is een fout opgetreden.");
e.printStackTrace();
}
return coordinateList;
} //method end
public String parseCoordinates(){
String text = "";
for(int i = 0; i<coordinateList.size(); i++) {
text+="&markers=label:" + (i+1) + "%7C" + coordinateList.get(i) + "%7C";
}
return text;
} //method end
public ArrayList getAllRoutes(){
ArrayList<String> routeArrayList = new ArrayList<String>();
try (Connection connection = ConnectionManager.getConnection()) {
String query = "SELECT RouteID, RouteName FROM route";
PreparedStatement stmt = connection.prepareStatement(query);
ResultSet results = stmt.executeQuery();
while (results.next()) {
String routeName = results.getString(2);
routeArrayList.add(routeName);
}
} catch (SQLException e) {
System.out.println("Er is een fout opgetreden.");
e.printStackTrace();
}
System.out.println(routeArrayList);
return routeArrayList;
} //method end

} //class end

我的控制器:

import main.app.view.MapView;
import main.java.models.MapModel;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.*;
public class MapController {
private MapView view;
private MapModel model;
public MapController(MapView view, MapModel model) {
this.view = view;
MapController.ListenerOfActions listener = new MapController.ListenerOfActions();
view.addListenerOfActions(listener);
this.model = model;
this.model.getAllRoutes();
ArrayList<String> routes = model.getAllRoutes();
this.view.getRouteComboBox().getSelectedItem();
this.view.setComboBoxValues(routes);
System.out.println(routes);
this.updateMap(1);
this.view.setVisible(true);
} //constructor end
private void updateMap(int routeID){
model.fillCoordinateListArray(routeID);
Image image = null;
try {
URL url = new URL("https://maps.googleapis.com/maps/api/staticmap?" +
"&size=600x450" +
"&maptype=roadmap" +
this.model.parseCoordinates() +
//"San+Francisco,CA" + "%7C" +
//"&markers=label:1%7C40.702147,-74.015794" + "%7C" +
"&key=AIzaSyAbLM94WcbkB-cf_ubHXOHmCDSsNWEz7XE");
image = ImageIO.read(url);
//  System.out.print(url);
} catch (IOException e) {
System.out.println("Ongeldige URL");
e.printStackTrace();
}
this.view.setImage(image);
this.view.repaint();
} //method end
class ListenerOfActions implements ActionListener {
int selected = 0;
@Override
public void actionPerformed(ActionEvent e) {
String actionCommand = e.getActionCommand();
if(actionCommand.equals("OK")) {
int selected = view.getRouteComboBox().getSelectedIndex() + 1;
System.out.println(selected);
System.out.println("hallo");
updateMap(selected);
}
}
public int getSelected(){
return this.selected;
}

} //class end
} //class end

我的观点:

package main.app.view;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.border.Border;

import main.java.models.MapModel;
import main.resources.ConnectionManager;
public class MapView extends JPanel {
public Image image;
private JLabel jlImage;
private JLabel jlName;
private static JButton jbOk;
private ArrayList<String> comboBoxValues;
private JComboBox routeComboBox = new JComboBox();
//private JComboBox<ArrayList<>>;
private ActionListener actionListener;
public MapView(){
super(new FlowLayout());
setSize(900, 450);
this.add(getRouteComboBox());
jbOk = new JButton("OK");
jbOk.setActionCommand("OK");
jbOk.addActionListener(this.actionListener);
add(jbOk);
// comboBoxValues.toArray();
} //constructor end
public void setComboBoxValues(ArrayList<String> comboBoxValues) {
this.comboBoxValues = comboBoxValues;
}
public void setImage(Image image){
this.image = image;
}

public MapView getView(){
jlImage = new JLabel(new ImageIcon(this.image));
add(jlImage);
return this;
}
public void addListenerOfActions(ActionListener listenForAction) {
this.actionListener = listenForAction;
}
public JComboBox getRouteComboBox(){
return routeComboBox;
}
} //class end

空的JComboBox图片: 在此处输入图像描述

在我看来,我创建了一个新的JComboBox,一个新的ArrayList comboBoxValues和一个新的方法setComboBoxValues((,它只填充ArrayList comboBoxValues。

所以你有一个包含数据的 ArrayList。

如何期望数据神奇地显示在组合框中?答案是不会。

您需要将数据从 ArrayList 复制到setComboBoxValues(…)方法中的组合框中:

像这样:

for (String item: comboBoxValues)
{
comboBox.addItem( item );
}

事实上,你的视图类中甚至不需要 ArrayList,因为数据将存储在组合框的模型中。

阅读 Swing 教程中有关如何使用组合框的部分,了解基础知识。

最新更新