Solved

JFrame

Posted on 1998-11-04
4
733 Views
Last Modified: 2013-11-23
Hi,

I created a JFrame with a JButton.If i pressed the JButton
I am calling a JInternalFrame .
Here My problem is once the JInternalFrame comes ,the
JButton in the JFrame is not visible.
Is there any method to avoid this.How will solve this problem.

Here is My code.

JFrame With Button
-------------------------------
import java.awt.*;
import java.awt.event.*;
import com.sun.java.swing.*;
public class Jmain extends JFrame implements ActionListener
{
      JLabel l1,l2,l3,l4;
      JTextField tf1,tf2;
      JPasswordField pf;
      JButton b1,b2;
      JDesktopPane desktop;
      Rectangle r1,r2,r3,r4,r5,r6,r7,r8,r9,r10;
      //passcheck pc;
      //error ee;
      //boolean bool1,bool2;
      public Jmain()
      {
            int inset = 50;
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            setBounds(inset, inset,
              screenSize.width - inset*2,
              screenSize.height-inset*2);
            /*l1=new JLabel("CheckIT");
            l2=new JLabel("AdminiName");
            l3=new JLabel("AdminiID");
            l4=new JLabel("Password");
            tf1=new JTextField(25);
            tf2=new JTextField(25);
            pf=new JPasswordField(25);
            pf.setEchoChar('*');*/
            b1=new JButton("UserDetail");
            b1.addActionListener(this);
            b1.setBounds(30,235,150,30);
            getContentPane().add(b1);
            l2=new JLabel("AdminiName");
            l2.setBounds(100,100,20,30);
            getContentPane().add(l2);
            l2.setVisible(false);
            /*b2=new JButton("QuestionBank");
            b1.addActionListener(this);
            //b2.addActionListener(this);
            getContentPane().add(l1);
            getContentPane().add(l2);
            getContentPane().add(tf1);
            getContentPane().add(l3);
            getContentPane().add(tf2);
            getContentPane().add(l4);
            getContentPane().add(pf);
            getContentPane().add(b1);
            getContentPane().add(b2);
            tf1.setSelectionColor(Color.blue);*/
            setVisible(true);
            addWindowListener(new winHandle());
      
      }
      class winHandle extends WindowAdapter
      {
            public void windowClosing(WindowEvent e)
            {
                  System.exit(0);
            }
      }
      public void actionPerformed(ActionEvent e)
      {
            String s=e.getActionCommand();
            if(s.equals("UserDetail"))
            {
                  System.out.println("Before");
                  
            desktop = new JDesktopPane();
            JUserDetail in=new JUserDetail();
            desktop.add(in);
            try{
                  in.setSelected(true);
            }catch(Exception ee){}
            setContentPane(desktop);
            setVisible(true);
            System.out.println("After");
            
            }
            if(s.equals("QuestionBank"))
            {
                  System.out.println("QuestionBank");
            }
      }
      /*public void paint(Graphics p)
      {
            
            Font f1=new Font("TimesRoman",Font.BOLD+Font.ITALIC,25);
            Font f2=new Font("TimesRoman",Font.BOLD,15);
            l1.setFont(f1);
            r1=new Rectangle(150,50,100,30);
            l1.setBounds(r1);
            r2=new Rectangle(80,100,100,30);
            l2.setBounds(r2);
            l2.setFont(f2);
            r3=new Rectangle(220,103,100,24);
            tf1.setBounds(r3);
            r4=new Rectangle(80,140,100,30);
            l3.setBounds(r4);
            l3.setFont(f2);
            r5=new Rectangle(220,143,100,24);
            tf2.setBounds(r5);
            r6=new Rectangle(80,180,100,30);
            l4.setBounds(r6);
            l4.setFont(f2);
            r7=new Rectangle(220,183,100,24);
            pf.setBounds(r7);
            r8=new Rectangle(30,235,150,30);
            b1.setBounds(r8);
            r9=new Rectangle(210,234,150,30);
            b2.setBounds(r9);
            
      }*/
            public static void main(String arg[])
            {
                  Jmain j=new Jmain();
                  j.show();
            }
      }

