Note that there are some explanatory texts on larger screens.

plurals
  1. POA java apprentice trying to migrate from System.out.println to swing
    primarykey
    data
    text
    <p>I'm finally trying to migrate my old console programs to Swing, to make distribution to my friends easier. To this end, I'm trying to write a class ConsoleFrame that I can extend instead of JFrame, that will allow me to interface my old code with Swing as effortlessly as possible. out(String) appears to be working, but inln() has me stumped.</p> <pre><code>//Imports not included public class ConsoleFrame extends JFrame { protected JTextField in; protected JTextArea out; public ConsoleFrame(){ this("Console Frame", 80, 10); } public ConsoleFrame(int cols){ this("Console Frame", cols, 10); } public ConsoleFrame(int cols, int rows){ this("Console Frame", cols, rows); } public ConsoleFrame(String title){ this(title, 80, 10); } public ConsoleFrame(String title, int cols, int rows){ in = new JTextField(); in.setEditable(true); in.setColumns(cols); out = new JTextArea(); out.setEditable(false); out.setColumns(cols); out.setRows(rows); out.setWrapStyleWord(true); setTitle(title); setVisible(true); setDefaultCloseOperation(EXIT_ON_CLOSE); add(in, BorderLayout.PAGE_END); add(out, BorderLayout.PAGE_START); pack(); } protected void out(String o) { out.append(o); } protected void outln(String o) { out(o + BIO.$ln); //BIO.$ln == System.getProperty("line.separator") } /* * This is supposed to halt execution until the user presses enter, then return the text entered in the JTextField named in. */ protected String inln() { in.setEnabled(true); KeyListener enter = new KeyListener() { @Override public void keyTyped(KeyEvent paramKeyEvent) { if(paramKeyEvent.getKeyCode() == KeyEvent.VK_ENTER) { if(in.hasFocus()) { in.setEnabled(false); } } } @Override public void keyPressed(KeyEvent paramKeyEvent) {} @Override public void keyReleased(KeyEvent paramKeyEvent) {} }; in.addKeyListener(enter); while(true){ //This loop is intended to interrupt flow until in.isEnabled()==false, which will only happen when the enter key is typed. if(in.isEnabled()==false){ String result = in.getText(); in.setText(""); in.setCaretPosition(0); this.removeKeyListener(enter); in.setEnabled(true); return result; } } } } </code></pre> <p>Tester program:</p> <pre><code>public class Tester extends ConsoleFrame { public static void main(String[] args) { new Tester(); } public Tester() { super("Test", 60, 30); out(inln()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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