codeguru26's Profile User Rating: -----

Reputation: 0 Apprentice
Group:
New Members
Active Posts:
10 (0.01 per day)
Joined:
31-March 10
Profile Views:
1,428
Last Active:
User is offline Nov 06 2013 04:18 AM
Currently:
Offline

Previous Fields

Country:
IN
OS Preference:
Linux
Favorite Browser:
Chrome
Favorite Processor:
Intel
Favorite Gaming Platform:
XBox
Your Car:
Mercedes
Dream Kudos:
0
Icon   codeguru26 has not set their status

Posts I've Made

  1. In Topic: File, open and save has a bug.

    Posted 5 Nov 2013

    I have been recently working on code for notepad using awt components in java. I have been facing the problem regarding use of the classes. If I edit the the opened file which class should I use..?
    Here is my code for notepad
    import java.io.*;
    import java.util.*;
    import java.awt.*;
    import java.awt.event.*;
    class Editor extends WindowAdapter implements ActionListener
    {
    	Frame f,f1;
    	TextField ta;
    	MenuBar mb;
    	Menu m1,m2;
    	FileDialog fc = new FileDialog(f, "Choose a file", FileDialog.LOAD);
    	FileDialog fs = new FileDialog(f, "Save a file", FileDialog.SAVE);
    	MenuItem nw,opn,sve,sve_as,ext;
    	String str21="";
    	char arr[];
    	String org_open="",org_save="";
    	static int flag_new=0,flag_open=0,flag_save=0,flag_edited=0,l=0;
    	public Editor()
    	{
    		f = new Frame();
        	f.setSize(400,400);
        	f.setLayout(new BorderLayout());
        	
    		
        	mb = new MenuBar();
        	m1 = new Menu("File");
        	
        	
        	
        	fc.setDirectory("C:\\");
        	
        	
        	fs.setDirectory("C:\\");
    
        	ta = new TextField();
        	f.add(ta,"Center");
     		   
        	nw = new MenuItem("New");
        	opn = new MenuItem("Open");
        	sve = new MenuItem("Save");
        	sve_as =  new MenuItem("Save As");
        	ext = new MenuItem("Exit");
    		
    		nw.addActionListener(this);
    		opn.addActionListener(this);
    		sve.addActionListener(this);
    		sve_as.addActionListener(this);
    		ext.addActionListener(this);
    		
    		m1.add(nw);
        	m1.add(opn);
        	m1.add(sve);
        	m1.add(sve_as);
        	m1.addSeparator();
        	m1.add(ext);
    		
    		mb.add(m1);
    		
        	f.setMenuBar(mb);
        	
        	f.setVisible(true);
        	
        	f.addWindowListener(this);
    	}
    	public void actionPerformed(ActionEvent e)
    	{
    		MenuItem mi=(MenuItem)e.getSource();
    		String str = ta.getText();
    		if(mi==nw)
    		{
    			if(str.length()!=0&flag_save==1)
    			{
    				String str12=ta.getText();
    				if(str12.equals(org_save))
    				{
    					ta.setText("");
    					flag_new=1;
    				}
    				else if(!str12.equals(org_save))
    				{
    					frame("New");
    					flag_new=1;
    				}
    				else
    				{
    					frame("New");
    					flag_new=1;
    				}
    				
    			}
    			else if(str.length()!=0&flag_save==0)
    			{
    				frame("New");
    				flag_new=1;
    			}
    			else if(str.length()==0&flag_save==0)
    			{
    				ta.setText("");
    				flag_new=1;
    			}
    			else
    			{	
    				frame("New");
    				flag_new=1;
    			}
    		}
    		else if(mi==opn)
    		{
    				if(flag_save==1&str.length()!=0)
    				{
    					String str1 = ta.getText();
    					if(!org_open.equals(str1))
    					{
    						System.out.println("1");
    						frame("Open");
    					}
    					else if(org_open.equals(org_save))
    					{
    						System.out.println("2");
    						file_open();
    					}
    					else
    					{
    						frame("Open");
    					}
    					flag_open=1;
    				}
    				else if(flag_save==0&str.length()!=0)
    				{
    					String str1 = ta.getText();
    					if(!org_open.equals(str1))
    					{
    						System.out.println("2");
    						frame("Open");
    					}
    					else if(org_open.equals(org_save))
    					{
    						System.out.println("12");
    						file_open();
    					}
    					else
    					{
    						frame("Open");
    					}
    					flag_open=1;
    					flag_open=1;
    				}
    				else if(flag_save==0&str.length()==0)
    				{
    					if(org_open.equals(org_save))
    					{
    						System.out.println("3");
    						file_open();
    					}
    					else
    					{
    						frame("Open");
    					}
    					flag_open=1;
    				}
    				else if(flag_save==1&str.length()==0)
    				{
    					if(org_open.equals(org_save))
    					{
    						System.out.println("4");
    						file_open();
    					}
    					else
    					{
    						frame("Open");
    					}
    					flag_open=1;
    				}
    				else if(str21.equals(org_open))
    				{
    					if(org_open.equals(org_save))
    					{
    						System.out.println("5");
    						file_open();
    					}
    					else
    					{
    						frame("Open");
    					}
    					flag_open=1;
    				}
    				else if(!ta.getText().equals(org_open))
    				{
    					frame("Open");
    				}
    				else
    				{
    					frame("Open");
    						
    				}
            					
    		}
    		else if(mi==sve)
    		{
    			
    		}
    		else if(mi==sve_as)
    		{
    			file_save();
    		}
    		else if(mi==ext)
    		{
    			f.setVisible(false);
    			f.dispose();
    		}
    	}
    	public void frame(String type1)
    	{
    					final String type=type1;
    						f.setEnabled(false);
    						f1 =  new Frame();
        					f1.setSize(200,200);
        					f1.setLayout(new FlowLayout());
    
        					f1.setVisible(true);
        					
        					//CLOSE THE SAVE DIALOG WINDOW
        					f1.addWindowListener(new WindowAdapter()
        					{
        						public void windowClosing(WindowEvent e)
        						{
        							Window w =e.getWindow();
        							w.setVisible(false);
        							w.dispose();
        							f.setEnabled(true);
        						}
        					});		
        					
        					//TO SAVE THE FILE
        					Button b1 = new Button("Save");
        					b1.addActionListener(new ActionListener()
        					{
        						public void actionPerformed(ActionEvent e)
        						{
    								f1.setVisible(false);
    								f1.dispose();
        							fs.setVisible(true);
        							
        							try
        							{
        				
        								String name=fs.getFile();
    									String path=fs.getDirectory();
    									String str1=ta.getText();
    					
    									File f1=new File(path,name);
    									if(path==null)
    									{
    										System.out.println("You have cancelled the choice");
    									}
    									else
    									{
    					
    										try
    										{
    						
    											FileOutputStream fos=new FileOutputStream(f1);
    											char arr[];
    											arr=str1.toCharArray();
    											for(int i=0;i<arr.length;i++)
    											{
    												fos.write(arr[i]);
    											}
    											org_save=String.copyValueOf(arr);
    											fos.close();
    											flag_save=1;
    											if(type.equals("New"))
    											{
    												ta.setText("");
    											}
    											else if(type.equals("Open"))
    											{
    												file_open();
    											}
    											else if(type.equals("Save"))
    											{
    												file_save();
    											}
    											
    											f.setEnabled(true);
    										
    											
    										}    		
    										catch(Exception e222)
    										{
    											System.out.println(e222.getMessage());
    										}	
    			
    									}	
        						}
    	    					catch(Exception e111)
        						{
        							System.out.println(e111.getMessage());
    								f.setEnabled(true);
        						}
    
        				
        					}
        			});
        					f1.add(b1);
        					
        					//TO DON`T SAVE THE FILE
        					Button b2 = new Button("Don`t Save");
        					b2.addActionListener(new ActionListener()
        					{
        						public void actionPerformed(ActionEvent e)
        						{
        							f1.setVisible(false);
        							f1.dispose();	
        							f.setEnabled(true);
        							ta.setText("");
    								if(type.equals("New"))
    								{
    									ta.setText("");
    								}
    								else if(type.equals("Open"))
    								{
    									file_open();
    								}
    								else if(type.equals("Save"))
    								{
    									file_save();
    								}
        							flag_new=1;
        						}
        					});
        					
        					f1.add(b2);
        					
        					//TO DO NOTHING WITH BOX
        					Button b3 = new Button("Cancel");
        					b3.addActionListener(new ActionListener()
        					{
        						public void actionPerformed(ActionEvent e)
        						{
        							f1.setVisible(false);
        							f1.dispose();	
        							f.setEnabled(true);
        						}
        					});
        					f1.add(b3);
    	}
    	public void file_open()
    	{
    								
    								fc.setVisible(true);
    								String name=fc.getFile();
    								String path=fc.getDirectory();
            						if (name == null)
              							System.out.println("You cancelled the choice");
            						else
            						{
            							File f = new File(path,name);
            			
            							try
            							{
            								FileInputStream fis  = new FileInputStream(f);
            								int ch;
         						   			
            								str21="";
            								while((ch=fis.read())!=-1)
            								{
          										str21+=(char)ch;
            								}
    										org_open=str21;
    										if(l==0)
    										{
    											org_save=org_open;
    										}
    										else
    										{
    											l=1;
    											org_save=String.copyValueOf(arr);
    										}
    										ta.setText("");
            								ta.setText(str21);
    										
    										flag_save=1;
            								fis.close();
            							}
            							catch(Exception e1)
            							{
            								System.out.println(e1.getMessage());
            							}
            					
            						}
    	}
    	public void file_save()
    	{
    							fs.setVisible(true);
        							
        							try
        							{
        				
        								String name=fs.getFile();
    									String path=fs.getDirectory();
    									String str1=ta.getText();
    					
    									File f1=new File(path,name);
    									if(path==null)
    									{
    										System.out.println("You have cancelled the choice");
    									}
    									else 
    									{
    				
    										try
    										{
    						
    											FileOutputStream fos=new FileOutputStream(f1);
    											
    											arr=str1.toCharArray();
    											for(int i=0;i<arr.length;i++)
    											{
    												fos.write(arr[i]);
    											}
    											org_save=String.copyValueOf(arr);
    											fos.close();
    											flag_save=1;
    											f.setEnabled(true);
    											
    										}    		
    										catch(Exception e222)
    										{
    											System.out.println(e222.getMessage());
    										}	
    			
    									}	
        						
    	    					}
    							catch(Exception e111)
        						{
        							System.out.println(e111.getMessage());
    								f.setEnabled(true);
        						}
    	}
    	public static void main(String args[])
    	{
    		Editor d = new Editor();
    	}
    }
    
    
  2. In Topic: File, open and save has a bug.

    Posted 31 Oct 2013

    Yes definitely it is my code. I would start with new portion
        	nw.addActionListener(new ActionListener()
        	{
        		public void actionPerformed(ActionEvent e)
        		{
        			String str1 = t.getText();
        			//IF THE TEXTAREA CONTAINS NO TEXT 
        			if(str1.length()==0|flag_save==1)
        			{
        				if(flag_edited==1)
        				{
        					f.setEnabled(false);
        					f3 =  new Frame();
        					f3.setSize(200,200);
        					f3.setLayout(new FlowLayout());
        					f3.setVisible(true);
        					
        					//CLOSE THE SAVE DIALOG WINDOW
        					f3.addWindowListener(new WindowAdapter()
        					{
        						public void windowClosing(WindowEvent e)
        						{
        							Window w =e.getWindow();
        							w.setVisible(false);
        							w.dispose();
        							f.setEnabled(true);
        						}
        					});		
        					
        					//TO SAVE THE FILE
        					Button b1 = new Button("Save");
        					b1.addActionListener(new ActionListener()
        					{
        						public void actionPerformed(ActionEvent e)
        						{
        							fs.setVisible(true);
        							try
        							{
        				
        								String name=fs.getFile();
    									String path=fs.getDirectory();
    									String str1=t.getText();
    					
    									File f1=new File(path,name);
    									if(path==null)
    									{
    										System.out.println("You have cancelled the choice");
    									}
    									else if(str1.length()!=0)
    									{
    					
    										try
    										{
    						
    											FileOutputStream fos=new FileOutputStream(f1);
    											char arr[];
    											arr=str1.toCharArray();
    											for(int i=0;i<arr.length;i++)
    											{
    												fos.write(arr[i]);
    											}
    											fos.close();
    											flag_new=1;
    										}    		
    										catch(Exception e222)
    										{
    											System.out.println(e222.getMessage());
    										}	
    			
    									}	
        						}
    	    					catch(Exception e111)
        						{
        							System.out.println(e111.getMessage());
        						}
    
        				
        					}
        			});
        					f3.add(b1);
        					
        					//TO DON`T SAVE THE FILE
        					Button b2 = new Button("Don`t Save");
        					b2.addActionListener(new ActionListener()
        					{
        						public void actionPerformed(ActionEvent e)
        						{
        							f3.setVisible(false);
        							f3.dispose();	
        							f.setEnabled(true);
        							t.setText("");
        							flag_new=1;
        						}
        					});
        					
        					f3.add(b2);
        					
        					//TO DO NOTHING WITH BOX
        					Button b3 = new Button("Cancel");
        					b3.addActionListener(new ActionListener()
        					{
        						public void actionPerformed(ActionEvent e)
        						{
        							f3.setVisible(false);
        							f3.dispose();	
        							f.setEnabled(true);
        						}
        					});
        					f3.add(b3);
        									
        				
        			}
        			
        			else
        			{
        				t.setText("");
        				flag_new=1;
        			}
        			
        			}
        			else if(str1.length()!=0|flag_save==0|flag_new==0|flag_edited==1)
        			{
        			
        				//IF TEXTAREA CONTAINS TEXT AND SAVE DIALOG BOX SHOULD POP UP
        					f.setEnabled(false);
        					f3 =  new Frame();
        					f3.setSize(200,200);
        					f3.setLayout(new FlowLayout());
        					f3.setVisible(true);
        					
        					//CLOSE THE SAVE DIALOG WINDOW
        					f3.addWindowListener(new WindowAdapter()
        					{
        						public void windowClosing(WindowEvent e)
        						{
        							Window w =e.getWindow();
        							w.setVisible(false);
        							w.dispose();
        							f.setEnabled(true);
        						}
        					});		
        					
        					//TO SAVE THE FILE
        					Button b1 = new Button("Save");
        					b1.addActionListener(new ActionListener()
        					{
        						public void actionPerformed(ActionEvent e)
        						{
        							fs.setVisible(true);
        							try
        							{
        				
        								String name=fs.getFile();
    									String path=fs.getDirectory();
    									String str1=t.getText();
    					
    									File f1=new File(path,name);
    									if(path==null)
    									{
    										System.out.println("You have cancelled the choice");
    									}
    									else if(str1.length()!=0)
    									{
    					
    										try
    										{
    						
    											FileOutputStream fos=new FileOutputStream(f1);
    											char arr[];
    											arr=str1.toCharArray();
    											for(int i=0;i<arr.length;i++)
    											{
    												fos.write(arr[i]);
    											}
    											fos.close();
    											flag_new=1;
    										}    		
    										catch(Exception e222)
    										{
    											System.out.println(e222.getMessage());
    										}	
    			
    									}	
        						}
    	    					catch(Exception e111)
        						{
        							System.out.println(e111.getMessage());
        						}
    
        				
        					}
        			});
        					f3.add(b1);
        					
        					//TO DON`T SAVE THE FILE
        					Button b2 = new Button("Don`t Save");
        					b2.addActionListener(new ActionListener()
        					{
        						public void actionPerformed(ActionEvent e)
        						{
        							f3.setVisible(false);
        							f3.dispose();	
        							f.setEnabled(true);
        							t.setText("");
        							flag_new=1;
        						}
        					});
        					
        					f3.add(b2);
        					
        					//TO DO NOTHING WITH BOX
        					Button b3 = new Button("Cancel");
        					b3.addActionListener(new ActionListener()
        					{
        						public void actionPerformed(ActionEvent e)
        						{
        							f3.setVisible(false);
        							f3.dispose();	
        							f.setEnabled(true);
        						}
        					});
        					f3.add(b3);
        									
        				
        			}
        			
        		}
        	});
    opn.addActionListener(new ActionListener()
        	{
        		public void actionPerformed(ActionEvent e)
        		{
        				f3 = new Frame("Editor");
    		    		String str=t.getText();
        				if(str.length()!=0&flag_save==0)
        				{
        					
        			 					
        					f3.setSize(200,200);
        					f3.setLayout(new BorderLayout());
        					f3.addWindowListener(new WindowAdapter()
        					{
        						public void windowClosing(WindowEvent e)
        						{
        							Window w =e.getWindow();
        							w.setVisible(false);
        							w.dispose();
        						}
        					});
        				
        					Panel p1 = new Panel();
        					Label l = new Label("Do you want to save the text");
        					p1.add(l);
        					f3.add(p1);
        				
        					Panel p2 = new Panel(new FlowLayout());
        					Button b1 = new Button("Save");
        					b1.addActionListener(new ActionListener()
        					{
        						public void actionPerformed(ActionEvent e)
        						{
        							f3.setVisible(false);
        							f3.dispose();
        							if(flag_save==0)
        							{
        								fs.setVisible(true);
     			   					}
        							else
        							{
        								fs.setVisible(false);
        								fs.dispose();
        							}
        			
        							try
        							{
        								String name=fs.getFile();
    									String path=fs.getDirectory();
    									String str=t.getText();
    									File f1=new File(path,name);
    					
    									try
    									{
    						
    										FileOutputStream fos=new FileOutputStream(f1);
    										char arr[];
    										arr=str.toCharArray();
    										for(int i=0;i<arr.length;i++)
    										{
    											fos.write(arr[i]);
    										}
    										flag_save=1;
    										fos.close();
    									}    		
    									catch(Exception e222)
    									{
    										System.out.println();
    									}	
    									
    									t.setText("");
    									fc.setVisible(true);
    									f3.setVisible(false);
    									f3.dispose();
    									
        							}
        							catch(Exception e323)
        							{
        								System.out.println(e323.getMessage());
        							}}});
        					p2.add(b1);
        					Button b2 = new Button("Don`t save");
        					b2.addActionListener(new ActionListener()
        					{
        						public void actionPerformed(ActionEvent e)
        						{
        							t.setText("");
        							f3.setVisible(false);
        							f3.dispose();
        							fc.setVisible(true);
            		
            						String name=fc.getFile();
    								String path=fc.getDirectory();
            						if (name == null)
              							System.out.println("You cancelled the choice");
            						else
            						{
            							File f = new File(path,name);
            			
            							try
            							{
            								FileInputStream fis  = new FileInputStream(f);
            								int ch;
         						   			t.setText("");
            								String str21="";
            								while((ch=fis.read())!=-1)
            								{
          										str21+=(char)ch;
            								}
            								t.setText(str21);
            								flag_open=1;
            								flag_save=1;
            								fis.close();
            							}
            							catch(Exception e1)
            							{
            								System.out.println(e1.getMessage());
            							}
            					
            						}
        						}
        					});
        					p2.add(b2);
        					Button b3 = new Button("Cancel");
        					b3.addActionListener(new ActionListener()
        					{
        						public void actionPerformed(ActionEvent e)
        						{
        							f3.setVisible(false);
        							f3.dispose();
        						}
        					});
        					p2.add(b3);
        				
        					f3.add(p2);
        				
        					f3.setVisible(true);
        				}
        				else
        				{
        						fc.setVisible(true);
            					String name=fc.getFile();
    							String path=fc.getDirectory();
            					if (name == null)
              						System.out.println("You cancelled the choice");
            					else
            					{
            						File f = new File(path,name);
            			
            						try
            						{
            							FileInputStream fis  = new FileInputStream(f);
            							int ch;
            //							System.out.println("-----------------");
            							t.setText("");
            							String str21="";
            							while((ch=fis.read())!=-1)
            							{
          									str21+=(char)ch;
            							}
            							t.setText(str21);
            							flag_open=1;
            							flag_save=1;
            							flag_edited=1;
            							fis.close();
            						}
            						catch(Exception e1)
            						{
            							System.out.println(e1.getMessage());
            						}
            			
            					}
        				}
        		
        	}
           	});
           	
           	
           	
        	sve.addActionListener(new ActionListener()
        	{
        		public void actionPerformed(ActionEvent e)
        		{
        			String str1=t.getText();
        			if(flag_save==0|flag_open==1|flag_edited==1)
        			{
        						fs.setVisible(true);
        			try
        			{
        				
        			String name=fs.getFile();
    				String path=fs.getDirectory();
    				
    					
    				File f1=new File(path,name);
    					if(path==null)
    					{
    						System.out.println("You have cancelled the choice");
    					}
    					else if(str1.length()!=0|flag_save==0)
    					{
    					
    						try
    						{
    						
    							FileOutputStream fos=new FileOutputStream(f1);
    							char arr[];
    							arr=str1.toCharArray();
    							for(int i=0;i<arr.length;i++)
    							{
    								fos.write(arr[i]);
    							}
    							flag_save=1;
    							fos.close();
    						}    		
    						catch(Exception e222)
    						{
    							System.out.println(e222.getMessage());
    						}	
    			
    					}
        		}
    	    	catch(Exception e111)
        		{
        			System.out.println(e111.getMessage());
        		}
    
        	
        		}
        		else
        		{
        			String str11=t.getText();
        			if(str11.equals(str1))
        			{
        				System.out.println("Do nothing");
        			}
        			else
        			{
        				String name=fs.getFile();
    				String path=fs.getDirectory();
    				str1=t.getText();
    				
    				File f1=new File(path,name);
    					if(path==null)
    					{
    						System.out.println("You have cancelled the choice");
    					}
    					else if(str1.length()!=0|flag_save==0)
    					{
    					
    						try
    						{
    						
    							FileOutputStream fos=new FileOutputStream(f1);
    							char arr[];
    							arr=str1.toCharArray();
    							for(int i=0;i<arr.length;i++)
    							{
    								fos.write(arr[i]);
    							}
    							flag_save=1;
    							
    							fos.close();
    						}    		
    						catch(Exception e222)
    						{
    							System.out.println(e222.getMessage());
    						}	
    			
    					}
        			}
        		}	
        	
        		}
        	});
        	
    
    
    


    In this code when when i create a file and save the file. After saving the file, if I edit the text and after click the "new" menu it doesn`t ask me to save the file. All the above code is written by me.
  3. In Topic: How to change color of TextArea..?

    Posted 21 Oct 2013

    I have tried and worked with every event class but it looks it is not working...?
  4. In Topic: How to change color of TextArea..?

    Posted 21 Oct 2013

    But when I`m adding tx.addAdjustmentListener(this) in the above code it is saying "symbol not found".
  5. In Topic: How to change color of TextArea..?

    Posted 20 Oct 2013

    But I`ve to do this only using awt class

My Information

Member Title:
New D.I.C Head
Age:
Age Unknown
Birthday:
Birthday Unknown
Gender:

Contact Information

E-mail:
Private

Friends

Comments

codeguru26 has no profile comments yet. Why not say hello?