Here only one JButton  Userdetail is  visible.If i click that JButton it will call another JInternalFrame.

JInternalFrame
----------------------
import com.sun.java.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JUserDetail extends JInternalFrame implements ActionListener
{
      JLabel l1,l2,l3,l4,l5,l6;
      JTextField tf1,tf2,tf3,tf4;
      JTextArea ta;
      JCheckBox cb;
      JButton b1,b2,b3,b4,b5,b6;
      JScrollPane jp;
      Rectangle r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13,r14,r15,r16,r17,r18;
      public JUserDetail()
      {
            super("UserSecurityDetail",true,true,true,true);
            Font f=new Font("TimesRoman",Font.BOLD+Font.ITALIC,15);
            l1=new JLabel("User Name");
            l1.setFont(f);
            l1.setBounds(50,10,100,30);
            getContentPane().add(l1);
            tf1=new JTextField();
            tf1.setBounds(150,12,120,28);
            getContentPane().add(tf1);
            l2=new JLabel("User No");
            l2.setBounds(50,60,100,30);
            l2.setFont(f);
            getContentPane().add(l2);
            tf2=new JTextField();
            tf2.setBounds(150,62,120,28);
            getContentPane().add(tf2);
            l3=new JLabel("Password");
            l3.setBounds(50,110,100,30);
            l3.setFont(f);      
            getContentPane().add(l3);
            tf3=new JTextField();
            tf3.setBounds(150,112,120,28);
            getContentPane().add(tf3);
            l4=new JLabel("User Level");
            l4.setBounds(50,160,100,30);
            l4.setFont(f);      
            getContentPane().add(l4);
            tf4=new JTextField();
            tf4.setBounds(150,162,120,28);
            getContentPane().add(tf4);
            l5=new JLabel("Active");
            l5.setBounds(50,210,100,30);
            l5.setFont(f);      
            getContentPane().add(l5);
            cb=new JCheckBox();
            cb.setBounds(150,212,30,30);
            getContentPane().add(cb);
            l6=new JLabel("User Details/Remarks");
            l6.setBounds(325,20,200,30);
            l6.setFont(f);
            getContentPane().add(l6);
            ta=new JTextArea();
            jp=new JScrollPane(ta);
            jp.setBounds(300,50,250,75);
            getContentPane().add(jp);
            b1=new JButton("ADD");
            b1.setBounds(5,300,100,30);
            getContentPane().add(b1);
            b2=new JButton("EDIT");
            b2.setBounds(105,300,100,30);
            getContentPane().add(b2);
            b3=new JButton("DELETE");
            b3.setBounds(205,300,100,30);
            getContentPane().add(b3);
            b4=new JButton("SAVE");
            b4.setBounds(305,300,100,30);
            getContentPane().add(b4);
            b5=new JButton("EXIT");
            b5.setBounds(405,300,100,30);
            getContentPane().add(b5);
            b5.addActionListener(this);
            b6=new JButton("RESET");
            b6.setBounds(505,300,100,30);
            getContentPane().add(b6);
            JButton b7=new JButton("Junk");
            b7.setBounds(550,300,10,10);
            getContentPane().add(b7);
            b7.setVisible(false);
            setSize(615,400);
            setLocation(250,10);
                        
      }
            public void actionPerformed(ActionEvent e)
            {
                  setVisible(false);
                  dispose();
            }
      
      
}
if i press the JButton this JInternalFrame is coming but the JButton is not visible.
0
Comment
Question by:senthil_krn
  • 2
4 Comments
 
LVL 7

Expert Comment

by:conick
ID: 1226913

If you could include your code it may help someone determine how to avoid your problem.

You can set the size, with setSize(Dimension d) and the location, with setLocation(Point p), of the internal frame within the container.  That way you can put your internal frame somewhere that the button isnt.

If that doenst help include your code and someone with more experience would surely help you.

