有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我试图用数据库中的记录填充组合框。在我的控制器中,我使用MVC,在我的模型中,我有一个方法getAllRoutes(),它将数据库中的所有路由名称放入ArrayList中。此方法的工作原理与我添加的系统相同。出来其中的println()表示ArrayList中确实填充了数据库值

在我的控制器中,我创建了一个名为“routes”的新ArrayList,并使用我在getAllRoutes()方法的返回中放入的ArrayList对其进行初始化。这也适用于我添加的系统。出来println()并打印

在我看来,我创建了一个新的JComboBox、一个新的ArrayList comboBoxValues和一个新的方法setComboxValues(),它只填充ArrayList ComboxValues。回到控制器中,我调用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的图片: enter image description here


共 (1) 个答案

  1. # 1 楼答案

    In my View, I create a new JComboBox, a new ArrayList comboBoxValues and a new method setComboBoxValues() that just fills the ArrayList comboBoxValues.

    因此,您有一个包含数据的ArrayList

    您如何期望数据神奇地出现在组合框中?答案是不会

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

    比如:

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

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

    阅读Swing教程中关于How to Use Combo Boxes的部分了解基本知识