How to set the date switch directly below the button

I want to open the date picker by clicking a button. But when I click on the button, it will show the calendar in the upper left corner of the screen. I want to see it just below the button. How can I do it?

Also, how can I resize the calendar to make it a little small?

The following is an example.


package practiceproblems; import java.awt.*; import java.awt.event.*; import javax.swing.*; class DatePicker { int month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH); int year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR); JLabel l = new JLabel("", JLabel.CENTER); String day = ""; JDialog d; JButton[] button = new JButton[49]; public DatePicker(JFrame parent) { d = new JDialog(); d.setModal(true); String[] header = { "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" }; JPanel p1 = new JPanel(new GridLayout(7, 7)); p1.setPreferredSize(new Dimension(430, 120)); for (int x = 0; x < button.length; x++) { final int selection = x; button[x] = new JButton(); button[x].setFocusPainted(false); button[x].setBackground(Color.white); if (x > 6) button[x].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { day = button[selection].getActionCommand(); d.dispose(); } }); if (x < 7) { button[x].setText(header[x]); button[x].setForeground(Color.red); } p1.add(button[x]); } JPanel p2 = new JPanel(new GridLayout(1, 3)); JButton previous = new JButton("<< Previous"); previous.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { month--; displayDate(); } }); p2.add(previous); p2.add(l); JButton next = new JButton("Next >>"); next.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { month++; displayDate(); } }); p2.add(next); d.add(p1, BorderLayout.CENTER); d.add(p2, BorderLayout.SOUTH); d.pack(); d.setLocationRelativeTo(parent); displayDate(); d.setVisible(true); } public void displayDate() { for (int x = 7; x < button.length; x++) button[x].setText(""); java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("MMMM yyyy"); java.util.Calendar cal = java.util.Calendar.getInstance(); cal.set(year, month, 1); int dayOfWeek = cal.get(java.util.Calendar.DAY_OF_WEEK); int daysInMonth = cal.getActualMaximum(java.util.Calendar.DAY_OF_MONTH); for (int x = 6 + dayOfWeek, day = 1; day <= daysInMonth; x++, day++) button[x].setText("" + day); l.setText(sdf.format(cal.getTime())); d.setTitle("Date Picker"); } public String setPickedDate() { if (day.equals("")) return day; java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd-MM-yyyy"); java.util.Calendar cal = java.util.Calendar.getInstance(); cal.set(year, month, Integer.parseInt(day)); return sdf.format(cal.getTime()); } } class Picker { public static void main(String[] args) { JLabel label = new JLabel("Selected Date:"); final JTextField text = new JTextField(20); JButton b = new JButton("popup"); JPanel p = new JPanel(); p.add(label); p.add(text); p.add(b); final JFrame f = new JFrame(); f.getContentPane().add(p); f.pack(); f.setVisible(true); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { text.setText(new DatePicker(f).setPickedDate()); } }); } } 
+4
source share
2 answers

Thus, the code required to display the calendar under the button is shown below. But before you can use it, there are several changes in your code that you must (1), must (2) and could (3) make.

  • In the DatePicker constructor, get rid of d.setVisible (true); since we are going to make some settings in the action listener before showing the dialog.
  • When you create a frame, it is always useful to add this line f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); therefore, your application will always be closed, otherwise you will get a tone of applications consuming the resources of your computer.
  • I would recommend for Date Picker to extend JDialog instead of having the dialog box as a variable. Because what’s like picking a date in your code is effective.
 b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { DatePicker dp = new DatePicker(f); Point bP = b.getLocationOnScreen(); dp.d.setLocation(bP.x, bP.y + b.getHeight()); dp.d.setVisible(true); text.setText(dp.setPickedDate()); } }); 

When it comes to the second part of your question, taking into account the resizing, Andrei Thompson answered this excellently, since you are already calling the package, any size modification is undesirable.

Enjoy it, borough.

+3
source

I want to see it just below the button. How can I do it?..

Component.setLocation (int, int) or Window.setLocationRelativeTo (Component) .

.. How can I resize the calendar to make it a little small?

Component.setSize (int, int) or as suggested by trashgod, call Window.pack () .

The latter is better, since pack() does a heavy lifting by calculating how small a window can be (frame, dialog, etc.) without truncating the content. It is not easy to determine the minimum size.

+2
source

Source: https://habr.com/ru/post/1347171/


All Articles