Results 1 to 5 of 5
  1. #1
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Exclamation 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

  2. #2
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    6

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    Check out camickr's Message Console.

    db

  4. #4
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Default

    Alright how do i implement this thing cant add it to the panel directly what am i doing wrong heres my code

    votePanelCode
    Java 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;
    	}
    	
    }
    and my JFrame code
    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);
    	}
    }

  5. #5
    ShinTec's Avatar
    ShinTec is offline Member
    Join Date
    Jan 2010
    Posts
    51
    Rep Power
    0

    Default

    aww dont worry i fixed it . Ohhhhhhh it works beautifully thanks camickr and Darryl.Burke for this god send really appreciate it:D

Similar Threads

  1. output to terminal instead of console on mac
    By firen in forum Advanced Java
    Replies: 0
    Last Post: 06-01-2010, 12:42 PM
  2. Printing text files to a JTextArea
    By Adrien in forum AWT / Swing
    Replies: 11
    Last Post: 02-23-2010, 03:45 PM
  3. How Do You?? Get the Console Output as a GUI??
    By Lyricid in forum AWT / Swing
    Replies: 10
    Last Post: 11-20-2009, 11:35 PM
  4. How to align the output on console?
    By sfe23 in forum New To Java
    Replies: 5
    Last Post: 03-30-2009, 03:28 AM
  5. Replies: 1
    Last Post: 07-24-2007, 02:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •