i made this a datepicker class which extends the JFXPanel so i could use the datepicker feature in my swing application. Everything was going well until i got stuck on how to get the date from the textfield into my textarea.. i tried using a String field n stored the LocalDate object assigned to it using the toString() method but it keeps coming back as null.
heres the code:
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);
}
}
There are number of problems, but your first is creating a new instance of DatePickin
in your CompHandler
private class CompHandler implements ActionListener
{
DatePickin date = new DatePickin();
This has nothing to do with the instance of DatePickin
that is on the screen, so it will always be null
.
Instead, you should pass a reference of DatePickin
to CompHandler
private class CompHandler implements ActionListener {
private DatePickin pickin;
public CompHandler(DatePickin pickin) {
this.pickin = pickin;
}
Next, I wouldn't get the String
representation of LocalDate
, you should get a reference to the LocalDate
and format as you need. You should also limit people from been able to access you class fields and favor getters instead
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;
}
Then, when the No Change
button is clicked, you can just grab the value of LocalDate
and do with it as you please...
if (button_command.equals("No Change")) {
LocalDate newbDate = pickin.getDateValue();
display_Area.setText("The date is " + newbDate);
display_Area.setFont(new Font("Serif", Font.BOLD, 18));
display_Area.setForeground(Color.black);
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments