0

I am making a software which will pick only default directory after installation and from that file(in directory) a button will be linked to perform the task. I am unable to set and show default path which will look exactly like the given example in windows builder. It must be hard coded.

Example:-
hard coded defined file path

private void initialize() {
        frmPdfPublisher = new JFrame();
        frmPdfPublisher.setIconImage(Toolkit.getDefaultToolkit().getImage("C:\\Users\\Admin\\Desktop\\imageedit_1_6449501097.png"));
        frmPdfPublisher.setTitle("PDF Publisher");
        frmPdfPublisher.setBounds(100, 100, 450, 439);
        frmPdfPublisher.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmPdfPublisher.getContentPane().setLayout(null);

        JButton btnTransform = new JButton("Transform");
        btnTransform.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

            }
        });
        btnTransform.setBounds(76, 167, 89, 23);
        frmPdfPublisher.getContentPane().add(btnTransform);

        JButton btnPreview = new JButton("Preview");
        btnPreview.setBounds(269, 167, 89, 23);
        frmPdfPublisher.getContentPane().add(btnPreview);

        JProgressBar progressBar = new JProgressBar();
        progressBar.setBounds(138, 218, 146, 14);
        frmPdfPublisher.getContentPane().add(progressBar);



        JButton btnQuit = new JButton("Quit");
        btnQuit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        btnQuit.setBounds(176, 288, 89, 23);
        frmPdfPublisher.getContentPane().add(btnQuit);

        JLabel lblLabel1 = new JLabel("Default DITA-OT File :");
        lblLabel1.setBounds(10, 79, 123, 14);
        frmPdfPublisher.getContentPane().add(lblLabel1);





        JSeparator separator = new JSeparator();
        separator.setBounds(10, 140, 414, 2);
        frmPdfPublisher.getContentPane().add(separator);

        JSeparator separator_1 = new JSeparator();
        separator_1.setBounds(10, 257, 414, 2);
        frmPdfPublisher.getContentPane().add(separator_1);

        textField_1 = new JTextField();
        textField_1.setBackground(SystemColor.menu);
        textField_1.setBounds(138, 76, 286, 20);
        frmPdfPublisher.getContentPane().add(textField_1);
        textField_1.setColumns(10);
        textField_1.setBorder(null);
        textField_1.setText(System.getProperty("C:/Program Files/dita-ot-2.4"));



        JMenuBar menuBar = new JMenuBar();
        frmPdfPublisher.setJMenuBar(menuBar);

        JMenu mnHelp = new JMenu("Option");
        menuBar.add(mnHelp);

        JMenuItem mntmHelp = new JMenuItem("Help");
        mnHelp.add(mntmHelp);
    }
}
Rohit Ghosh
  • 53
  • 11
  • For generic part you may use; System.getProperty("user.home") + "\\Desktop" – newuserua_ext Dec 29 '16 at 06:48
  • @newuserua_ext This syntax will be in jFile chooser ? – Rohit Ghosh Dec 29 '16 at 08:42
  • I don't use Swing and don't know anything about JFileChooser. – greg-449 Dec 29 '16 at 08:43
  • @greg-449 ok, Thank You Sir. – Rohit Ghosh Dec 29 '16 at 08:56
  • @newuserua_ext `textField_1 = new JTextField(); textField_1.setBounds(136, 76, 288, 20); frmPdfPublisher.getContentPane().add(textField_1); textField_1.setColumns(10); textField_1.setEnabled(false);` _now I want to add System.getProperty. Where to add this syntax?_ – Rohit Ghosh Dec 29 '16 at 09:28
  • @newuserua_ext I just want to make *read* automatically , only single path whenever my software will open. And that readable file will be shown in read only textfield from where I will do further task. – Rohit Ghosh Dec 29 '16 at 10:12
  • in that case you can use System.getProperty("user.dir") to get path. It returns string value you may set it to textField_1.setText(System.getProperty("user.dir")); – newuserua_ext Dec 29 '16 at 10:32
  • @newuserua_ext thank you , I am looking to it. – Rohit Ghosh Dec 29 '16 at 10:36
  • @newuserua_ext I unable to see any path in the text field area . Its blank . I want to show the path. Rectify my error in the code – Rohit Ghosh Dec 29 '16 at 11:49

1 Answers1

0

See here for a list of system properties and their descriptions.

Instead of this ;

textField_1.setText(System.getProperty("C:/Program Files/dita-ot-2.4"));

Use this ;

textField_1.setText(System.getProperty("user.dir"));

If you want to use absolute path then you should use ;

textField_1.setText("C:/Program Files/dita-ot-2.4");
newuserua_ext
  • 577
  • 6
  • 18
  • I want to use another directory , not mine work space where I am working now. I want to pick value from other drive/folder . Then ? – Rohit Ghosh Dec 29 '16 at 12:45
  • I don't have 20 reputation here , so unable to do on chat. – Rohit Ghosh Dec 29 '16 at 12:59
  • user.dir gives you working directory. If you are going to read somewhere else like C:/Program Files/ is a system folder. So you may use "C:/Program Files/dita-ot-2.4" as a string value. Otherwise if you are using desktop or some other user folder. You need to specify System.getProperty("user.home"). – newuserua_ext Dec 29 '16 at 13:04
  • yes , that is what I was asking that I wan to use string value and it is not showing the path in the application , but rest "user.dir" and "user.home" is showing. `textField_1.setText(System.getProperty("C:/Program Files/dita-ot-2.4"));` textField_1.setEditable(false); – Rohit Ghosh Dec 29 '16 at 13:12
  • you should set it like textField_1.setText("C:/Program Files/dita-ot-2.4"); – newuserua_ext Dec 30 '16 at 06:40
  • It will set text only like label . How will it will pick the value from the mentioned dir? – Rohit Ghosh Dec 30 '16 at 07:13
  • after all if you are going to read data from the path. You may check this link http://stackoverflow.com/questions/1844688/read-all-files-in-a-folder – newuserua_ext Dec 30 '16 at 07:16