Java JComboBox接受ArrayList但不显示ArrayList项

问题描述 投票:0回答:1

我正在尝试用数据库中的记录填充组合框。在我的控制器中,我使用的是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的图片:enter image description here

java swing model-view-controller arraylist jcombobox
1个回答
1
投票

在我的视图中,我创建了一个新的JComboBox,一个新的ArrayList comboBoxValues和一个新方法setComboBoxValues(),它仅填充了ArrayList comboBoxValues。

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

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

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

类似:

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

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

阅读有关How to Use Combo Boxes的Swing教程中的基础知识。

© www.soinside.com 2019 - 2024. All rights reserved.