从Jcombo获取所选数据

时间:2023-01-28 11:05:22

Trying to save the users selection into a string to use further in the code. but I'm unsuccessful in retrieving the data that the users selects. not to sure if I'm doing it wrong or right, or how to do it to be honest because i feel as if I'm doing it wrong. the code is commented out near the bottom of the script. and is

尝试将用户选择保存到字符串中以便在代码中进一步使用。但我没有成功检索用户选择的数据。我不确定我做错了什么或做对了,或者说如何做到诚实,因为我觉得我做错了。代码在脚本底部附近注释掉。并且是

 //String stationS = (String)station.getSelectedItem();

and the rest of the code is this just incase for referencing on how i create a jcombobox

其余的代码就是参考我如何创建一个jcombobox

import javax.swing.*;

import java.awt.Dialog.ModalityType;
import java.awt.event.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;

public class TouchOn extends JDialog {
private JPanel mainPanel;

public ArrayList Reader(String Txtfile) {
    try {

    ArrayList<String> Trains = new ArrayList<String>();
    int count = 0;
    String testing = "";
    File file = new File(Txtfile);
    FileReader fileReader = new FileReader(file);
    BufferedReader bufferedReader = new BufferedReader(fileReader);
    StringBuffer stringBuffer = new StringBuffer();
    String line;
    while ((line = bufferedReader.readLine()) != null) 
    {
        stringBuffer.append(line);
        count += count;
        if(!line.contains("*")){
            Trains.add(line + "\n");
        }
        stringBuffer.append("\n");



    }
    fileReader.close();
    //Arrays.asList(Trains).stream().forEach(s -> System.out.println(s));
    return Trains;
} catch (IOException e) {
    e.printStackTrace();
}
//return toString();
return null;




 }


public TouchOn()
{
    setPanels();

    setModalityType(ModalityType.APPLICATION_MODAL);
    setSize(400, 300);
    setVisible(true);
}
public void setPanels()
{
    mainPanel = new JPanel(new GridLayout(0, 2));
    JPanel containerPanel = new JPanel(new GridLayout(0, 1));
    JLabel startDay = new JLabel("Day:");
    JTextField sDay = new JTextField();

    JLabel startMonth = new JLabel("Month:");
    JTextField sMonth = new JTextField();

    JLabel startYear = new JLabel("Year:");
    JTextField sYear = new JTextField("2015");
    String trainline;
    String checked = "";

    JLabel touchOnTimehr = new JLabel("Time Hour: ");
    JLabel touchOnTimem = new JLabel("Time Minute:");
    JLabel station = new JLabel("Station: ");


    JTextField touchOnTimeFieldhour = new JTextField();
    JTextField touchOnTimeFieldminute = new JTextField();

    JPanel lowerPanel = new JPanel(new FlowLayout());
    ArrayList<String> stations = Reader("TrainLines.txt");
    JComboBox<String> cb = new JComboBox<>(stations.toArray(new String[stations.size()]));
    JRadioButton belgrave = new JRadioButton("Belgrave Line");      
    JRadioButton glenwaverly = new JRadioButton("Glen Waverly Line");
    ButtonGroup bG = new ButtonGroup();
    JButton apply = new JButton("Touch on");
    JButton cancel = new JButton("Cancel");
    cancel.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent e){
            dispose();
        }
    });
    apply.addActionListener(new ActionListener()
    {
        @SuppressWarnings("unused")
        public void actionPerformed(ActionEvent e)
        {

            String timestamp = new java.text.SimpleDateFormat("dd/MM/yyyy").format(new Date());
            String day = sDay.getText();
            String month = sMonth.getText();
            String year = sYear.getText();
            String trainline = "";
            String hour = touchOnTimeFieldhour.getText();
            String minute = touchOnTimeFieldminute.getText();
            if(belgrave.isSelected()){
                trainline = belgrave.getText();
            }
            if(glenwaverly.isSelected()){
                trainline = glenwaverly.getText();
            }


            //String stationS = (String)station.getSelectedItem();

            // The code above is the one that doesnt im not to sure how it works 
            System.out.println(trainline);
        }
    });



    cb.setVisible(true);
    bG.add(belgrave);
    bG.add(glenwaverly);
    mainPanel.add(startDay);
    mainPanel.add(sDay);
    mainPanel.add(startMonth);
    mainPanel.add(sMonth);
    mainPanel.add(startYear);
    mainPanel.add(sYear);
    mainPanel.add(touchOnTimehr);
    mainPanel.add(touchOnTimeFieldhour);
    mainPanel.add(touchOnTimem);
    mainPanel.add(touchOnTimeFieldminute);
    mainPanel.add(belgrave);
    mainPanel.add(glenwaverly);
    mainPanel.add(station);
    mainPanel.add(new JLabel());
    mainPanel.add(cb);
    lowerPanel.add(apply);
    lowerPanel.add(cancel);
    touchOnTimeFieldhour.setSize(10,10);
    containerPanel.add(mainPanel);
    containerPanel.add(lowerPanel);

    add(containerPanel);
}



  }

1 个解决方案

#1


2  

IMO the problem is, that you access local variables from inner class. Instead of adding actionListeners to the buttons, you should implement ActionLisntener into the TouchOn and override actionPerformed().

IMO的问题是,您从内部类访问局部变量。您应该将ActionLisntener实现到TouchOn并覆盖actionPerformed(),而不是将actionListeners添加到按钮。

@override
public void actionPerformed(ActionEvent ex){
    Object source = ex.getSource();
    if (source == apply){
        //your code here
    }
    if (source == cancel){
        //your code here
    }

In

//your code here

you can use references to your JRadioButtons

您可以使用对JRadioButtons的引用

#1


2  

IMO the problem is, that you access local variables from inner class. Instead of adding actionListeners to the buttons, you should implement ActionLisntener into the TouchOn and override actionPerformed().

IMO的问题是,您从内部类访问局部变量。您应该将ActionLisntener实现到TouchOn并覆盖actionPerformed(),而不是将actionListeners添加到按钮。

@override
public void actionPerformed(ActionEvent ex){
    Object source = ex.getSource();
    if (source == apply){
        //your code here
    }
    if (source == cancel){
        //your code here
    }

In

//your code here

you can use references to your JRadioButtons

您可以使用对JRadioButtons的引用