Results 1 to 4 of 4
  1. #1
    arndtmatt is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Reading a comma separated file into JTextPane

    Hello,

    I am currently working on my final for my Java class. We are to design a gui based program that reads a text file and displays it. What I am currently stuck on is that when I read the text file it is only reading the 2/3 of the data and I also do not want the commas to show, I would like the commas to be ignored and have the data tab over when a comma is present.

    Here is what I have so far. It is a mess right now since I have been trying out different things to get it to work. Also, my save and exit buttons do not work either.
    Java Code:
    package javaFinal;
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.*;
    import java.util.Scanner;
    
    import javax.swing.BorderFactory;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JSeparator;
    import javax.swing.JTextArea;
    import javax.swing.JTextPane;
    
    
    
    public class JavaFinalForm extends JFrame implements ActionListener
    {
    
    	private JLabel salesManLbl, salesRegionLbl, yearSalesLbl, totalDollarsLbl, avgSalesLbl, lowSalesLbl, highSalesLbl;
    	JButton save, exit;
    	
    	//Scanner readFile = new Scanner ("C:/Users/mparndsp.CCCW/workspace/JavaFinal/src/javaFinal/sales.txt");
    
        public JavaFinalForm()
        {
        	salesManLbl = new JLabel ("Salesman" );
        	salesRegionLbl = new JLabel ("Sales Region");
        	yearSalesLbl = new JLabel ("Yearly Sales");
        	totalDollarsLbl = new JLabel ("Total Dollars: ");
        	avgSalesLbl = new JLabel ("Average Sales: ");
        	lowSalesLbl = new JLabel ("Lowest Salesman: ");
        	highSalesLbl = new JLabel ("Highest Salesman: ");
                
        	save = new JButton("Save");
            exit = new JButton("Exit");
        	
            
            ButtonListener listener = new ButtonListener();
        	save.addActionListener (listener);
        	exit.addActionListener (listener);
    
        	
            JPanel panel = new JPanel();
            setTitle("Sales Report");
            setSize(new Dimension(500, 300));
            setResizable(false);
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            add(panel);
    
            
            JPanel titlePanel = new JPanel(new BorderLayout());
            titlePanel.setMaximumSize(new Dimension(500, 10));
            JLabel title = new JLabel("ABC Company Sales Report", JLabel.CENTER);
            titlePanel.add(title);
            JSeparator separator = new JSeparator();
            separator.setForeground(Color.black);
            titlePanel.add(separator, BorderLayout.SOUTH);
           
            panel.add(titlePanel);
    
            
            JPanel saleHeadings = new JPanel();
            saleHeadings.setLayout (new GridLayout(1,3));
            saleHeadings.setPreferredSize(new Dimension(350,12));
            saleHeadings.add(salesManLbl);
            saleHeadings.add(salesRegionLbl);
            saleHeadings.add(yearSalesLbl);
            
            panel.add(saleHeadings);          
            
            
            JPanel salesPanel = new JPanel (new BorderLayout());
            salesPanel.setBorder(BorderFactory.createEmptyBorder(5,10,5,10));
            salesPanel.setPreferredSize(new Dimension(400,100));
            JTextPane salesInfo = new JTextPane();
            salesInfo.setEditable(false);
            try
            {
            	File sales = new File("C:/Users/mparndsp.CCCW/workspace/JavaFinal/src/javaFinal/sales.txt");
    
                BufferedReader in = new BufferedReader(
                					new InputStreamReader(
                					new FileInputStream(sales)));
                String line;
                while ((line = in.readLine()) != null)
                {
                	salesInfo.read(in, "salesInfo");
                }
            	
            }
            catch (IOException e)
            {
            	System.err.println("File not found");
            	System.exit(1);
            }
    
    
            salesPanel.add(new JScrollPane(salesInfo));
           
            panel.add(salesPanel);
                                 
            
            JPanel figures = new JPanel();
            figures.setLayout(new BoxLayout (figures, BoxLayout.Y_AXIS));
            figures.setPreferredSize(new Dimension(490,65));
            figures.add(totalDollarsLbl);
            figures.add(avgSalesLbl);
            figures.add(lowSalesLbl);
            figures.add(highSalesLbl);
          
            panel.add(figures);
                    
            
            JPanel bottom = new JPanel(new FlowLayout(FlowLayout.RIGHT));
            bottom.setPreferredSize(new Dimension(400,100));
            bottom.add(save);
            bottom.add(exit);
           
            panel.add(bottom);
        }
        
        
        private class ButtonListener implements ActionListener
        {
    	    public void actionPerformed(ActionEvent e)
    	    {
    	    	if(e.getSource().equals(save))
    	    	{
    	    		try {
    					FileWriter fstream = new FileWriter("save.txt");
    					} 
    	    		catch (IOException e1)
    	    		{
    					System.err.println("Error: " + e1.getMessage());
    				}
    	    		if(e.getSource().equals(exit))
    	    		{
    	    			System.exit(0);
    	    		}
    	    			
    	    	}
    	    }
        	
        }
    
    
        public static void main(String[] args)
        {
        	JavaFinalForm ABC = new JavaFinalForm();
        	ABC.setVisible(true);
        }
    
    
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    }
    The text file is as follows:

    Bob Smith, east, 2000
    Jim Jones, west, 40000
    Frank Fiddler, north, 20000


    Thanks,

    Matthew

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: Reading a comma separated file into JTextPane

    What are the results of your code's processing of the input?
    Can you print out what your code produces so we can see the problem?
    Add some comments to show what you want the output to look like.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    arndtmatt is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default Re: Reading a comma separated file into JTextPane

    Reading a comma separated file into JTextPane-abc-sales-report.jpg

    I attached what I get when I run the program. It is just skipping the first line entirely (Bob Smith, east, 2000). In the end I want all 3 to show up without the comma and to be tabbed so the data fits under the header.


    Thank you,

    Matthew

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,432
    Rep Power
    25

    Default Re: Reading a comma separated file into JTextPane

    What does the code do with the first line it reads from the file? Look at where the code reads from the file.

    I don't know how to tell JTextPane to change the contents of the text it reads (comma to tabs). Perhaps you should read the file first and convert the text to what you want before putting it into the JTextPane for display.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Reading from a JTextPane
    By Mash in forum AWT / Swing
    Replies: 7
    Last Post: 04-17-2010, 03:33 PM
  2. Delimite the file using comma
    By gokulcool in forum New To Java
    Replies: 3
    Last Post: 12-30-2008, 05:40 PM
  3. JTextPane (reading char and its attributes)
    By Defero in forum New To Java
    Replies: 0
    Last Post: 07-19-2008, 08:09 PM
  4. Replies: 7
    Last Post: 05-23-2008, 03:46 AM
  5. Replies: 0
    Last Post: 04-16-2008, 10:58 PM

Posting Permissions

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