When it comes to asking a user for data, I start from the premise that he is not only the dumbest guy in the world, but that his only neuron is used to create problems for me... and you don't want to use external libraries, well, let's make our own chooser:
public class Gui extends JDialog {
Principal principal;
JTextField days[], yearInput;
JComboBox<String> months;
JPanel panel;
JButton button;
JLabel messages;
// "first" and "last" will help us to determine which of the fields
// that make up the grid, we should use
int first = 50, last = 41, year, month, day;
public Gui( Principal pri ) {
principal = pri;
setTitle( "MyDateChooser" );
initializeComponents();
createAndSet();
setFields();
setPositionAndSize();
setComponents();
addListeners();
addToPanel( button, messages, yearInput, months );
add( panel );
setVisible( true );
}
private void setFields() {
int one = 1;
int x = 10;
int y = 30;
for( int i = 0; i < days.length; i ++ ) {
if( i % 7 == 0 ) {
x = 10;
y += 26;
}
if( i < first || i >= last ) {
days[ i ].setVisible( false );
}
else {
days[ i ].setVisible( true );
days[ i ].setText( "" + one ++ );
days[ i ].setBounds( x, y, 30, 24 );
}
x += 30;
}
}
private void initializeComponents() {
panel = new JPanel( null );
button = new JButton( "Done" );
messages = new JLabel( "Enters the year" );
yearInput = new JTextField();
}
private void createAndSet() {
days = new JTextField[ 42 ];
for( int i = 0; i < days.length; i ++ ) {
days[ i ] = new JTextField();
days[ i ].setBackground( new Color( 130, 130, 130 ) );
days[ i ].setHorizontalAlignment( JTextField.RIGHT );
days[ i ].setEditable( false );
panel.add( days[ i ] );
days[ i ].addMouseListener( new MouseAdapter() {
@Override
public void mouseClicked( MouseEvent evt ) {
createDate( evt );
}
} );
}
String m[] = {
"Enero", "Febrero", "Marzo", "Abril",
"Mayo", "Junio", "Julio", "Agosto",
"Septiembre", "Octubre", "Noviembre", "Diciembre" };
months = new JComboBox<>( m );
String[] nameOfDays = "LMMJVSD".split( "" );
JLabel[] daysInitials = new JLabel[ 7 ];
int x = 22;
for( int i = 0; i < 7; i ++ ) {
daysInitials[ i ] = new JLabel( nameOfDays[ i ] );
daysInitials[ i ].setBounds( x, 35, 30, 24 );
daysInitials[ i ].setForeground( Color.yellow );
panel.add( daysInitials[ i ] );
x += 30;
}
}
private void setPositionAndSize() {
setBounds( 10, 10, 240, 258 );
button.setBounds( 150, 10, 70, 24 );
messages.setBounds( 15, 10, 100, 24 );
yearInput.setBounds( 100, 10, 50, 24 );
months.setBounds( 140, 10, 80, 24 );
}
private void setComponents() {
months.setVisible( false );
panel.setBackground( Color.darkGray );
messages.setForeground( Color.LIGHT_GRAY );
yearInput.setBackground( new Color( 170, 170, 170 ) );
requestFocusInWindow();
setFocusable( true );
}
private void addListeners() {
button.addActionListener( ( evt ) -> {
// after entering the year we verify that the string
// is not empty and that it only contains numbers.
String text = yearInput.getText();
if( ! text.isEmpty() && text.matches( "^[1-9]\\d*$" ) ) {
year = Integer.parseInt( text );
messages.setText( "Select month" );
button.setVisible( false );
months.setVisible( true );
yearInput.setVisible( false );
}
} );
months.addActionListener( ( evt ) -> {
// based on the user's selection, we instantiate “month” we create a “LocalDate”
// object of the first day of the month, from it, we obtain the day of the
// week and the number of days of the month, with this data we instantiate
// “first” and ‘last’ and call “setFields” to show the calendar.
month = months.getSelectedIndex() + 1;
LocalDate aux = LocalDate.of( year, month, 1 );
int dayOfWeek = aux.getDayOfWeek().getValue();
int DayOfMonth = aux.lengthOfMonth();
first = dayOfWeek + 1;
last = first + DayOfMonth;
setFields();
} );
}
private void addToPanel( JComponent... comps ) {
for( JComponent comp : comps ) {
panel.add( comp );
}
}
// the listener of the JTextField on which the user clicked, calls this method that
// instantiates “day” with the string of the same and invokes the corresponding
// method of the object that created the ‘chooser’ passing it as a parameter, the
// object “LocalDate”.
void createDate( MouseEvent evt ) { // and send
day = Integer.parseInt( ( (JTextComponent) evt.getSource() ).getText() );
principal.setFecha( LocalDate.of( year, month, day ) );
dispose();
}
}
PS: it is a basic version, some things need to be modified and others implemented (like going backwards), I hope it will serve as an inspiration for you.