Results 1 to 16 of 16
  1. #1
    javaTech is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default Jbutton do not show up

    Hi,

    I have defined a JPanel in my code which conains some buttons but some times the buttons in the panel are not displayed or only few of them are dipslyed.Most of the times it functions properly and i am able to see all the buttons.

    Here is the code -

    protected JPanel DialPanel;
    protected JButton m_btn1;
    protected JButton m_btn2;

    DialPanel = new JPanel();
    getContentPane().add(DialPanel);
    DialPanel.setBounds(0, 231, 280, 133);
    DialPanel.setLayout(null);
    DialPanel.setBackground(bkColor);
    {
    m_btn1 = new JButton();
    DialPanel.add(m_btn1);
    m_btn1.setIcon(imgDPOne);
    m_btn1.setContentAreaFilled(false);
    m_btn1.setBounds(79, 0, 35, 28);
    m_btn1.setBorderPainted(false);
    }
    {
    m_btn2 = new JButton();
    DialPanel.add(m_btn2);
    m_btn2.setBounds(120, 0, 35, 28);
    m_btn2.setIcon(imgDPTwo);
    m_btn2.setContentAreaFilled(false);
    m_btn2.setBorderPainted(false);
    }
    }

    Will adding m_btn1.setVisible(true) and m_btn2.setVisible(true) help resolve this issue?But without this also most of the time it works correctly.
    Can someone please help me with this?

    Thanks

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I doubt that setVisible(true) will help on the buttons at all, unless you call setVisible(false) some time (and I don't recommend this). You may get a lot of benefit by reading the Sun Swing Layout tutorial. This will help you avoid using null layout and absolute positioning and instead let the buttons and their containers decide themselves what would be their best size and location. It makes maintenance of code a LOT easier.

  3. #3
    javaTech is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    Hi ,

    Thanks a lot for replying.

    After going through the tutorial i came to know about two functions- revalidate() and repaint(). I was not aware fo these before and so have not used them in my code. Do you think using these can resolve the problem?
    I am not bale to reproduce this isue. It hapens only in poduction environment. So i don't really know in what scenario this occurs and will this change have any effect.

  4. #4
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    You have to call validate() on containers when you first set-up their contents, and revalidate() after that when you add/remove components.
    You have to call repaint() whenever you want to update the display.

    I would imagine that using them would help greatly.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  5. #5
    javaTech is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    Thanks.I will do that change.

    I have one more question. Any idea why issue occurs sometimes? It is not consistent. It works perfectly most of the times. Can you plaese help me undersatnd if there is a problem with the definition of the containers why this does not happen all the times and occurs only sometimes.What factors influence the behavior and cause this to happen?

    Thanks

  6. #6
    howardL's Avatar
    howardL is offline Member
    Join Date
    Apr 2009
    Posts
    2
    Rep Power
    0

    Default

    Hi,
    I remember I face this problem once I forgotten how I fix it, but I remember I adjust on the object.setBounds(x, y, width, height) also the below part. maybe your setBounds overlap.
    if not it could be the order you put.
    like
    form = new JFrame();
    content = new JPanel();
    object = new JButton("Login");
    object.setBounds(x, y, w, h);
    .
    .
    .
    // this part is important, I remember I adjust this part also

    content.add(object);
    form.add(content);
    form.setVisible(true);

    if I am wrong correct me.

  7. #7
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    You are wrong, Howard. As posted before, the OP should use a LayoutManager. By that he doesn't have to worry about bounds and sizes, that will get screwed up anyway as soon as the user resizes the window/container.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I agree strongly with PhHein: using (often nested) layout managers and letting the manager do the hard business of figuring out exactly where to place components is the way to go. It is a much more robust and flexible way of creating GUIs.

    Imagine placing a bunch of components in a JFrame by hand then realizing that you need to have 5 JRadioButtons in the middle of the GUI, not 4. If you did this by hand, you'd have to manually re-position everything that was below the radio buttons to accomodate the new one creating 10-50 new lines of code and lots of room for bugs. If you used layout managers on the other hand, you would add the new radiobutton with a single line of code. You tell me which is easier to deal with in the long run?

  9. #9
    javaTech is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    I see below exception in the java console when the panel is expanded and button should show up but do not actually show up.
    Can some one please explain me why this exception occurs and what i should do to avoid this?

    java.lang.NullPointerException
    at sun.awt.windows.WWindowPeer.displayChanged(Unknown Source)
    at sun.awt.SunDisplayChanger.notifyListeners(Unknown Source)
    at sun.awt.Win32GraphicsDevice.displayChanged(Unknown Source)
    at sun.awt.Win32GraphicsEnvironment.displayChanged(Un known Source)
    at sun.awt.windows.WToolkit$4.run(Unknown Source)
    at java.awt.event.InvocationEvent.dispatch(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierar chy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

  10. #10
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Debug it with source attachments to find out which object was null, when it shouldn't have been.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default

    Just another cross poster.
    Swing - Jbuttons are not visible

    db

  12. #12
    javaTech is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    I have posted this problem in two seperate forums and not two times within the same forum. I think it is not wrong to do that. Please correct me if i am wrong.

  13. #13
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    I've tried and the Sun forums people refuse to answer unless you cross-post like this.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    JavaRanch says it nicely: BeForthrightWhenCrossPostingToOtherSites

    But regardless, I don't think that you've given enough information on either site to answer this question. I hope I"m wrong.

  15. #15
    javaTech is offline Member
    Join Date
    Apr 2009
    Posts
    6
    Rep Power
    0

    Default

    My intention was not to annoy anyone by posting my problem at two different forums. Sorry if i did that. I just wanted to get some more help on this problem.

    The suggestions i received on this one was mainly to use 'Layout Manager'. I tried using that but was running into other issues(issues with size). If i have to use Layout Manager then i guess i have to change the design of the full window. I cannot use layout manger on just the part of the window which is giving problem (please let me know i am wrong and there is a way to do that). Also, I am new to Swings and not much familiar with 'Layout Manager'.

    Is there a way to resolve this problem without using layout manager. This is not reproducible. It happens only in production.

    I am pasting my code below -

    I have modified the code as per the suggestions received from the forum to include repaint() and revalidate() methods.
    The part i added is in bold. Please let me know what changes i should do to improve this and resolve the issue.

    Java Code:
    public static void main(String[] args) {
    	{		
    		initGUI();		
    		frame = new JFrame();		
    		frame.getContentPane().add(this);		
    		frame.setBackground(LIGHT_BLUE);		
    		frame.setTitle("ABC");
    		logoImage = imgSPIcon.getImage();
    		frame.setIconImage(logoImage);		
    		frame.setSize(286,298);
    		frame.setResizable(false);		
    		frame.setVisible(false);   
                             frame.getContentPane().validate();   
                             frame.validate();
    	}
    	
    private void initGUI() {
    		try {		
    			this.setSize(294, 280);
    			this.setBackground(LIGHT_BLUE);
    			this.setName("ABC");
    			getContentPane().setBackground(this.LIGHT_BLUE);
    			getContentPane().setLayout(null);			
    			this.setFocusable(false);
    			
    			//collapse button used for expanding and collapsing the dial panel
    			{
    					m_btnCollapse = new JButton();
    					BottamPanel.add(m_btnCollapse);
    					m_btnCollapse.setBounds(163, 0, 112, 21);
    					m_btnCollapse.setIcon(imgExpandDialpad);
    					m_btnCollapse.setBorder(BorderFactory.createCompoundBorder(null, null));
    					m_btnCollapse.setBackground(new java.awt.Color(177, 201, 224));
    					m_btnCollapse.setContentAreaFilled(false);
    			}			
    			
    			//Dial panel 
    			{
    				DialPanel = new JPanel();				
    				//getContentPane().add(DialPanel);				
    				DialPanel.setBounds(0, 251, 280, 0);				
    				DialPanel.setLayout(null);
    				DialPanel.setBackground(bkColor);				
    				{
    					m_btn1 = new JButton();					
    					m_btn1.setIcon(imgDPOne);
    					m_btn1.setContentAreaFilled(false);
    					m_btn1.setBounds(79, 0, 35, 28);
    					m_btn1.setBorderPainted(false);
    					DialPanel.add(m_btn1);
    				}
    				{
    					m_btn2 = new JButton();					
    					m_btn2.setBounds(120, 0, 35, 28);
    					m_btn2.setIcon(imgDPTwo);
    					m_btn2.setContentAreaFilled(false);
    					m_btn2.setBorderPainted(false);
    					DialPanel.add(m_btn2);
    				}
    				{
    					m_btn3 = new JButton();					
    					m_btn3.setBounds(161, 0, 35, 28);
    					m_btn3.setIcon(imgDPThree);
    					m_btn3.setContentAreaFilled(false);
    					m_btn3.setBorderPainted(false);
    					DialPanel.add(m_btn3);
    				}
    				//includes more such buttons
                                    DialPanel.validate();
    				getContentPane().add(DialPanel);			
                             }
    			//adding mouse listner for the collapse button
    			JavaPhoneMouse aJavaPhoneMouse = new JavaPhoneMouse();
    			m_btnCollapse.addMouseListener(aJavaPhoneMouse);
    		}
    	}
    	//mouse listner.
    	class JavaPhoneMouse extends java.awt.event.MouseAdapter {
     
    		public void mouseClicked(java.awt.event.MouseEvent event) {
    			Object object = event.getSource();
     
    			if (object == m_btnCollapse)
    				m_btnCollapse_MouseClicked(event);
     
    		}
     
    	} 
     
    	//On expanding the dial panel the dial panel is expanded(the size changes) but the buttons are invisible.  
    	void m_btnCollapse_MouseClicked(java.awt.event.MouseEvent event) {
    		//if the dial pad is in expanded form it will collapse else it will be expanded.
    		if (isDialPadCollapsed == false) {			
    			DialPanel.setBounds(0, 251, 280, 0);
    			DialPanel.revalidate();
    			DialPanel.repaint();						
    			BottamPanel.setBounds(0, 251, 280, 21);
    			m_btnCollapse.setIcon(imgExpandDialpad);			
    			this.setSize(280, 277);			
    			frame.setResizable(true);
    			frame.setSize(286, 298);
    			frame.setResizable(false);
                            frame.getContentPane().repaint();
    			frame.repaint();						
    			isDialPadCollapsed = true;
    		} else {			
    			this.setSize(280, 408);			
    			frame.setResizable(true);
    			frame.setSize(286, 430);
    			frame.setResizable(false);			
    			BottamPanel.setBounds(0, 384, 280, 21);			
    			DialPanel.setBounds(0, 251, 280, 133);	
                            DialPanel.revalidate();
    			DialPanel.repaint();
    			frame.getContentPane().repaint();
    			frame.repaint();		
    			m_btnCollapse.setIcon(imgCollDialpad);
    			isDialPadCollapsed = false;
    		}
    		
    	}
    Thanks

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Show Javadoc isn't Working
    By ScottVal in forum NetBeans
    Replies: 8
    Last Post: 11-20-2009, 03:06 AM
  2. Creating a JButton that does not show focus
    By Java Tip in forum javax.swing
    Replies: 0
    Last Post: 06-26-2008, 07:38 PM
  3. How to Show Calendar
    By praveen.kb in forum AWT / Swing
    Replies: 2
    Last Post: 02-09-2008, 08:23 AM
  4. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 06:20 AM
  5. show a RTF FORMAT
    By Jack in forum Advanced Java
    Replies: 2
    Last Post: 07-04-2007, 03:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •