0

i have a created a java frame that takes in info from a user....its kind of an appointment book and i want to add a text field that accepts date and time from the user...

package notifyme;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.util.Hashtable;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

public class InputWindow extends JFrame {

    private JLabel EQUIPMENT;
    private JLabel Location;
    private JLabel DATE;
    private JLabel TIME,TYPE,space,space1;
    private JButton enter;
    private JComboBox location1, equipment1,type1;
    private Hashtable<Object, Object> subItems = new Hashtable<Object, Object>();
    private JTextField time;
    DateComboBox dcb = new DateComboBox();

    private static String[] Lnames = {"SelectOne","Addis Ababa", "Dire Dawa", "Mekelle", "Arbaminch", "Gode", "Gore", "Gawassa"};
   private static String[] Elist = {
       "SelectOne","COMMUNICATION","NAVIGATION","SURVEILLANCE"};
   private static String[] Clist = {
       "SelectCommEqui","VHF121.9","VHF118.1","125.1","125.2","VSAT"};
   private static String[] Nlist = {
       "SelectNavEqui","VOR","DME","ILS","DVOR","GP","AB Beacon","BL Beacon"};
   private static String[] Slist = {
       "SelectSurveillanceEqui","PSR","SSR","ADS-B",};
    public InputWindow(){
        super("Notification Input Window");
        setLayout(new FlowLayout());
         final Container pane = getContentPane();
        pane.setLayout(new GridLayout(8,8));

         EQUIPMENT = new JLabel("EQUIPMENT");
         TYPE =new JLabel("TYPE");
         equipment1= new JComboBox(Elist);
         SelectType st = new SelectType();
         equipment1.addActionListener(st);
        //equipment1.addItemListener(this);
         type1 = new JComboBox();
         //type.addItemListener(this);
         String[] subItems1 = {
       "SelectCommEqui","VHF121.9","VHF118.1","125.1","125.2","VSAT"};
        subItems.put(Elist[1], subItems1);
        String[] subItems2 = {
       "SelectNavEqui","VOR","DME","ILS","DVOR","GP","AB Beacon","BL Beacon"};
        subItems.put(Elist[2], subItems2);
        String[] subItems3 = {
       "SelectSurveillanceEqui","PSR","SSR","ADS-B",};
        subItems.put(Elist[3], subItems3);
//      mainComboBox.setSelectedIndex(1);




         Location = new JLabel("LOCATION");
         location1 = new JComboBox(Lnames);
         DATE = new JLabel("DATE");
         TIME = new JLabel("TIME");
         time = new JTextField();
         space = new JLabel();
         space1 = new JLabel();
         enter = new JButton("ENTER");

         pane.add(EQUIPMENT);
       pane.add(equipment1);
       pane.add(TYPE);
         pane.add(type1);
         pane.add(Location);
         pane.add(location1);
         pane.add(DATE);
         pane.add(dcb);
         pane.add(TIME);
         pane.add(time);
         pane.add(space);
         pane.add(space1);
         pane.add(enter);
         setDefaultCloseOperation(EXIT_ON_CLOSE);
         setSize(1300,1200);
         setVisible(true);
}
    class SelectType implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        String item = (String) equipment1.getSelectedItem();
        Object o = subItems.get(item);
        if (o == null) {
            type1.setModel(new DefaultComboBoxModel());
        } else {
            type1.setModel(new DefaultComboBoxModel((String[]) o));
        }
    }
}  
}

i have a created a java frame that takes in info from a user....its kind of an appointment book and i want to add a text field that accepts date and time from the user...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 1
    The short answer is don't, it's a mess. You could use a `JXDatePicker` (SwingLab's SwingX libary) to get the date and maybe a combobox or two for the time, maybe something like [this](http://stackoverflow.com/questions/30111020/show-the-time-in-a-combobox/30111134#30111134) – MadProgrammer Jun 19 '15 at 06:58
  • Now, if you're really crazy, you could use something like [this](http://stackoverflow.com/questions/11881301/best-way-to-constrain-user-to-enter-a-time-in-a-jtextfield/11881681#11881681) for getting the time from the user. – MadProgrammer Jun 19 '15 at 07:02
  • thanks for the help....but if i take your code it would be like adding a frame to a frame and i dont think its possible and im not experienced enough to take the important stuff and add it to my code – hermela belachew Jun 19 '15 at 07:22
  • isnt it possible to parse whatever the user puts in the text field to a time format – hermela belachew Jun 19 '15 at 07:23
  • time input is very important to my program – hermela belachew Jun 19 '15 at 07:24
  • The `TimeField` example code doesn't have any frame's in it... – MadProgrammer Jun 19 '15 at 07:24
  • The question is, do you really want to mess with post validation of the field, because `JTextField` will allow the user to enter ANY thing, it's not a particularly nice user experience. You could also use a couple of `JSpinner`'s to constraint the user's input, better idea then a `JTextField` – MadProgrammer Jun 19 '15 at 07:25
  • 1
    You could try fiddeling with [JFormattedTextField](http://docs.oracle.com/javase/7/docs/api/javax/swing/JFormattedTextField.html) and [DateFormat](http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html) or [SimpleDateFormat](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html). I guess something like time = new JFormattedTextField(DateFormat.getTimeInstance()); should come close to what you want. – Denis Jun 19 '15 at 07:37
  • i got this code for a spinner.... – hermela belachew Jun 19 '15 at 07:54
  • Date date = new Date(); SpinnerDateModel sm = new SpinnerDateModel(date, null, null, Calendar.HOUR_OF_DAY); jSpinner1 = new javax.swing.JSpinner(sm); JSpinner.DateEditor de = new JSpinner.DateEditor(jSpinner1, "HH:mm:ss"); jSpinner1.setEditor(de); – hermela belachew Jun 19 '15 at 07:55
  • Date date = new Date();SpinnerDateModel sm = new SpinnerDateModel(date, null, null, Calendar.HOUR_OF_DAY); jSpinner1 = new javax.swing.JSpinner(sm); JSpinner.DateEditor de = new JSpinner.DateEditor(jSpinner1, "HH:mm:ss"); jSpinner1.setEditor(de); – hermela belachew Jun 19 '15 at 07:55
  • i have one more question.....after the user fills in my frame, i want the data to be displayed on a table when they hit enter.....tnx....dnt mean to be a pain in the butt..lol – hermela belachew Jun 19 '15 at 07:59

0 Answers0