Note that there are some explanatory texts on larger screens.

plurals
  1. POJava: need to get input line while in a GUI, is only working in console
    primarykey
    data
    text
    <p>My friends and I have written a program in Java with a GUI, made for touchscreen terminals with no keyboard/mouse. Everything occurs within the GUI, and most of the input is button presses. We want to attach a magnetic card reader (in keyboard mode) and read from a card, then perform processing based on the data from it. </p> <p>I wrote a class that works in the console, but when run through the GUI it just hangs until I alt+tab and click inside the IDE's (Eclipse's) console and swipe the card. What I'm looking for is a way to get this input w/o having to leave the GUI. </p> <p>The MCR works as if you simply typed the card info on a keyboard-- it'll send a line containing both tracks of data anywhere you could type the same line, txt document, console, whatever.</p> <p>The relevant code is as follows:</p> <pre><code>import java.util.Scanner; public class CardRead { public static void main() { String raw_card_data = ""; Scanner read = new Scanner( System.in ); System.out.println("Scan card"); // changed to an outputArea.setText for GUI raw_card_data = read.nextLine(); // works in console, not within GUI /* insert processing here */ } } </code></pre> <p>in the GUI, the code is simply:</p> <pre><code>cardreadbtn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { CardRead.main(); } }); </code></pre> <p>I thought maybe an InputStream would work, but I've never really worked with them. I googled and found this <a href="https://stackoverflow.com/questions/309424/in-java-how-do-i-read-convert-an-inputstream-to-a-string">thread</a> : I tried integrating the suggestion about IOUtils, as follows:</p> <pre><code>InputStream is = System.in; StringWriter writer = new StringWriter(); IOUtils.copy(is, writer, null); raw_card_data = writer.toString(); System.out.println(raw_card_data); IOUtils.closeQuietly(is); IOUtils.closeQuietly(writer); </code></pre> <p>I don't know if I'm doing that right, but in the console (haven't tried in the GUI), it now says "Scan Card" and never progresses to print out raw_card_data or do anything else. "closeQuietly" I got from <a href="http://commons.apache.org/io/api-1.2/org/apache/commons/io/IOUtils.html" rel="nofollow noreferrer">here:</a></p> <p>...but again, I dunno if I'm doing it right. Never worked with apache IOUtils either.</p> <p>So I'm stuck, and I'm looking to you guys. How can I grab this card data w/o leaving the GUI to do so? </p> <p>Important note: The card data has a variable # of spaces in it (which we need to preserve), so anything like Scanner.next() won't work. The card reader is set up to send a line with both tracks separated by delimiters and then a carriage return, so .nextLine() works. The card data is also a variable # of bytes/characters.</p> <p>Also: In the first code block, the System.out.println is before a do while loop that raw_card_data = read.nextLine() is actually in (I left it out because I felt it's not relevant, but now I'm curious why it's doing this). When the println is changed to a setText() to display to a JTextArea in the GUI, it doesn't display until AFTER the card data is input in the console, even though it occurs before the do while and should execute before it. I don't understand, lol.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload