Results 1 to 5 of 5
- 06-04-2010, 06:23 AM #1
Need help with printing console output to JTextArea
Hi forum
Im trying to find a solution to printing the console output to a JTextArea that is on a JPanel. I have found i simple solution by coverting the system.out.printf() in my code to using string.format and then assigning them to String values. But this is becaming more trouble then it worth. Is there a better way of doing this or are there any java swing books/ebook that explain how to this. Any help would be much appreciated thank you in advance
- 06-04-2010, 06:31 AM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,146
- Rep Power
- 5
- 06-04-2010, 06:36 AM #3
Check out camickr's Message Console.
db
- 06-04-2010, 09:03 AM #4
Alright how do i implement this thing cant add it to the panel directly what am i doing wrong heres my code
votePanelCode
and my JFrame codeJava Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintStream; import java.io.PrintWriter; import java.util.ListIterator; public class VotePanel extends JPanel implements ActionListener { //public attributes public static final int NO_OF_CHAR = 40; public static final int NO_OF_LINES = 15; //private attributes private JTextField fileOutputField; private JButton browseButton; private JButton runElection; private JTextArea voteAreaOutput; private JScrollPane voteText; /*this is for storing the name of the file * retrieved by JFileChooser*/ private String statefile; /*using election class in the actionPerformed()*/ Election elec = new Election(); //constuctor public VotePanel() { /*setting panelBackground color*/ setBackground(Color.gray); fileOutputField = new JTextField(NO_OF_CHAR); fileOutputField.setBackground(Color.WHITE); fileOutputField.setEditable(false); //buttons for votePanel /*this button is for browsing the file*/ browseButton = new JButton("Browse"); browseButton.addActionListener(this); /*This button to run the election*/ runElection = new JButton("Run Election"); runElection.addActionListener(this); /*This is the textArea which will display the output of a program*/ voteAreaOutput = new JTextArea(); //voteAreaOutput.setEditable(false); //voteAreaOutput.setBackground(Color.white); /*Scroll bar for textArea*/ //voteText = new JScrollPane(voteAreaOutput); //voteText.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); /**/ MessageConsole mc = new MessageConsole(voteAreaOutput); mc.redirectOut(); mc.redirectErr(); mc.setMessageLines(100); /*this section is where i add shit to the JPanel*/ add(fileOutputField); add(browseButton); add(runElection); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub String buttonPressed = e.getActionCommand(); if(buttonPressed.equals("Browse")) { String filename = File.separator+"tmp"; JFileChooser fc = new JFileChooser(new File(filename)); // Show open dialog; this method does not return until the dialog is closed fc.showOpenDialog(new VotePanel()); File selFile = fc.getSelectedFile(); /*setting the variable stateName and setting the output of the JTextField*/ setStatefile(selFile.getName()); fileOutputField.setText(getStatefile()); } if(buttonPressed.equals("Run Election")) { elec.seatFileReadIn(getStatefile()); ListIterator<Seat> testL = elec.getsL().listIterator(); Seat s = null; /*reading in parties and the votes*/ while(testL.hasNext()) { s = testL.next(); s.partyFileReadIn(); s.voteFileReadIn(); s.CalculateSeatWinner(); } elec.printOverallresult(); } } //get set methods public void setStatefile(String statefile) { this.statefile = statefile; } //------------------------------------- public String getStatefile() { return this.statefile; } }
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.PrintWriter; public class GuiMain extends JFrame { //public attributes /*frame size*/ public static final int WIDTH = 1024; public static final int HEIGTH = 786; //private private JTabbedPane theTab; private static JTextArea test; /*Main*/ public static void main(String[] args) { GuiMain runGui = new GuiMain(); runGui.setVisible(true); } /*constructors*/ public GuiMain() { /*Naming JFrame*/ super("Raymonds Voting Program"); /*set size of window*/ setSize(WIDTH,HEIGHT); /*setting default close operation*/ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); /*adding Panels to tab panel*/ theTab = new JTabbedPane(); theTab.add("Run",new VotePanel()); theTab.add("Query",new SqlPanel()); /*add tabbed panel to JFrame*/ add(theTab); } }
- 06-04-2010, 10:10 AM #5
Similar Threads
-
output to terminal instead of console on mac
By firen in forum Advanced JavaReplies: 0Last Post: 06-01-2010, 12:42 PM -
Printing text files to a JTextArea
By Adrien in forum AWT / SwingReplies: 11Last Post: 02-23-2010, 03:45 PM -
How Do You?? Get the Console Output as a GUI??
By Lyricid in forum AWT / SwingReplies: 10Last Post: 11-20-2009, 11:35 PM -
How to align the output on console?
By sfe23 in forum New To JavaReplies: 5Last Post: 03-30-2009, 03:28 AM -
how to write the output of the console to a file
By fred in forum New To JavaReplies: 1Last Post: 07-24-2007, 02:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks