Results 1 to 5 of 5
  1. #1
    aminem2080 is offline Member
    Join Date
    Oct 2013
    Posts
    2
    Rep Power
    0

    Default spreadsheet problem

    i have an assignment to make a simple spreadsheet with Java swing and awt packages
    not using openOffice or other packages,i must make it to work like MicroSoft excel
    it means must have JTxetField where user enters
    the formula (like =SUM(D3:G3) or =COUNTIF(B1:B6,3))or =F2)
    and the result is displayed in a selected cell in the JTable
    i have triead but am stack ;here below is my work so far
    Java Code:
    import javax.swing.*;
    import javax.swing.table.*;
    
    import java.awt.*;
    import java.awt.event.*;
    
    public class SpreadSheetGUI extends JFrame {
    	JTable tab;
    	JTextField txf,txfcell;
    	String [] columnNames={" ","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T"};
    	String[][] rowdata={{"  1","","","","","","","","","","","","","","","","","","","",""},
    			{"  2","","","","","","","","","","","","","","","","","","","","",""},
    			{"  3","","","","","","","","","","","","","","","","","","","",""},
    			{"  4","","","","","","","","","","","","","","","","","","","",""},
    			{"  5","","","","","","","","","","","","","","","","","","","",""},
    			{"  6","","","","","","","","","","","","","","","","","","","",""},
    			{"  7","","","","","","","","","","","","","","","","","","","",""},
    			{"  8","","","","","","","","","","","","","","","","","","","",""},
    			{"  9","","","","","","","","","","","","","","","","","","","",""},
    			{"10","","","","","","","","","","","","","","","","","","","",""},
    			{"11","","","","","","","","","","","","","","","","","","","",""},
    			{"12","","","","","","","","","","","","","","","","","","","",""},
    			{"13","","","","","","","","","","","","","","","","","","","",""},
    			{"14","","","","","","","","","","","","","","","","","","","",""},
    			{"15","","","","","","","","","","","","","","","","","","","",""}};
    	DefaultTableModel mymodel;
    
    	public SpreadSheetGUI() {
    //construct the gui and put every thing there
    		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.setTitle("My SpreadSheet");
    		this.setSize(1100, 500);
    		this.setLocation(100, 100);
    		this.setLayout(null);
    		
    		txf=new JTextField();
    		txf.setBounds(100, 50, 900, 30);
    		txf.setFont(new Font("Arial",Font.BOLD,18));
    		this.add(txf);
            mymodel=new DefaultTableModel(rowdata,columnNames);
    		
    		tab=new JTable(mymodel);
    		tab.setFillsViewportHeight(true);
    		tab.setCellSelectionEnabled(true);
    		tab.setRowSelectionAllowed(false);
    		tab.setColumnSelectionAllowed(false);
    		tab.setSelectionBackground(Color.CYAN);
    		tab.addMouseListener(new myMouseListener());
    		tab.addKeyListener(new mykeylistener());
    		txfcell=new JTextField();
    		TableColumn tc=tab.getColumnModel().getColumn(0);
    		tc.setCellEditor(new DefaultCellEditor(txfcell));
    		txfcell.setEditable(false);
    		tc.setResizable(false);
    		tc.setMaxWidth(20);
    	
    	
    		JScrollPane mypan=new JScrollPane(tab);
    		mypan.setBounds(50, 80, 950, 300);
    		
    		this.add(mypan);
    		
    		this.setVisible(true);
    		
    	}
    //the method that deals with entering cell content in txtfield
    	public void addCellValueToField(){
    		
    		int cellposx=tab.getSelectedRow();
    		int cellposy=tab.getSelectedColumn();
    	    int rowreference=tab.getSelectionModel().getLeadSelectionIndex()+1;
    	    
    		String columnreference=tab.getColumnName(cellposy);
    		String celltext=tab.getValueAt(cellposx, cellposy).toString();
    		String cellrefrence=columnreference+rowreference;
    		
    		if(celltext.equals("")){
    			txf.setText(" = "+cellrefrence);
    			
    		}
    		else if (cellposy==0){
    			txf.setText("The Whole Entire Row : "+rowreference);
    			
    		}
    		else {txf.setText(celltext);
    		
    		}
    	}
    	
    	public class myMouseListener implements MouseListener {
            public void mouseClicked(MouseEvent e) {
    			
    			addCellValueToField();//the method that deals with entering cell content in txtfield
    		}
            public void mouseEntered(MouseEvent e) {}
    		public void mouseExited(MouseEvent e) {}
    		public void mousePressed(MouseEvent e) {}
            public void mouseReleased(MouseEvent e) {}
    
    	}
    	class mykeylistener implements KeyListener{
            public void keyPressed(KeyEvent e) {
    			int cellposx=tab.getSelectedRow();
    			int cellposy=tab.getSelectedColumn();
    		    
    			String celltext=tab.getValueAt(cellposx, cellposy).toString();
    			txf.setText(celltext);
    			
    			
    		}
    
    		public void keyReleased(KeyEvent e) {
    			
    			
    		}
    		public void keyTyped(KeyEvent e) {
    		
    			int cellposx=tab.getSelectedRow();
    			int cellposy=tab.getSelectedColumn();
    		    
    			String celltext=tab.getValueAt(cellposx, cellposy).toString();
    			txf.setText(celltext);
    			
    		}
    		
    	}
    
    }
    please help me with this.
    thank you for your tim
    Last edited by JosAH; 10-15-2013 at 09:41 PM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: spreadsheet problem

    @OP: don't hijack someone else's thread; I moved your post to its own thread (and removed the duplicate post)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: spreadsheet problem

    "i must make it to work like MicroSoft excel"??

    Whoever has asked you to do this has seriously underestimated the amount of work involved.
    POI and JExcel and the like have taken years to get to a point where they can read and write Excel files...and they have several people with lots of Java experience working on them.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: spreadsheet problem

    Perhaps I misunderstood what you were saying but I don't believe the OP needs to read and write Excel files. I believe the requirement is to write a spreadsheet program that behaves similar to Excel in that it accepts Excel syntax. Although much simpler since you don't need to understand native Excel formats, it would still be a significant challenge.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default Re: spreadsheet problem

    I know it's not to read Excel, but I would argue it isn't simpler to any significant degree.
    The real challenge (from what I can gather) for the likes of POI and Excel was the syntax for the commands.
    There are, after all, hundreds of them.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Array Spreadsheet error
    By dokunbam in forum New To Java
    Replies: 2
    Last Post: 10-09-2013, 04:19 PM
  2. Exporting recordset to xls spreadsheet
    By drimades in forum JDBC
    Replies: 4
    Last Post: 03-07-2012, 06:11 PM
  3. generate excel spreadsheet
    By BigBear in forum Advanced Java
    Replies: 5
    Last Post: 04-27-2010, 02:44 PM
  4. Recovering data from Excel spreadsheet
    By Anubis in forum SWT / JFace
    Replies: 0
    Last Post: 12-17-2008, 08:28 PM
  5. Spreadsheet in Java
    By emceenugget in forum New To Java
    Replies: 4
    Last Post: 10-03-2008, 07:18 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
  •