0

I tried to implement a simple user interface of my programm.It is the first time that I used swing designer plugin.

1st problem :User enters start and end date and program automatically calculates the duration. But i dont know how i can write the restriction of the yyyy/MM/dd. I mean that user begins to enter year then skip the another block without make anything.

Here is my code:

public class MyFrame extends JFrame {
    public MyFrame() {
    }

    private static JLabel labelBegin;
    private static JLabel labelEnd;
    private static JLabel labelDuration;
    private static JLabel labelA;
    private static JLabel labelB;
    private static JLabel labelC;
    private static JLabel labelD;
    private static JLabel labelTotal;
    private static JLabel labelSafety;
    private static JLabel labelSpeed;
    private static JLabel labelEfficiency;
    private static JLabel message;
    private static JTextField textFieldBegin;
    private static JTextField EndingField;
    private static JTextField DurationField;
    private static JTextField AField;
    private static JTextField BField;
    private static JTextField CField;
    private static JTextField DField;
    private static JTextField TotalField;
    private static JTextField SafetyField;
    private static JTextField SpeedField;
    private static JTextField EfficiencyField;
    private static JButton clear;
    private static JButton buttonNext;
    private static JButton buttonBack;
    private static JRadioButton PlacementResult;
    private static JRadioButton Collision;
    private static JPanel panelOptions;
    private static JPanel panelLogin;
    private static JFrame frame;
    private static JButton Mybutton;
    private static JPanel panelCons;

