2

EDITED 27/7/2014

PLEASE READ CAREFULLY AS MY QUESTION IS A BIT COMPLICATED

Hi, I wanted to do a coding whereby it involves a JAVA GUI, batch file and command prompt.

I got part of my answer from this website: Batch scripting multiple selection

Here's what I have now in my batch file [FOR EXAMPLE]:

echo.
echo Selection time!
echo.
echo 1. My father is Joe
echo 2. My mother is Audrey
echo 3. My brother is Jerry
echo 4. My elder sister is June
echo 5. My youngest sister is Awy
echo 6. Include All
echo.

:getOptions
set /p "choices=Type the number without spacing (e.g. 1,2,3): "

if not defined choices ( 
    echo Please enter a valid option
    goto getOptions
    )

for %%a in (%choices%) do if %%a EQU 6 set choices=1,2,3,4,5
for %%i in (%choices%) do call :option-%%i

echo.
echo Done
pause
exit

:option-1
echo My father is Joe > Family.txt
exit /B

:option-2
echo My mother is Audrey > Family.txt
exit /B

:option-3
echo My brother is Jerry > Family.txt
exit /B

:option-4
echo My elder sister is June > Family.txt
exit /B

:option-5
echo My youngest sister is Awy > Family.txt
exit /B

Next, with that, I would also like to include this batch file into a java GUI whereby there will be a few checkboxes for the user to select and when the user ticked box #1, box #2 and box #3 or maybe it would be ticking the check box in sequence but then when the user clicked "OK". It will pass the ticked boxes value to the batch file (it will become 1,2,3 or 1,3,2 or 2,3,1) and then it will run in the command prompt.

Here's what I have now in my java file [FOR EXAMPLE]:

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JCheckBox;
import java.awt.Window.Type;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class GUI extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
           public void run() {
                try {
                   GUI frame = new GUI();
                   frame.setVisible(true);
               } catch (Exception e) {
                    e.printStackTrace();
                }
            }
       });
    }

    /**
    * Create the frame.
    */
   public GUI() {
        setTitle("FAMILY");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JCheckBox chckbxMyFatherIs = new JCheckBox("My Father is Joe");
        chckbxMyFatherIs.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            }
        });
        chckbxMyFatherIs.setBounds(45, 48, 137, 23);
        contentPane.add(chckbxMyFatherIs);

        JCheckBox chckbxNewCheckBox = new JCheckBox("My Mother is Audrey");
        chckbxNewCheckBox.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        chckbxNewCheckBox.setBounds(196, 48, 198, 23);
        contentPane.add(chckbxNewCheckBox);

        JCheckBox chckbxNewCheckBox_1 = new JCheckBox("My Bother is Jerry");
        chckbxNewCheckBox_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        chckbxNewCheckBox_1.setBounds(45, 97, 137, 23);
        contentPane.add(chckbxNewCheckBox_1);

        JCheckBox chckbxNewCheckBox_2 = new JCheckBox("My eldest Sister is June ");
        chckbxNewCheckBox_2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        chckbxNewCheckBox_2.setBounds(196, 97, 198, 23);
        contentPane.add(chckbxNewCheckBox_2);

        JCheckBox chckbxNewCheckBox_3 = new JCheckBox("My youngest sister is Awy");
        chckbxNewCheckBox_3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
           }
        });
        chckbxNewCheckBox_3.setBounds(196, 149, 198, 23);
        contentPane.add(chckbxNewCheckBox_3);

        JCheckBox chckbxAll = new JCheckBox("All");
        chckbxAll.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            }
        });
        chckbxAll.setBounds(45, 149, 97, 23);
        contentPane.add(chckbxAll);
    }
}

I just learnt coding java but coding java GUI is a bit difficult for me. I have started coding more than just the above coding in the batch file. That's why I came here to seek help. I hope my explanation is clear enough. Please feel free to ask me anything if you are unclear with my question. Any help would be greatly appreciated!

So... My question is how to integrate batch scripting into JAVA GUI??

Community
  • 1
  • 1
Steven
  • 107
  • 2
  • 14

2 Answers2

0

You can write a Java program and use Scanner to take input from user:

Scanner in = new Scanner(System.in);

You can invoke this program from a batch file using following command:

@ECHO OFF

%JAVA_HOME%\bin\java MyClass

Have given you pointers and leave rest to you to figure out.

Cheers !!

Sachin Thapa
  • 3,559
  • 4
  • 24
  • 42
0

Change this line:

if %choices% equ 6 set choices=1,2,3,4,5

by this one:

if "%choices:6=%" neq "%choices%" set choices=1,2,3,4,5

I also suggest you to use arrays.

EDIT: Example added

@echo off

:getOptions
set "choices="
set /P "choices=Choices: "
if not defined choices goto :EOF
if "%choices:6=%" neq "%choices%" set choices=1,2,3,4,5,6
echo Execute: %choices%
goto getOptions

Example output:

C:\> test.bat
Choices: 1,3,5
Execute: 1,3,5
Choices: 1,2,4,6
Execute: 1,2,3,4,5,6
Choices: 1,6
Execute: 1,2,3,4,5,6
Choices:

EDIT: I obviously made the error of also include 6 in the replacement, but you get the point!

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • it is unable to recognise `1,6` when i typed that in cmd – Steven Jul 25 '14 at 12:03
  • See the edit in my answer... Please, explain clearly what do you mean with "unable to recognise"... – Aacini Jul 25 '14 at 12:29
  • Aacini, actually i do kind of understand your coding. But for your coding, it is unable to execute one by one. it is like. %choices% = `1,2,3` but the problem is I don't know how to point the option 1 to execute the "My father is Joe" then it will carry on to option 2 etc – Steven Jul 25 '14 at 13:11
  • i also tried using `for /f "delims=, tokens=1-5" %%i in ("%choices%") do (set i=%%i set j=%%j set k=%%k set l=%%l set m=%%m )` whereby it separates the choices from if `1,2,3` e.g. "i" would be assigned with 1. But I do not know how to make it change whereby it will goto the option set. – Steven Jul 25 '14 at 13:18
  • See [my answer](http://stackoverflow.com/questions/24809638/allowing-multiple-choice-selections-for-users-using-batch-file/24813986#24813986) at the same link you posted above... – Aacini Jul 25 '14 at 15:14
  • Yes, I have read your answer. But it was only half of what I wanted. For example: I give 5 options and the 6th option is running all 5 of it. So for ur answer given, it only allows it to run one number which is 1,2,3,4,5 **OR** 6. But it does not allow the user to include multiple choices like **1,2,3** if the user does not want **4,5**. Do you understand what I want? Sorry if my explanation isn't clear enough – Steven Jul 26 '14 at 14:55