我在DatePicker中选择日期时如何获取文本字段值

时间:2015-04-20 07:46:12

标签: java swing javafx datepicker

我做了一个datepicker类,扩展了JFXPanel,所以我可以在swing应用程序中使用datepicker功能。一切顺利,直到我陷入如何从文本字段到我的textarea的日期...我尝试使用字符串字段n存储使用toString()方法分配给它的LocalDate对象,但它一直返回为null。

继承人代码:

public class FNAFrame extends JFrame {

public FNAFrame()
{
    super ("FNA Comments Generator");
    setLayout(new BorderLayout());

    setResizable(false);
    TextFrame comps = new TextFrame();
    add(comps);
    pack();
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public static void main(String[] args) {

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
                // 
            } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) 
            {
                ex.printStackTrace();
            }


            new FNAFrame();

        }
    });
 }  
} // end of class FNA Frame


public class TextFrame extends JPanel {
// variable declarations
private JLabel newbLabel;

private JButton noChange_Button;

private JTextArea display_Area;

// end of variable declarations

public TextFrame()
{
    super(new GridBagLayout());

    setPreferredSize(new Dimension(300,200));
    setBackground(Color.white);

    init();  
  } // end of class constructor

    private void init()
    { 
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10,10,10,10);

        // date picker
        DatePickin date_Picker = new DatePickin();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.BOTH;
        gbc.anchor = GridBagConstraints.EAST;
        add(date_Picker, gbc);

        // button to display date in textarea
        noChange_Button = new JButton("No Change");
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.WEST;
        add(noChange_Button, gbc);

        ///////////////////// TEXT AREA ///////////////////////

        display_Area = new JTextArea();
        gbc.gridx = 0;
        gbc.gridy = 3;
        //gbc.weighty = 1;
        gbc.gridwidth = 3;
        gbc.gridheight = 4;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.anchor = GridBagConstraints.SOUTHWEST;
        display_Area.setEditable(true);
        display_Area.setLineWrap(true);
        display_Area.setWrapStyleWord(true);

        JScrollPane scroll = new JScrollPane(display_Area);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scroll.setPreferredSize(new Dimension(0, 70));
        add(scroll, gbc);

        // adding listeners to components
        // registering all components with their respective listeners
        CompHandler compHandler = new CompHandler();
        noChange_Button.addActionListener(compHandler);
    }

    // class to handle text fields
    private class CompHandler implements ActionListener
    {
        DatePickin date = new DatePickin();

        private String newbDate;

        @Override
        public void actionPerformed(ActionEvent e)  
        {
            Object button_command = e.getActionCommand();

            try {

                if (button_command.equals("No Change"))
                {    
                    newbDate = date.strDate;

                    display_Area.setText("The date is " + newbDate);
                    display_Area.setFont(new Font("Serif", Font.BOLD, 18));
                    display_Area.setForeground(Color.black);
                }
            }
            catch (NullPointerException np)
            {
            }       
        }
       } // end component handler class 
} // end of TextFrame class


public class DatePickin extends javafx.embed.swing.JFXPanel{

private DatePicker date_Picker;
String strDate;
private VBox pane;

public DatePickin()
{
    setLayout(new FlowLayout());
    setPreferredSize(new Dimension(90, 30));

    init();   
} // end of class constructor

private void init()
{
    pane = new VBox();
    pane.setBackground(Background.EMPTY);
    pane.setAlignment(Pos.CENTER_LEFT);
    getDate();
    pane.getChildren().addAll(date_Picker);

    Platform.runLater(this::createScene);
}

public void getDate()
{
    date_Picker = new DatePicker();

    date_Picker.setShowWeekNumbers(false);

    date_Picker.setOnAction((e) -> {

        LocalDate ld;
        try
        {
            //This is where i the problem is
            ld = date_Picker.getValue();

            strDate = ld.toString();
        }
        catch(UnsupportedOperationException uoe)
        {
        }
    });
}

private void createScene()
{
    Scene scene = new Scene(pane);
    setScene(scene);
}    

}

2 个答案:

答案 0 :(得分:0)

有很多问题,但您首先要在DatePickin

中创建CompHandler的新实例
private class CompHandler implements ActionListener
{
    DatePickin date = new DatePickin();

这与屏幕上的DatePickin实例无关,因此它始终为null

相反,您应该将DatePickin的引用传递给CompHandler

private class CompHandler implements ActionListener {

    private DatePickin pickin;

    public CompHandler(DatePickin pickin) {
        this.pickin = pickin;
    }

接下来,我不会得到String的{​​{1}}代表,您应该根据需要获得对LocalDate和格式的引用。您还应该限制人们访问您的类字段并改为使用getter

LocalDate

然后,当点击public class DatePickin extends javafx.embed.swing.JFXPanel { private DatePicker date_Picker; private LocalDate dateValue; //... public void getDate() { date_Picker = new DatePicker(); date_Picker.setShowWeekNumbers(false); date_Picker.setOnAction((e) -> { try { dateValue = date_Picker.getValue(); } catch (UnsupportedOperationException uoe) { uoe.printStackTrace(); } }); } public LocalDate getDateValue() { return dateValue; } 按钮时,您可以抓住No Change的值并随意使用它...

LocalDate

答案 1 :(得分:0)

好吧,我想通了..我做了疯狂的建议,但是在FNAComponents类里面的CompHandler类声明中有一个错误,我把Jcomponents初始化为各自的动作监听器......

这就是我做的事情

private void init()
{ 
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.insets = new Insets(10,10,10,10);

    // date picker
    DatePickin date_Picker = new DatePickin(); /// here i used the same object i created here in the CompHandler class as the arg
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.anchor = GridBagConstraints.EAST;
    add(date_Picker, gbc);

    // button to display date in textarea
    noChange_Button = new JButton("No Change");
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.anchor = GridBagConstraints.WEST;
    add(noChange_Button, gbc);

    ///////////////////// TEXT AREA ///////////////////////

    display_Area = new JTextArea();
    gbc.gridx = 0;
    gbc.gridy = 3;
    //gbc.weighty = 1;
    gbc.gridwidth = 3;
    gbc.gridheight = 4;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.anchor = GridBagConstraints.SOUTHWEST;
    display_Area.setEditable(true);
    display_Area.setLineWrap(true);
    display_Area.setWrapStyleWord(true);

    JScrollPane scroll = new JScrollPane(display_Area);
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    scroll.setPreferredSize(new Dimension(0, 70));
    add(scroll, gbc);

    // adding listeners to components
    // registering all components with their respective listeners
    CompHandler compHandler = new CompHandler(date_Picker); // here u can see i added the reference of DatePicker to the class as an arg
    noChange_Button.addActionListener(compHandler);
} 

我创建了一个对象date_Picker,将面板放在我的swing中,在init方法中..我用作CompHandler类的参数的同一个对象..感谢madpro的方向