    private static void mydesign() {

        frame = new JFrame("Placement Tool V1.0");

        frame.getContentPane().setLayout(new GridLayout(1, 1));

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        panelLogin = new JPanel();
        panelLogin.setLayout(null);
        message = new JLabel ("In order to begin your testing, please enter the data");
        labelBegin = new JLabel("Enter Begin Date :");
        textFieldBegin = new JTextField(10);
        labelEnd = new JLabel("Test Duration :");
        EndingField = new JTextField(10);
        labelDuration = new JLabel ("Enter End Date :");
        DurationField = new JTextField(10);
        labelA = new JLabel ("Enter Group A :");
        AField = new JTextField (10);
        labelB = new JLabel ("Enter Group B:");
        BField = new JTextField (10);
        labelC = new JLabel ("Enter Group C:");
        CField = new JTextField (10);
        labelD = new JLabel ("Enter Group D:");
        DField = new JTextField (10);
        labelTotal = new JLabel ("Total Test Devices :");
        TotalField = new JTextField (10);
        labelSafety = new JLabel ("Enter Safety (%) : ");
        SafetyField = new JTextField (10);
        labelSpeed = new JLabel ("Enter Speed (kpbs)");
        SpeedField = new JTextField (10);
        labelEfficiency = new JLabel ("Enter Efficiency (%)");
        EfficiencyField = new JTextField (10);
        buttonNext = new JButton("Next");
        clear = new JButton("Cancel");

        panelLogin.add(buttonNext);
        panelLogin.add(clear);
        panelLogin.add(labelBegin);
        panelLogin.add(message);
        panelLogin.add(textFieldBegin);
        panelLogin.add(labelEnd);
        panelLogin.add(EndingField);
        panelLogin.add(labelDuration);
        panelLogin.add(DurationField);
        panelLogin.add (labelA);
        panelLogin.add(labelB);
        panelLogin.add(labelC);
        panelLogin.add(labelD);
        panelLogin.add(labelTotal);
        panelLogin.add(AField);
        panelLogin.add(BField);
        panelLogin.add(CField);
        panelLogin.add(DField);
        panelLogin.add(TotalField);
        panelLogin.add(labelSafety);
        panelLogin.add(labelSpeed);
        panelLogin.add(labelEfficiency);
        panelLogin.add(SpeedField);
        panelLogin.add(EfficiencyField);
        panelLogin.add(SafetyField);

        message.setBounds(50, 70, 500, 20);
        textFieldBegin.setBounds(200, 120, 100, 20);
        labelBegin.setBounds(50, 120, 120, 20);

        EndingField.setBounds(200, 140, 100, 20);
        labelEnd.setBounds(50, 160, 120, 20);

        DurationField.setBounds(200, 160, 100, 20);
        labelDuration.setBounds(50, 140, 120, 20);

        AField.setBounds(200, 180, 100, 20);
        labelA.setBounds(50, 180, 120, 20);

        BField.setBounds(200, 200, 100, 20);
        labelB.setBounds(50, 200, 120, 20);

        CField.setBounds(200, 220, 100, 20);
        labelC.setBounds(50, 220, 120, 20);

        DField.setBounds(200, 240, 100, 20);
        labelD.setBounds(50, 240, 120, 20);

        TotalField.setBounds(200, 260, 100, 20);
        labelTotal.setBounds(50, 260, 120, 20);

        SafetyField.setBounds(200, 280, 100, 20);
        labelSafety.setBounds(50, 280, 120, 20);

        SpeedField.setBounds(200, 300, 100, 20);
        labelSpeed.setBounds(50, 300, 120, 20);

        EfficiencyField.setBounds(200, 320, 100, 20);
        labelEfficiency.setBounds(50, 320, 120, 20);


       buttonNext.setBounds(70, 350, 100, 20);
       clear.setBounds(200, 350, 100, 20);

        buttonNext.addActionListener(actionEvent);
        clear.addActionListener(actionEvent);
        frame.getContentPane().add(panelLogin);
        //frame ends.

        //Options frame starts.

        panelOptions = new JPanel();
        panelOptions.setLayout(null);
        PlacementResult = new JRadioButton ("Show the placement result.");
        Collision = new JRadioButton ("Show the collision (%)");
        buttonBack = new JButton("Back");
        buttonNext = new JButton("Next");
        panelOptions.add(buttonBack);
        panelOptions.add(buttonNext);
        panelOptions.add(Collision);
        panelOptions.add(PlacementResult);
        Collision.setBounds(150, 25, 200, 25);
        PlacementResult.setBounds(150, 50, 200, 25);
        buttonBack.setBounds(150, 100, 200, 25);
        buttonNext.setBounds(150, 150, 200, 25);
        buttonBack.addActionListener(actionEvent);
        buttonNext.addActionListener(actionEvent);
        //Options Frame ends.




        panelCons = new JPanel();
        panelCons.setLayout(null);
        Mybutton = new JButton("Button");
        panelCons.add(Mybutton);
        Mybutton.setBounds(180, 180, 150, 25);
        Mybutton.addActionListener(actionEvent);
        frame.setSize(500, 500);
        frame.setVisible(true);
    }


