Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Problem with simple keystroke/executing code

  1. #1
    Junior Member
    Join Date
    Sep 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with simple keystroke/executing code

    A project has fallen in my lap which is pretty daunting. I took Java in highschool but this isn't my forte.. I could really use your help!



    I need a program that will execute when "insert" key is pressed. It needs to print a label file (premade). So it would have to open the file, then print (key stroke cltr+p, enter?) all in 1 keystroke.



    Also, it would have to choose the right file path.. I am to receive a 4 digit number string from another Java program which will decide which file to print.. So the program would first have to receive this code and set it as a variable and check against my logic..

    for example if x=1803 {

    print 1803.pdf

    }

    else if x=1804{

    print 1804.pdf

    }

    And so on.. Could anyone please help with this?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with simple keystroke/executing code

    To have a program respond to a key press while it is executing, use a KeyListener.
    Define a class that implements the KeyListener interface with its methods and use an instance of that class with the GUI object that is being shown.
    Take a look at the tutorial: https://docs.oracle.com/javase/tutor...ylistener.html
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with simple keystroke/executing code

    Thanks for your response! I'm loading the program onto a linux machine, so I can bind F9 to open a command, which will open the program.

    But could you help me with a program opening and/or sending a file to a printer?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with simple keystroke/executing code

    If there is a program that will print a file, you can build a command line with that program and file and pass that to the OS.
    See the Runtime class's exec method.
    Also See the Desktop class's print method
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks,top worked!

    import java.awt.Desktop;
    import java.io.File;

    public class loadlabel3x1{
    public static void main(String args[])throws IOException{

    File file = new File("C:\\Users\\akunovszky\\Downloads\\java\\load label\\3x1\\3x1label.lbl");
    Desktop desktop = Desktop.getDesktop();
    desktop.open(file);



    }
    }
    Now if I want an if statement .. let's say a separate program generates a number "x" in this case "1234"

    How would I set that variable?

    if (x=1234){
    do this}
    else if (x=1123){
    do that}
    else if (x=1112){
    do that}

    Correct?

    Thanks,top worked!

    import java.awt.Desktop;
    import java.io.File;

    public class loadlabel3x1{
    public static void main(String args[])throws IOException{

    File file = new File("C:\\Users\\akunovszky\\Downloads\\java\\load label\\3x1\\3x1label.lbl");
    Desktop desktop = Desktop.getDesktop();
    desktop.open(file);



    }
    }
    Now if I want an if statement .. let's say a separate program generates a number "x" in this case "1234"

    How would I set that variable?

    if (x=1234){
    do this}
    else if (x=1123){
    do that}
    else if (x=1112){
    do that}

    Correct?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with simple keystroke/executing code

    The comparison operator is ==
    The assignment operator is =

    Your code is assigning a value to x, not comparing it.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Executing cmd from Java, Compiling and Executing IN Runtime
    By Andrew R in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 9th, 2013, 10:00 AM
  2. I have a problem with my code of a simple game
    By byrne in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 17th, 2012, 09:38 AM
  3. Problem with a waitFor() executing a script
    By Baldurian in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 29th, 2010, 10:45 AM
  4. Simple program that detects my keystroke
    By chronoz13 in forum Java Theory & Questions
    Replies: 3
    Last Post: December 27th, 2009, 11:44 PM
  5. [SOLVED] Detecting whether the piece of code is executing?
    By vivek1982 in forum Web Frameworks
    Replies: 4
    Last Post: March 27th, 2009, 06:17 AM