Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Opening a java file as popup window

 
ponselvi murali
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a java swing app with some data entry screens. In one of the screens I need to choose the date from date picker.I have separate java class for java date picker.But I need to open the date picker as Popup window and selecting the any of the date I need to close the popup and selected date should set into a textfield in parent window.

I dont have idea on

1.Opening a date picker java file as popup window
2.Returning a selected date to parent window.

please help me on this..

here is the code for DatePickerSimple.java which needs to be opened as popup window
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class DatePickerSimple extends JFrame
{
JButton[] btn = new JButton[49];
int month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
int year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);;
JLabel lbl = new JLabel("",JLabel.CENTER);

public DatePickerSimple()
{
buildGUI();
setDates();
}
public void buildGUI()
{
setLocation(300,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
String[] header = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
JPanel midPanel = new JPanel(new GridLayout(7,7));
//midPanel.setPreferredSize(new Dimension(200,200));
for(int x = 0; x < btn.length; x++)
{
final int selection = x;
btn[x] = new JButton();
btn[x].setFocusPainted(false);
if(x>6)btn[x].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
displayDatePicked(btn[selection].getActionCommand());}});
if(x < 7){btn[x].setFont(new Font("Lucida",Font.PLAIN,10)); btn[x].setText(header[x]);}
midPanel.add(btn[x]);
}
JPanel lowPanel = new JPanel(new GridLayout(1,3));
JButton prevBtn = new JButton("<< Previous");
prevBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
month--;setDates();}});
lowPanel.add(prevBtn);
lowPanel.add(lbl);
JButton nextBtn = new JButton("Next >>");
nextBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
month++;setDates();}});
lowPanel.add(nextBtn);
getContentPane().add(midPanel,BorderLayout.CENTER);
getContentPane().add(lowPanel,BorderLayout.SOUTH);
pack();
}
public void setDates()
{
for(int x = 7; x < btn.length; x++) btn[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++) btn[x].setText(""+day);
lbl.setText(sdf.format(cal.getTime()));
setTitle("Date Picker - "+lbl.getText());
}
public void displayDatePicked(String day)
{
if(day.equals("") == false)
{
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("EEEE d MMMM, yyyy");
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.set(year,month,Integer.parseInt(day));
JOptionPane.showMessageDialog(this,"You picked "+sdf.format(cal.getTime()));

}
}

public static void main(String[] args){
new DatePickerSimple().setVisible(true);
}
}



thanks/
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please edit your post to UseCodeTags. It's unnecessarily hard to read the code as it is, making it less likely that people will bother to do so.
 
ponselvi murali
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ponselvi murali wrote:Hi,

I have a java swing app with some data entry screens. In one of the screens I need to choose the date from date picker.I have separate java class for java date picker.But I need to open the date picker as Popup window and selecting the any of the date I need to close the popup and selected date should set into a textfield in parent window.

I dont have idea on

1.Opening a date picker java file as popup window
2.Returning a selected date to parent window.

please help me on this..

here is the code for DatePickerSimple.java which needs to be opened as popup window
<code>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class DatePickerSimple extends JFrame
{
JButton[] btn = new JButton[49];
int month = java.util.Calendar.getInstance().get(java.util.Calendar.MONTH);
int year = java.util.Calendar.getInstance().get(java.util.Calendar.YEAR);;
JLabel lbl = new JLabel("",JLabel.CENTER);

public DatePickerSimple()
{
buildGUI();
setDates();
}
public void buildGUI()
{
setLocation(300,300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
String[] header = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"};
JPanel midPanel = new JPanel(new GridLayout(7,7));
//midPanel.setPreferredSize(new Dimension(200,200));
for(int x = 0; x < btn.length; x++)
{
final int selection = x;
btn[x] = new JButton();
btn[x].setFocusPainted(false);
if(x>6)btn[x].addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
displayDatePicked(btn[selection].getActionCommand());}});
if(x < 7){btn[x].setFont(new Font("Lucida",Font.PLAIN,10)); btn[x].setText(header[x]);}
midPanel.add(btn[x]);
}
JPanel lowPanel = new JPanel(new GridLayout(1,3));
JButton prevBtn = new JButton("<< Previous");
prevBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
month--;setDates();}});
lowPanel.add(prevBtn);
lowPanel.add(lbl);
JButton nextBtn = new JButton("Next >>");
nextBtn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
month++;setDates();}});
lowPanel.add(nextBtn);
getContentPane().add(midPanel,BorderLayout.CENTER);
getContentPane().add(lowPanel,BorderLayout.SOUTH);
pack();
}
public void setDates()
{
for(int x = 7; x < btn.length; x++) btn[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++) btn[x].setText(""+day);
lbl.setText(sdf.format(cal.getTime()));
setTitle("Date Picker - "+lbl.getText());
}
public void displayDatePicked(String day)
{
if(day.equals("") == false)
{
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("EEEE d MMMM, yyyy");
java.util.Calendar cal = java.util.Calendar.getInstance();
cal.set(year,month,Integer.parseInt(day));
JOptionPane.showMessageDialog(this,"You picked "+sdf.format(cal.getTime()));

}
}

public static void main(String[] args){
new DatePickerSimple().setVisible(true);
}
}
</code>



thanks/
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those aren't code tags, that's just quoting your entire post.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic