Results 1 to 6 of 6
  1. #1
    r.srimathi is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Thumbs up Interface concept

    Hi friends,
    I need a small help from you.
    In my program i have used interface.
    I did that program in swings in JFrame but interface concept is not working
    If anyone of you have samplecode for interface concept in swings in frames
    send me URGENT:confused:

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    You have several unrelated concepts here. JFrame is a visual container in Swing. Interfaces are essentially a means of creating a contract for classes that implement the interface. For basic Swing classes, you don't need to create any interfaces, and you shouldn't have to implement any, either, unless you are doing something advanced.

    Look at Sun's Java Tutorial. If you don't know Java, start at the beginning. You won't be able to hack together a Swing program without at least a little background.

  3. #3
    lightstream is offline Member
    Join Date
    Jan 2009
    Posts
    9
    Rep Power
    0

    Default

    r.srimathi > Are you sure you aren't confusing GUI (graphic user interface) and interface as the contract for classes to comply with as suggested by Steve11235??

  4. #4
    r.srimathi is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    Thank u
    i know interface concept but the interface is not working while it is used in swings .That is my doubt.
    If i used in java its working. But in my program its throwing arrayindexoutofbounds exception.
    I have quoted the important things in red please see this and clear my doubt

    This is my where i am declaring a method which going to be called b using interface conceptimport java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.lang.*;

    public class test extends JFrame implements ActionListener,iest
    {

    private static final long serialVersionUID = 1L;
    JButton b,b1,b2,b3;
    JLabel l[],l1,l3,l4,l5,l6,l7;
    Font f,f1;
    JTextField t[],t1[],t2[],t3[],t4[],t5[],t6;
    Container c;
    int row[],col[],i,col1[];
    float m,k,z[] ;
    float n,p,q,r,s;
    int d;
    int ver,hor;
    JScrollPane bar;

    public test(int a)
    {
    super("AREA OF THE POOJA ROOM");

    b=new JButton("OK");
    b.addActionListener(this);
    b.setBounds(100, 500, 80, 30);
    b1=new JButton("CLEAR DATA");
    b1.addActionListener(this);
    b1.setBounds(210, 500, 120, 30);
    b2=new JButton("BACK");
    b2.addActionListener(this);
    b2.setBounds(340, 500, 80, 30);


    f=new Font("CopperBlack",Font.BOLD,13);
    f1=new Font("CopperBlack",Font.BOLD,11);
    l1=new JLabel("LENGTH (in SFT)");
    l1.setFont(f);
    l3=new JLabel("BREADTH (in SFT)");
    l3.setFont(f);
    l4=new JLabel("AREA (in SFT)");
    l4.setFont(f);
    l5=new JLabel("FEET IN");
    l5.setFont(f1);
    l6=new JLabel("FEET IN");
    l6.setFont(f1);
    l7=new JLabel(" FEET");
    l7.setFont(f1);

    c = getContentPane();
    c.setLayout(null);
    c.setBackground(Color.lightGray);
    c.setForeground(Color.black);

    t=new JTextField[10];
    l=new JLabel[10];
    t1=new JTextField[10];
    t2=new JTextField[10];
    t3=new JTextField[10];
    t4=new JTextField[10];
    t6=new JTextField(10);

    c.add(l1);
    c.add(l4);
    c.add(l3);
    c.add(l5);
    c.add(l6);
    c.add(l7);
    c.add(b);
    c.add(b1);
    c.add(b2);

    l1.setBounds(320, 10, 130, 50);
    l3.setBounds(450, 10, 130, 50);
    l4.setBounds(580, 10, 100, 50);
    l5.setBounds(345, 25, 100, 50);
    l6.setBounds(475, 25, 100, 50);
    l7.setBounds(605, 25, 100, 50);

    int col=60;
    int col1=60;

    d=a;
    for(i=1;i<=d;i++)
    {

    int row=40;

    l[i] = new JLabel("ENTER THE LENGTH & BREADTH FOR POOJA ROOM "+i);
    l[i].setBounds(row,col,350,30);
    c.add(l[i]);

    row=row+300;
    t[i]=new JTextField();
    c.add(t[i]);
    t[i].setBounds(row,col1,30,30);


    row=row+30;
    t1[i]=new JTextField();
    c.add(t1[i]);
    t1[i].setBounds(row,col1,30,30);

    row=row+100;
    t2[i]=new JTextField();
    c.add(t2[i]);
    t2[i].setBounds(row,col1,30,30);


    row=row+30;
    t3[i]=new JTextField();
    c.add(t3[i]);
    t3[i].setBounds(row,col1,30,30);

    row=row+100;
    t4[i]=new JTextField();
    c.add(t4[i]);
    t4[i].setBounds(row,col1,70,30);


    col=col+70;
    col1=col1+70;
    }


    setSize(800, 800);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public void actionPerformed(ActionEvent ae)
    {

    if(ae.getSource()==b)
    {

    for (int i = 1; i <=d; i++)
    {
    z=new float[10];
    try
    {
    m= Integer.parseInt(t[i].getText());
    n= Integer.parseInt(t1[i].getText());
    n=n/12;
    k=m+n;
    p= Integer.parseInt(t2[i].getText());
    q= Integer.parseInt(t3[i].getText());
    q=q/12;
    r=p+q;
    s=k*r;
    t4[i].setText(" "+s);
    z[i]=s;
    }
    catch (Exception err)
    {
    System.out.println("test"+err);
    }
    }

    }
    if(ae.getSource()==b2)
    {
    new est2();
    }
    if(ae.getSource()==b1)
    {
    new est5(d,z);
    }
    }
    public void pass()
    {
    System.out.println("test");
    }
    }

    This is my interface

    public interface iest
    {
    void pass();
    }

    here i am calling pass method

    import javax.swing.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;

    public class iest1
    {
    public static void main(String args[])
    {

    iest obe = new test(' ');
    obe.pass();
    }
    }

    Once again thank you

  5. #5
    r.srimathi is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    Thank u
    i know interface concept but the interface is not working while it is used in swings .That is my doubt.
    If i used in java its working. But in my program its throwing arrayindexoutofbounds exception.
    I have quoted the important things in red please see this and clear my doubt

    This is my where i am declaring a method which going to be called b using interface conceptimport java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import java.lang.*;

    public class test extends JFrame implements ActionListener,iest
    {

    private static final long serialVersionUID = 1L;
    JButton b,b1,b2,b3;
    JLabel l[],l1,l3,l4,l5,l6,l7;
    Font f,f1;
    JTextField t[],t1[],t2[],t3[],t4[],t5[],t6;
    Container c;
    int row[],col[],i,col1[];
    float m,k,z[] ;
    float n,p,q,r,s;
    int d;
    int ver,hor;
    JScrollPane bar;

    public test(int a)
    {
    super("AREA OF THE POOJA ROOM");

    b=new JButton("OK");
    b.addActionListener(this);
    b.setBounds(100, 500, 80, 30);
    b1=new JButton("CLEAR DATA");
    b1.addActionListener(this);
    b1.setBounds(210, 500, 120, 30);
    b2=new JButton("BACK");
    b2.addActionListener(this);
    b2.setBounds(340, 500, 80, 30);


    f=new Font("CopperBlack",Font.BOLD,13);
    f1=new Font("CopperBlack",Font.BOLD,11);
    l1=new JLabel("LENGTH (in SFT)");
    l1.setFont(f);
    l3=new JLabel("BREADTH (in SFT)");
    l3.setFont(f);
    l4=new JLabel("AREA (in SFT)");
    l4.setFont(f);
    l5=new JLabel("FEET IN");
    l5.setFont(f1);
    l6=new JLabel("FEET IN");
    l6.setFont(f1);
    l7=new JLabel(" FEET");
    l7.setFont(f1);

    c = getContentPane();
    c.setLayout(null);
    c.setBackground(Color.lightGray);
    c.setForeground(Color.black);

    t=new JTextField[10];
    l=new JLabel[10];
    t1=new JTextField[10];
    t2=new JTextField[10];
    t3=new JTextField[10];
    t4=new JTextField[10];
    t6=new JTextField(10);

    c.add(l1);
    c.add(l4);
    c.add(l3);
    c.add(l5);
    c.add(l6);
    c.add(l7);
    c.add(b);
    c.add(b1);
    c.add(b2);

    l1.setBounds(320, 10, 130, 50);
    l3.setBounds(450, 10, 130, 50);
    l4.setBounds(580, 10, 100, 50);
    l5.setBounds(345, 25, 100, 50);
    l6.setBounds(475, 25, 100, 50);
    l7.setBounds(605, 25, 100, 50);

    int col=60;
    int col1=60;

    d=a;
    for(i=1;i<=d;i++)
    {

    int row=40;

    l[i] = new JLabel("ENTER THE LENGTH & BREADTH FOR POOJA ROOM "+i);
    l[i].setBounds(row,col,350,30);
    c.add(l[i]);

    row=row+300;
    t[i]=new JTextField();
    c.add(t[i]);
    t[i].setBounds(row,col1,30,30);


    row=row+30;
    t1[i]=new JTextField();
    c.add(t1[i]);
    t1[i].setBounds(row,col1,30,30);

    row=row+100;
    t2[i]=new JTextField();
    c.add(t2[i]);
    t2[i].setBounds(row,col1,30,30);


    row=row+30;
    t3[i]=new JTextField();
    c.add(t3[i]);
    t3[i].setBounds(row,col1,30,30);

    row=row+100;
    t4[i]=new JTextField();
    c.add(t4[i]);
    t4[i].setBounds(row,col1,70,30);


    col=col+70;
    col1=col1+70;
    }


    setSize(800, 800);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public void actionPerformed(ActionEvent ae)
    {

    if(ae.getSource()==b)
    {

    for (int i = 1; i <=d; i++)
    {
    z=new float[10];
    try
    {
    m= Integer.parseInt(t[i].getText());
    n= Integer.parseInt(t1[i].getText());
    n=n/12;
    k=m+n;
    p= Integer.parseInt(t2[i].getText());
    q= Integer.parseInt(t3[i].getText());
    q=q/12;
    r=p+q;
    s=k*r;
    t4[i].setText(" "+s);
    z[i]=s;
    }
    catch (Exception err)
    {
    System.out.println("test"+err);
    }
    }

    }
    if(ae.getSource()==b2)
    {
    new est2();
    }
    if(ae.getSource()==b1)
    {
    new est5(d,z);
    }
    }
    public void pass()
    {
    System.out.println("test");
    }}

    This is my interface

    public interface iest
    {
    void pass();
    }

    here i am calling pass method

    import javax.swing.*;
    import java.io.*;
    import java.awt.*;
    import java.awt.event.*;

    public class iest1
    {
    public static void main(String args[])
    {

    iest obe = new test(' ');
    obe.pass();
    }
    }

    Once again thank you

  6. #6
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    But in my program its throwing arrayindexoutofbounds exception.
    Your problem has nothing to do with an interface. You have an array of some size, and you are accessing it using an index that is too large.

    One simple mistake is to try to access the last element in an array using myArray.size. That will lead to the exception, because arrays are zero based. The correct expression is

    myArray[myArray.size - 1]

    You have a lot of arrays, and I didn't see the specific location of the error...

    One last thing. Consider using something like an ArrayList instead of an array. It is self-expanding, and it helps avoid some of the problems associated with arrays.

Similar Threads

  1. What is Seeding Concept?
    By bhupal4all in forum New To Java
    Replies: 1
    Last Post: 09-03-2008, 06:47 AM
  2. Map concept in JAVA
    By Mathan in forum New To Java
    Replies: 19
    Last Post: 06-27-2008, 07:02 AM
  3. mail concept
    By indirani in forum New To Java
    Replies: 3
    Last Post: 04-16-2008, 02:30 PM
  4. mail concept
    By thamizhisai in forum Advanced Java
    Replies: 4
    Last Post: 04-11-2008, 08:19 AM
  5. What is RMI concept in Spring Framework
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 10-04-2007, 10:21 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
  •