Results 1 to 4 of 4
  1. #1
    archanajathan is offline Member
    Join Date
    Nov 2007
    Posts
    5
    Rep Power
    0

    Question Java Components are not displayed(sometimes)

    My java applications consists of a 2 JTextArea, JButton, and 2 images.

    When i execute my program, i have noticed that sometimes all these components dont get displayed.

    For example, the 1st JTextArea gets displayed but the rest of the components are hidden. The JButton gets displayed when i move the mouse over it and the 2nd textarea gets displayed when i select its text.

    I have included setVisible(True); at every possible and needed step.

    This problem sounds strange. And it happens atleast once in 3 executions. Rest of the 2 executions seems just normal (as expected).

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Let's see how you put your gui together.

  3. #3
    archanajathan is offline Member
    Join Date
    Nov 2007
    Posts
    5
    Rep Power
    0

    Default

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.io.*;
    import javax.imageio.*;
    import java.awt.image.*;
    import java.awt.event.*;
    import java.applet.*;
    import java.net.*;
    
    public class welcome extends JFrame
    {
    	JTextArea txt=new JTextArea("WELCOME");		//TEXT AREA
    	JTextArea txt1=new JTextArea("HELLO");		//TEXT AREA
    	JButton enter=new JButton(" ENTER ");		//ENTER BUTTON
    	JLabel label_image1=new JLabel();		
    	JLabel label_image2=new JLabel();	
    
    	JPanel jp=(JPanel)this.getContentPane();
    	
    	
    	public static void main(String args[])
    	{
    		new welcome();
    	}
    	
    	public welcome()
    	{
    		setExtendedState(getExtendedState() | Frame.MAXIMIZED_BOTH);//MAXIMIZES THE WINDOW ON EXECUTION
    		try
    		{
    		label_image1.setIcon(new ImageIcon("a4.jpg"));				//SETS THE IMAGES OF THE LABELS
    		label_image2.setIcon(new ImageIcon("a5.jpg"));
    		
    		jp.setLayout(null);//REQUIRED TO POSITION THE COMPONENTS 
    		jp.setBackground(new Color(0,0,0));							//FRAME BACKGROUND SET TO BLACK
    		
    		enter.setForeground(new Color(0,255,255));					//ENTER BUTTON FOREGROUND = LIGHT BLUE
    		txt.setForeground(new Color(255,0,0));						//FOREGROUND OF TEXT = RED
    		txt1.setForeground(new Color(255,0,0));						//FOREGROUND OF TEXT = RED
    		
    		enter.setBackground(new Color(0,0,255));					//BACKGROUND OF BUTTON = DARK BLUE
    		txt.setBackground(new Color(0,0,0));						//BACKGROUND OF TEXT = BLACK
    		txt1.setBackground(new Color(0,0,0));						//BACKGROUND OF TEXT = BLACK
    		
    		enter.setFont(new Font("Cooper Black",Font.PLAIN, 30));		//FONT OF ENTER BUTTON
    		txt.setFont(new Font("Courier", Font.BOLD, 48));			//FONT OF TEXT
    		txt1.setFont(new Font("Courier", Font.BOLD, 20));			//FONT OF TEXT
    		
    		enter.setBounds(430,450,150,50);							//PARAMETERS(X,Y,WIDTH,HEIGHT)
    		txt.setBounds(300,100,400,200);								//SET POSITION AND SIZE OF THE COMPONENTS 
    		txt1.setBounds(300,350,500,100);								//SET POSITION AND SIZE OF THE COMPONENTS 
    		label_image1.setBounds(50,50,100,200);
    		label_image2.setBounds(600,550,100,200);
    		
    		enter.setBorder(BorderFactory.createLineBorder(Color.red, 10));	//CREATE BORDER FOR ENTER BUTTON AS RED
    		txt.setEditable(false);											//SET TO UNEDITABLE
    		txt1.setEditable(false);											//SET TO UNEDITABLE
    		
    		setSize(800,600);											//SET SIZE OF FRAME 
    		setVisible(true);
    		
    		
    		jp.add(enter);
    		setVisible(true);												//ADDED ALL THE COMPONENTS TO FRAME/PANEL
    		jp.add(txt);
    		setVisible(true);
    		jp.add(txt1);
    		setVisible(true);
    		jp.add(label_image1);
    		setVisible(true);
    		jp.add(label_image2);
    		setVisible(true);
    		}catch(Exception e){}
    		
    	}
    }
    Do execute the code 4-5 times.. I have not sent the images.. It is supposed to diaplay 2 texts, 1 button and 2 images.
    Last edited by JavaBean; 11-05-2007 at 08:14 AM. Reason: Code placed inside [code] tag.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Works okay now: all components show up and remain visible.
    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class Welcome extends JFrame
    {
        JTextArea txt=new JTextArea("Welcome");
        JTextArea txt1=new JTextArea("Hello");
        JButton enter=new JButton(" Enter ");
        JLabel label_image1=new JLabel(); 
        JLabel label_image2=new JLabel(); 
    
        JPanel jp=(JPanel)this.getContentPane();
    
        public static void main(String args[])
        {
            new Welcome();
        }
    
        public Welcome()
        {
    //        setExtendedState(getExtendedState() | Frame.MAXIMIZED_BOTH);
            label_image1.setIcon(new ImageIcon(//"a4.jpg"));
                                               "images/Bird.gif"));
            label_image2.setIcon(new ImageIcon(//"a5.jpg"));
                                               "images/Rabbit.gif"));
    
            jp.setLayout(null); 
    //        jp.setBackground(new Color(0,0,0));
    
    //        enter.setForeground(new Color(0,255,255));
            txt.setForeground(new Color(255,0,0));
            txt1.setForeground(new Color(255,0,0));
    
    //        enter.setBackground(new Color(0,0,255));
    //        txt.setBackground(new Color(0,0,0));
    //        txt1.setBackground(new Color(0,0,0));
    
            enter.setFont(new Font("Cooper Black",Font.PLAIN, 30));
            txt.setFont(new Font("Courier", Font.BOLD, 48));
            txt1.setFont(new Font("Courier", Font.BOLD, 20));
    
            enter.setBounds(430,450,150,50);  // ) <-- errant brace
            txt.setBounds(300,100,400,200);
            txt1.setBounds(300,350,500,100);
            Dimension d = label_image1.getPreferredSize();
            label_image1.setBounds(//50,50,100,200);
                                   50,50,d.width,d.height);
            d = label_image2.getPreferredSize();
            label_image2.setBounds(//600,550,100,200);  // was offscreen
                                   50,350,d.width,d.height);
    
    //        enter.setBorder(BorderFactory.createLineBorder(Color.red, 10));
            txt.setEditable(false);
            txt1.setEditable(false);
    
            jp.add(enter);
            jp.add(txt);
            jp.add(txt1);
            jp.add(label_image1);
            jp.add(label_image2);
    
            setSize(800,600);
            setLocation(100,100);
            // Always call this method last.
            setVisible(true);
        }
    }

Similar Threads

  1. Replies: 1
    Last Post: 05-01-2008, 03:19 PM
  2. displayed the rownumbers
    By geeta_ravikanti in forum JDBC
    Replies: 1
    Last Post: 04-22-2008, 02:30 AM
  3. Useful Java Application Components 1.0.0-b3
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 03-16-2008, 08:41 PM
  4. Replies: 0
    Last Post: 12-06-2007, 06:10 PM
  5. Useful Java Application Components 0.9.27
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 10-02-2007, 04:14 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
  •