skip to Main Content

I have this app that runs in eclipse’s console and I want it to run in a jframe.
By that I mean that I want it to ask for name, a and b on the JFrame window and then write something on a file.

It works perfectly in the console but I don’t know how to run it as a JFrame.

I want it to look something like this(Image made in photoshop):
http://i.imgur.com/rTWko1R.png
And then automaticaly close

Thanks in advance!

some imports...(trying to save space)

public class Test {

    public static void main(String[] args) throws FileNotFoundException,IOException {

    Scanner s = new Scanner(System.in);
    String fileName = new SimpleDateFormat("dd-MM-yyyy_HH-mm'.txt'").format(new Date());
    String obs;
    String name;
    String path = "some path";
    int a = 0, b = 0, c = 0, d = 0;
    System.out.println("input file name");
    name = s.nextLine();
    System.out.println("input a");
    a = s.nextInt();
    System.out.println("input b");
    b = s.nextInt();
    obs = s.nextLine();
    if (a >= 100) {
        d = a / 100;
        c = a % 100;
        b = c;
        a = a + d;
    }
    File file;
    if (StringUtils.isBlank(name)) {
        file = new File(path + fileName);
    } else {
        file = new File(path + name + "#" + fileName);
    }

    FileWriter writer = null;
    try {
        writer = new FileWriter(file);
        writer.write("something");

        if (StringUtils.isBlank(obs)) {
            writer.write("something");
        } else {
            writer.write(obs + "n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (writer != null)
            try {
                writer.close();
            } catch (IOException ignore) {
            }
    }

}
}

2

Answers


  1. What you’ll need to do

    • separate out your core logic into a separate method that takes String name, int a, int b, ideally in a separate class – then you can reuse from your console version
    • Create a basic GUI in a frame with a button to kick off the process
    • listen to the button press and call core logic method
    • add validation of inputs if necessary
    • consider using JFileChooser to allow user to pick the file rather than having to type it in

    Example

    public class ConsoleInFrame {
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new ConsoleInFrame().showGui();
                }
            });
        }
    
        public void showGui() {
            JFrame frame = new JFrame();
            JTextField file = new JTextField(20);
            JTextField aText = new JTextField(4);
            JTextField bText = new JTextField(4);
            JButton go = new JButton("Go");
    
            JPanel panel = new JPanel();
            panel.setLayout(new GridLayout(3, 2));
            panel.add(new JLabel("File"));
            panel.add(file);
            panel.add(new JLabel("a"));
            panel.add(aText);
            panel.add(new JLabel("b"));
            panel.add(bText);
    
            frame.getContentPane().setLayout(
                    new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));
            frame.getContentPane().add(panel);
    
            frame.getContentPane().add(go);
    
            go.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    process(file.getText(), Integer.parseInt(aText.getText()),
                            Integer.parseInt(bText.getText()));
                }
            });
            frame.pack();
            frame.setVisible(true);
        }
    
        public void process(String name, int a, int b) {
            String fileName = new SimpleDateFormat("dd-MM-yyyy_HH-mm'.txt'")
                    .format(new Date());
            String obs;
            String path = "some path";
            int c = 0, d = 0;
            if (a >= 100) {
                d = a / 100;
                c = a % 100;
                b = c;
                a = a + d;
            }
            File file;
            if (StringUtils.isBlank(name)) {
                file = new File(path + fileName);
            } else {
                file = new File(path + name + "#" + fileName);
            }
    
            FileWriter writer = null;
            try {
                writer = new FileWriter(file);
                writer.write("something");
    
                if (StringUtils.isBlank(obs)) {
                    writer.write("something");
                } else {
                    writer.write(obs + "n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (writer != null)
                    try {
                        writer.close();
                    } catch (IOException ignore) {
                    }
            }
    
        }
    }
    
    Login or Signup to reply.
  2. I think you could do something like this:

    enter image description here

    To do this you have to use JLabel to display text: https://docs.oracle.com/javase/tutorial/uiswing/components/label.html

    Then to get the input use JTextField:
    https://docs.oracle.com/javase/tutorial/uiswing/components/textfield.html

    And if you want you can use a JButton after you write in the JTextField to save everything to the file:
    https://docs.oracle.com/javase/7/docs/api/javax/swing/JButton.html
    http://www.javamex.com/tutorials/swing/jbutton.shtml

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search