0
 

Author Comment

by:senthil_krn
ID: 1226914
Edited text of question
0
 

Author Comment

by:senthil_krn
ID: 1226915
Adjusted points to 30
0
 

Accepted Solution

by:
pocram earned 30 total points
ID: 1226916
this is a correct code for Jmain:

you have to initialyse your JDesktopPane in Jmain, and not any more in your actionPerformed method, and define it as the ContentPane of your JFrame!
PS:no modifications for your JInternalFrame


    public class Jmain extends JFrame implements ActionListener
    {
    JLabel l1,l2,l3,l4;
    JTextField tf1,tf2;
    JPasswordField pf;
    JButton b1,b2;
    JDesktopPane desktop = new JDesktopPane();
    Rectangle r1,r2,r3,r4,r5,r6,r7,r8,r9,r10;
    //passcheck pc;
    //error ee;
    //boolean bool1,bool2;
    public Jmain()
    {
    int inset = 50;
    setContentPane(desktop);
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setBounds(inset, inset,
      screenSize.width - inset*2,
      screenSize.height-inset*2);
    /*l1=new JLabel("CheckIT");
    l2=new JLabel("AdminiName");
    l3=new JLabel("AdminiID");
    l4=new JLabel("Password");
    tf1=new JTextField(25);
    tf2=new JTextField(25);
    pf=new JPasswordField(25);
    pf.setEchoChar('*');*/
    b1=new JButton("UserDetail");
    b1.addActionListener(this);
    b1.setBounds(30,235,150,30);
    getContentPane().add(b1);
    l2=new JLabel("AdminiName");
    l2.setBounds(100,100,20,30);
    getContentPane().add(l2);
    l2.setVisible(false);
    /*b2=new JButton("QuestionBank");
    b1.addActionListener(this);
    //b2.addActionListener(this);
    getContentPane().add(l1);
    getContentPane().add(l2);
    getContentPane().add(tf1);
    getContentPane().add(l3);
    getContentPane().add(tf2);
    getContentPane().add(l4);
    getContentPane().add(pf);
    getContentPane().add(b1);
    getContentPane().add(b2);
    tf1.setSelectionColor(Color.blue);*/
    setVisible(true);
    addWindowListener(new winHandle());

    }
    class winHandle extends WindowAdapter
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    }
    public void actionPerformed(ActionEvent e)
    {
    String s=e.getActionCommand();
    if(s.equals("UserDetail"))
    {
    System.out.println("Before");

    JUserDetail in=new JUserDetail();
    desktop.add(in);
    try{
    in.setSelected(true);
    }catch(Exception ee){}
    setContentPane(desktop);
    setVisible(true);
    System.out.println("After");

    }
    if(s.equals("QuestionBank"))
    {
    System.out.println("QuestionBank");
    }
    }
    /*public void paint(Graphics p)
    {

    Font f1=new Font("TimesRoman",Font.BOLD+Font.ITALIC,25);
    Font f2=new Font("TimesRoman",Font.BOLD,15);
    l1.setFont(f1);
    r1=new Rectangle(150,50,100,30);
    l1.setBounds(r1);
    r2=new Rectangle(80,100,100,30);
    l2.setBounds(r2);
    l2.setFont(f2);
    r3=new Rectangle(220,103,100,24);
    tf1.setBounds(r3);
    r4=new Rectangle(80,140,100,30);
    l3.setBounds(r4);
    l3.setFont(f2);
    r5=new Rectangle(220,143,100,24);
    tf2.setBounds(r5);
    r6=new Rectangle(80,180,100,30);
    l4.setBounds(r6);
    l4.setFont(f2);
    r7=new Rectangle(220,183,100,24);
    pf.setBounds(r7);
    r8=new Rectangle(30,235,150,30);
    b1.setBounds(r8);
    r9=new Rectangle(210,234,150,30);
    b2.setBounds(r9);

    }*/
    public static void main(String arg[])
    {
    Jmain j=new Jmain();
    j.show();
    }
    }

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now