1

How can you process command line arguments in a Java Swing program?

EDIT: What I want to do is have the user give a file path as the arg. Then I set the text of a jTextPane to the contents of that file.

Matt R. Johnson
  • 783
  • 1
  • 7
  • 14
  • please clarify 1) start Swing from command line... 2) from Swing start some Command line process – mKorbel Jun 30 '11 at 22:52
  • 3
    *"..give a file path as the arg. Then I set the text of a jTextPane to the contents of that file."* That sounds great for *last millennium*, but for this millennium, launch the app. using [Java Web Start](http://stackoverflow.com/tags/java-web-start/info) & declare an interest in the files types (e.g. txt, html, rtf) in the launch file. Then when the user double clicks one of those file types, the path/name will be provided as the 2nd argument to the `main(String[])` (the first will be `-open`. See the [JNLP API File services demo.](http://pscode.org/jws/api.html#fs). – Andrew Thompson Jun 30 '11 at 23:02
  • @Andrew Thompson: That option isn't viable for the situation I'm writing this program for. – Matt R. Johnson Jul 01 '11 at 15:23
  • @trashgod: It's not a duplicate. My situation is different from that post, because accessing Swing components and giving them command line args is not mentioned there. – Matt R. Johnson Jul 01 '11 at 15:29
  • @Matt R. Johnson: Re - JWS *"That option isn't viable"* Why not? Please don't make me play '20 questions'. – Andrew Thompson Jul 01 '11 at 15:51

2 Answers2

5

The only way is through the main method when you run the Java class file.

You do the following in the command line once compiled; java NameOfClassFile a b c d

For example to print them out as an array of command line arguments:

public static void main(String[] args)
{
    for(String arg : args)
        System.out.println(arg);
}

I don't know of anyway to pass command line arguments to a currently running Java Swing program. Not sure if you can.

To display a files contents on a JTextPane you can use args[0] a a command line argument to the file path using:

JTextPane pane = new JTextPane();
JFrame frame = new JFrame("Example");
frame.add(pane);
frame.setVisible(true);
File file = new File(args[0]);
pane.setPage(file.toURL().toString());
frame.pack();

If you want more than one files contents just use java NameOfClassFile file1 file2 file3 Then you can use args[0] args[1] args[2] to get the file path and modify the above code to put on a JTextPane.

adamjmarkham
  • 2,150
  • 2
  • 18
  • 26
  • This doesn't seem to be working. What I want to do is have the user give a file path as the arg. Then I set the text of a jTextPane to the contents of that file. – Matt R. Johnson Jun 30 '11 at 22:47
  • 1
    @Matt R. Johnson: If that is what you want, you should be more clear in your post - go ahead and edit it to ask what you really want. – Nate W. Jun 30 '11 at 22:52
  • @Shakedown: Sorry. You're right. I should have said that from the beginning. – Matt R. Johnson Jun 30 '11 at 22:56
  • @Dan: Unfortunately, I cannot post my code. A) There's way too much of it, and I don't know which parts would be helpful B) This is for a job, and I don't think they'd appreciate me making the code public. – Matt R. Johnson Jun 30 '11 at 22:57
1

As mentioned, there's no direct way to go from command-line arguments to Swing. Your Swing application will be executed from a main method somewhere, so you'll have to modify that method to obtain the file from the provided arguments, and then somehow pass that into your Swing components.

On the other hand, if you're making a Swing application, you might as well forget the command-line and instead provide a way to invoke the JFileChooser to allow your user to graphically select a file on their system, and then proceed to load the contents into your JTextArea.

Another method that would be similar to providing command-line arguments would be to provide a JTextField that allows the user to type in the full path to the file.

I'd recommend just adding a "Load File" button above your JTextArea that will invoke JFileChooser.

Good luck!

Nate W.
  • 9,141
  • 6
  • 43
  • 65
  • 1
    I agree. From a usability perspective JFileChooser is the way to go here. See here for details: http://download.oracle.com/javase/tutorial/uiswing/components/filechooser.html – adamjmarkham Jun 30 '11 at 23:35
  • 2
    Please explain the comment, "As mentioned, there's no direct way to go from command-line arguments to Swing." Going through the main method is a fine way to do this. The parameters can be passed via a class's constructor parameter easily. – Hovercraft Full Of Eels Jul 01 '11 at 01:19
  • I was merely trying to say that there's nothing in the Swing library that has to do with command-line arguments. – Nate W. Jul 01 '11 at 02:27
  • 1
    I actually have a FileChooser and a full file management system programmed in. But I'd like to have command line arguments. Eventually, I'll be adding a right click option to the associated file type, so that it can be opened in my program. – Matt R. Johnson Jul 01 '11 at 15:26