java program..Notepad prob

Status
Not open for further replies.

swati_walia_88

Right off the assembly line
I have a source code for developing a notepad but the problem is that when i run it the buttons which i've used as shortcuts don't have there photos displayed on them... they just come across as a rectangular button rather than a cut or a copy photo...i've even saved those images in the c drive only where other files like .class file are stored still its not working...plz help...
Code:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class Notepro extends JFrame implements ActionListener
{
    JMenuBar mbar;
    JMenu file,edit,format,font,font1,font2;
    JMenuItem item1,item2,item3,item4;
    JMenuItem item5,item6,item7,item8,item9,item10;
    JMenuItem fname1,fname2,fname3,fname4;
    JMenuItem fstyle1,fstyle2,fstyle3,fstyle4;
    JMenuItem fsize1,fsize2,fsize3,fsize4;
    JButton button,button1,button2,button3,button4,button5;


    JPanel mainpanel;
    JTextArea text;
    JScrollPane jsp;
    
    String command=" ";
    String str=" ";

    String str1=" ",str2=" ",str3=" ";
    String str4=" ";
    
    String str6=" ";
    String str7=" ",str8=" ",str9=" ";

    int len1;

    int i=0;
    int pos1;
    int len;
    
    


    public Notepro(String str)
    {

    super(str);

    mainpanel=new JPanel();
    mainpanel=(JPanel)getContentPane();
    mainpanel.setLayout(new FlowLayout());

    MyWindowAdapter adapter=new MyWindowAdapter(this);    
    addWindowListener(adapter);
    mbar=new JMenuBar();
    setJMenuBar(mbar);

    file=new JMenu("File");
    edit=new JMenu("Edit");
    format=new JMenu("Format");
    
    
    file.add(item1=new JMenuItem("New..."));
    file.add(item2=new JMenuItem("Open"));
    file.add(item3=new JMenuItem("Save As..."));
    file.add(item4=new JMenuItem("Exit"));
    mbar.add(file);


    edit.add(item5=new JMenuItem("Cut"));
    edit.add(item6=new JMenuItem("Copy"));
    edit.add(item7=new JMenuItem("Paste"));
    edit.add(item8=new JMenuItem("Delete"));
    edit.add(item9=new JMenuItem("Select All"));
    mbar.add(edit);

    
format.add(item10=new JMenuItem("Font"));
    mbar.add(format);
            
        button = new JButton(new ImageIcon("new.gif"));
        mainpanel.add(button);
        button1 = new JButton(new ImageIcon("open.gif"));
        mainpanel.add(button1);
        button2 = new JButton(new ImageIcon("save.gif"));
        mainpanel.add(button2);
        button3 = new JButton(new ImageIcon("copy.gif"));
        mainpanel.add(button3);
        button4 = new JButton(new ImageIcon("cut.gif"));
        mainpanel.add(button4);
        button5 = new JButton(new ImageIcon("paste.gif"));
        mainpanel.add(button5);
    
    item1.addActionListener(this);
    item2.addActionListener(this);
    item3.addActionListener(this);
    item4.addActionListener(this);
    item5.addActionListener(this);
    item6.addActionListener(this);
    item7.addActionListener(this);
    item8.addActionListener(this);
    item9.addActionListener(this);
    item10.addActionListener(this);
    
    button.addActionListener(this);
    button1.addActionListener(this);
    button2.addActionListener(this);
    button3.addActionListener(this);
    button4.addActionListener(this);
    button5.addActionListener(this);
    
    

    text=new JTextArea(60,80);
    mainpanel.add(text);
jsp=new JScrollPane(text);
        mainpanel.add(jsp);
    }
         
        
        
       public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource()==button4)
        {
        str=text.getSelectedText();
        i=text.getText().indexOf(str);
        text.replaceRange(" ",i,i+str.length());
        }
        if(ae.getSource()==button)
        {
        dispose();
        Notepro note1 = new Notepro("Untitled-Notepad");
        note1.setSize(500,500);
        note1.setVisible(true);
        }
        command=(String)ae.getActionCommand();

        if(command.equals("New..."))
        {
    
        dispose();
        Notepro note1 = new Notepro("Untitled-Notepad");
        note1.setSize(500,500);
        note1.setVisible(true);
        
        }
        
        try
        {
        if((command.equals("Open"))||(ae.getSource()==button1))
        {
        str4=" ";
        FileDialog dialog=new FileDialog(this,"Open");
        dialog.setVisible(true);
        str1=dialog.getDirectory();
        str2=dialog.getFile();
        setTitle(str2);
        str3=str1+str2;
        File f=new File(str3);
        FileInputStream fobj=new FileInputStream(f);
        len=(int)f.length();
        for(int j=0;j<len;j++)
        {
            char str5=(char)fobj.read();
            str4=str4 + str5;
            text.setText(str4);
        }

        this.text.setText(str4);

        }
        }
        catch(IOException e)
        {}
        try
        {
        if((command.equals("Save As..."))||(ae.getSource()==button2))
        {
        FileDialog dialog1=new FileDialog(this,"Save As",FileDialog.SAVE);
        dialog1.setVisible(true);
        str7=dialog1.getDirectory();
        str8=dialog1.getFile();
        str9=str7+str8;
        str6=text.getText();
        len1=str6.length();
        byte buf[]=str6.getBytes();

        File f1=new File(str9);
        
        FileOutputStream fobj1=new FileOutputStream(f1);
        for(int k=0;k<len1;k++)
        {
        fobj1.write(buf[k]);
        }
        fobj1.close();
        }

        this.setTitle(str8);

        }
        catch(IOException e){}



        if(command.equals("Exit"))
        {
        System.exit(0);
        }

        if((command.equals("Cut"))||(ae.getSource()==button4))
        {
        str=text.getSelectedText();
        i=text.getText().indexOf(str);
        text.replaceRange(" ",i,i+str.length());
        }

        if((command.equals("Copy"))||(ae.getSource()==button3))
        {
        str=text.getSelectedText();
        }

        if((command.equals("Paste"))||(ae.getSource()==button5))
        {
        pos1=text.getCaretPosition();
        text.insert(str,pos1);
        }
        if(command.equals("Delete"))
        {
        String msg=text.getSelectedText();
        i=text.getText().indexOf(msg);
        text.replaceRange(" ",i,i+msg.length());
        }
        if(command.equals("Font"))
        {

          MyFont  m=new MyFont(this);
        }
    }
    public static void main(String args[])
    {
    Notepro note = new Notepro("Untitled-Notepad");
    note.setSize(500,500);
    note.setVisible(true);
    }
};

