java - Use KeyListener in a loop -
i don't have lot of experience keylisteners used 1 in application , works fine except need wait input before program can continue. made while loop loops until string temp not null (which mean there input).
the problem there no way type in jtextfield (called input). below code 2 methods supposed work text in jtextfield (input) can returned (as temp). i'm not sure why doesn't work or how fix it.
the keypressed method keylistener:
public void keypressed(keyevent e) { //only sends text if enter key pressed if (e.getkeycode()==keyevent.vk_enter) { //if there text if (!input.gettext().equals("")) { //string temp changed null input temp=input.gettext(); //text sent jtextfield output.append(temp+"\n"); //input no longer has text input.settext(""); } } }
the method thats trying text, in keylistener class
public string gettemp() { booleans isnull=temp==null; //loops until temp not null while (isnull) { //unnecessary line of code, used loop not empty isnull=checktemp(); } return temp; } public boolean checktemp() { return temp==null; }
your while loop common console program construct, understand you're not creating console program here rather event-driven gui, , in situation, while loop fights against swing gui library, , need rid of it. instead of while loop continual polling want respond events, , if you're listening user input jtextfield do not use keylistener low-level listener can cause unwanted side effects. instead add documentlistener jtextfield's document.
edit: you're listening enter key, , solution easier: add actionlistener jtextfield!
e.g.,
input.addactionlistener(e -> { string text = input.gettext().trim(); if (text.isempty()) { return; } output.append(text + "\n"); input.settext(""); });
more complete example:
import java.awt.borderlayout; import java.awt.event.actionlistener; import javax.swing.*; public class chatbox extends jpanel { private static final int cols = 40; private jtextfield input = new jtextfield(cols); private jtextarea output = new jtextarea(20, cols); private jbutton submitbutton = new jbutton("submit"); public chatbox() { output.setfocusable(false); // user can't output jscrollpane scrollpane = new jscrollpane(output); scrollpane.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_always); actionlistener inputlistener = e -> { string text = input.gettext().trim(); if (text.isempty()) { return; } output.append(text + "\n"); input.settext(""); input.requestfocusinwindow(); }; input.addactionlistener(inputlistener); submitbutton.addactionlistener(inputlistener); jpanel bottompanel = new jpanel(); bottompanel.setlayout(new boxlayout(bottompanel, boxlayout.line_axis)); bottompanel.add(input); bottompanel.add(submitbutton); setlayout(new borderlayout()); add(scrollpane, borderlayout.center); add(bottompanel, borderlayout.page_end); } private static void createandshowgui() { chatbox mainpanel = new chatbox(); jframe frame = new jframe("chat box"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(mainpanel); frame.pack(); frame.setlocationrelativeto(null); frame.setvisible(true); } public static void main(string[] args) { swingutilities.invokelater(() -> createandshowgui()); } }
Comments
Post a Comment