    public static void main(String args[])  {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                mydesign();
            }
        });
    }
    private static ActionListener actionEvent = new ActionListener() {

        public void actionPerformed(ActionEvent e) {
             if(frame == null)
                 frame = new JFrame();
             else {
                 //remove the previous JFrame
                 frame.setVisible(false);
                 frame.dispose();
                 //create a new one
                 frame = new JFrame();
             }
            String action = e.getActionCommand();
            if ("Login".equals(action)) {
                System.out.println("Login action" + e.getActionCommand());
                if ("".equals(textFieldBegin.getText()) || "".equals(EndingField.getText()) ||"".equals(DurationField.getText()) ) {
                    JOptionPane.showMessageDialog(null,
                            "Enter Both begin test date and end test date");

                } else if ("admin".equals(textFieldBegin.getText())&&"admin".equals(AField.getText())&&
                "admin".equals(BField.getText())&&
                "admin".equals(CField.getText())&&"admin".equals(DField.getText())&& "admin".equals(TotalField.getText())&&
                "admin".equals(EfficiencyField.getText()) && "admin".equals(SafetyField.getText()) &&
                "admin".equals(SpeedField.getText()) && "admin".equals(EndingField.getText())) 
                {
                    System.out.println("Test begin date" + textFieldBegin.getText() + " Test end date" + EndingField.getText()
                            //+" Test duration" + (EndingField.getText() - textFieldBegin.getText())
                            +" Group A devices" + AField.getText() +" Group B devices"+ BField.getText()
                            + " Group C devices "+ CField.getText()
                            + "Group D devices"+ DField.getText()
                            + "Total devices" + (AField.getText() + BField.getText() + CField.getText() + DField.getText())
                            +"Max speed (kbps)" + SpeedField.getText()
                            + "Efficiency (%)" + EfficiencyField.getText() + "Safety (%)" + SafetyField.getText()
                            );
                    frame.remove(panelLogin);
                    frame.getContentPane().add(panelOptions);
                    frame.setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(null,
                            "Date format that you entered is not appropriate! Hint: YYYY/MM/DD");

                }
            } else if ("Cancel".equals(action)) {
                System.out.println(e.getActionCommand());
                System.exit(0);

            } 

              {
                frame.remove(panelOptions);
               frame.getContentPane().add(panelCons);

                frame.setVisible(true);
            }

        }
        ;

    };
}

2nd problem is i want to make my program user friendly so i want to make a group every component that are similar each other.Here is output:

Advance thanks for all helps

limonik
  • 499
  • 1
  • 6
  • 28
  • *"I tried to implement a simple user interface.."* You regard at least 33 components, in code that is already over 240 lines, as *simple*? It would be advantageous to work out this sort of stuff with just a couple of components, not 10, not >33! For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). One way to get image(s) for an example is to hot link to images seen in [this Q&A](http://stackoverflow.com/q/19209650/418556). – Andrew Thompson Jul 21 '15 at 10:18
  • *"**3rd problem**"* Not that this is not a problem solving site, nor a help desk. It is a Q&A site that works best when there is at least one (preferably accepted) correct answer to a single specific question. What is the (single, specific) question that you would like to ask on *this thread?* – Andrew Thompson Jul 21 '15 at 10:21
  • @AndrewThompson i mean that simplicity for the users..I will check the documents. Thanks. Yess you are exactly right :) – limonik Jul 21 '15 at 10:22
  • 2
    Oh, and `private static JLabel labelBegin;` .. Use of the `static` keyword in GUIs is more often a sign of bad design than anything else. Remove all the `static` key words and fix it (if necessary) from there. – Andrew Thompson Jul 21 '15 at 10:25
  • *"..i mean that simplicity for the users.."* My bad, and nice of you to try to offer the KISS principal. Too many programmers presume the end user wants 'kewl skins' and other such dross. What users typically want is an app. that does exactly what they want without them even having to think about the app. itself. – Andrew Thompson Jul 21 '15 at 10:27
  • 1
    @AndrewThompson yes sir. I learned "keep it simple stupid" principle in university as a theory but making a practise is exactly different thing. – limonik Jul 21 '15 at 10:30
  • 2
    *"I learned "keep it simple stupid" principle in university as a theory but making a practise is exactly different thing."* Ain't it the truth! It is ironic that implementing the KISS principle in computer programming actually means a **lot more programming.** It is just that we need to carefully hide that complexity from the user - to make it look 'like magic' on screen. ;) – Andrew Thompson Jul 21 '15 at 10:37

1 Answers1

1

SOLUTION :

  1. Use TF(For 4 digits) + LB(For Slash) + TF(For 2 digits) + LB(For Slash) + TF(For 2 digits) to achive that format in date [TF : TextField and LB : Label] OR you can simply validate on Next button click whether date format match the specific format or not or even precisely date is valid or not.
  2. Use JPanel and set border of it for grouping. (Hint : Use BorderFactory)
  3. I didn't get you in this specific problem.SO can't say anything.
akash
  • 22,664
  • 11
  • 59
  • 87