class MyWindowAdapter extends WindowAdapter
{
 Notepro mynote;
 MyWindowAdapter(Notepro mynote)
 {
  this.mynote=mynote;
 }
 public void windowClosing(WindowEvent we)
 {

  System.exit(0);
 }
}

class MyFont extends Dialog implements ActionListener,ItemListener
{
 GraphicsEnvironment ge;
 java.awt.List l1,l2,l3;
 Button b1,b2;
 Font f1[];
 String f[];
 int i,j;
 Notepro p;
 TextField tf1,tf2,tf3;
Label ll1,ll2,ll3;
 String fn;
int fs,fsize;
 public MyFont(Notepro myno)
 {
    super(myno,"Font",true);

  setLayout(null);

p=myno;

  ge=GraphicsEnvironment.getLocalGraphicsEnvironment();
  l1=new java.awt.List();
  l2=new java.awt.List();
  l3=new java.awt.List();
  b1=new Button("OK");
  b2=new Button("CANCEL");
tf1=new TextField(10);
tf2=new TextField(10);
tf3=new TextField(10);
ll1=new Label("Font");
ll2=new Label("Font Style");
ll3=new Label("Size");
    ll1.setBounds(20,60,100,20);
    add(ll1);
    tf1.setBounds(20,80,100,20);
    add(tf1);
        l1.setBounds(20,100,100,150);
        add(l1);
    ll2.setBounds(140,60,100,20);
    add(ll2);
    tf2.setBounds(140,80,100,20);
    add(tf2);
   l2.setBounds(140,100,100,150);
  add(l2);
    ll3.setBounds(260,60,100,20);
    add(ll3);

    tf3.setBounds(260,80,100,20);
    add(tf3);

       l3.setBounds(260,100,100,150);    
  add(l3);

    b1.setBounds(380,70,80,20);
  add(b1);
    b2.setBounds(380,95,80,20);
  add(b2);

  f=ge.getAvailableFontFamilyNames();

  for(i=0;i<f.length;i++)
  {
   l1.add(f[i]);
   }

    l2.add("Regular");
l2.add("Bold");
l2.add("Italic");
l2.add("BoldItalic");
   for(i=8;i<=72;i+=2)
      l3.add(String.valueOf(i));

   b2.addActionListener(this);
   b1.addActionListener(this);
l1.addItemListener(this);
l2.addItemListener(this);
l3.addItemListener(this);
   setSize(480,280);
setResizable(false);    
   setVisible(true);

 }


 public void actionPerformed(ActionEvent fe)
 {
  if(fe.getSource()==b2)
  {
   dispose();
    }

  if(fe.getSource()==b1)
    {

    p.text.setFont(new Font(fn,fs,fsize));
    dispose();
    }
   }

public void itemStateChanged(ItemEvent ie)
    {

     if(ie.getSource()==l1)
      {
   tf1.setText(l1.getSelectedItem());
    fn=tf1.getText();
       }
    
    if(ie.getSource()==l2)
        {
        tf2.setText(l2.getSelectedItem());
        if(tf2.getText().equals("Bold"))
          fs=Font.BOLD;
          
         else
                if(tf2.getText().equals("Italic"))
            fs=Font.ITALIC;
       else
        if(tf2.getText().equals("BoldItalic"))
              fs=Font.BOLD+Font.ITALIC;
         else
            fs=Font.PLAIN;    
    
    if(tf2.getText()=="")
    {
     tf2.setText("Regular");
      fs=Font.PLAIN; 
    }    
        }
    
    if(ie.getSource()==l3)
      {  
          
     tf3.setText(l3.getSelectedItem());
      fsize=Integer.parseInt(tf3.getText());
              if(tf3.getText()=="")
        {
              fsize=8;
        }    
               }    
    }
}
 

Bandu

Journeyman
Check the .gif files that you are using. Do they open up properly in IE / FF or MS Paint?

You did not provide your images here, so I just downloaded a sample new.gif image and used it in the program. Worked for me.

Here is the snap of your own program with the sample new.gif used by me:
*img508.imageshack.us/img508/3787/swatcd2.png

Here is the new.gif that I used.
*img76.imageshack.us/img76/7214/newxx5.gif

Please also specify how you are running your program and what is the directory structure?
 

QwertyManiac

Commander in Chief
Isn't it better to use resource files/classes to do icon-work? So that it works fine when run from any location?
 
Status
Not open for further replies.
Top Bottom