Display file in java swing GUI -
having problems displaying ".txt" file in java swing gui.
i have code read .txt file , display in cmd window cant seem link display in gui.
here code reads file:
import java.io.*; public class readfile { public static void main (string [] args) throws ioexception{ string inputline = ""; filereader inputfile = new filereader( "player.txt"); bufferedreader br = new bufferedreader(inputfile); system.out.println( "\nthe contents of file player.txt "); // read lines of text file, looping until // null character (ctrl-z) endountered while( ( inputline = br.readline()) != null){ system.out.println(inputline); // write screen } br.close(); // close stream } // end of main method } // end of class
here code swing gui
import java.awt.event.*; import javax.swing.*; import java.io.*; public class gui extends jframe{ private jbutton button; //set hight , width of interface private static final int width = 500; private static final int height = 500; //method create inteface public gui(){ createcomponents(); createdisplay(); setsize(width, height); } //an action listener contorl action preformed class clicklistener implements actionlistener{ public void actionperformed(actionevent event){ } } //method create button private void createcomponents(){ button = new jbutton("click me"); actionlistener listener = new clicklistener(); button.addactionlistener(listener); jpanel panel = new jpanel(); panel.add(button); add(panel); } //method create display private void createdisplay(){ jtextarea myarea = new jtextarea(); jpanel panel1 = new jpanel(); panel1.add(myarea); add(panel1); } }
and here code run swing gui
import javax.swing.*; public class guitest{ public static void main (string [] agrs){ jframe frame = new gui(); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); } }
all files in same folder on laptop along .txt file. , using textpad code , compile.
all grate
thanks derek
here code reads file:
get rid of code, not needed.
jtextarea has read(...)
method can use read text file directly text area.
Comments
Post a Comment