Results 1 to 16 of 16
Thread: Jbutton do not show up
- 04-13-2009, 08:59 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 6
- Rep Power
- 0
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
-
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.
- 04-22-2009, 01:50 AM #3
Member
- Join Date
- Apr 2009
- Posts
- 6
- Rep Power
- 0
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.
- 04-22-2009, 02:40 AM #4
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
- 04-22-2009, 06:00 PM #5
Member
- Join Date
- Apr 2009
- Posts
- 6
- Rep Power
- 0
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
- 04-23-2009, 10:03 AM #6
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.
- 04-23-2009, 10:13 AM #7
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.
-
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?
- 05-06-2009, 10:07 PM #9
Member
- Join Date
- Apr 2009
- Posts
- 6
- Rep Power
- 0
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)
- 05-06-2009, 11:01 PM #10
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
- 05-07-2009, 05:31 AM #11
Just another cross poster.
Swing - Jbuttons are not visible
db
- 05-08-2009, 12:37 AM #12
Member
- Join Date
- Apr 2009
- Posts
- 6
- Rep Power
- 0
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.
- 05-08-2009, 01:16 AM #13
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
-
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.
- 05-12-2009, 07:06 AM #15
Member
- Join Date
- Apr 2009
- Posts
- 6
- Rep Power
- 0
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.
ThanksJava 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; } }
-
Similar Threads
-
Show Javadoc isn't Working
By ScottVal in forum NetBeansReplies: 8Last Post: 11-20-2009, 03:06 AM -
Creating a JButton that does not show focus
By Java Tip in forum javax.swingReplies: 0Last Post: 06-26-2008, 07:38 PM -
How to Show Calendar
By praveen.kb in forum AWT / SwingReplies: 2Last Post: 02-09-2008, 08:23 AM -
netbeans 6.0 not show commpunent or show blank page
By fahimaamir in forum NetBeansReplies: 1Last Post: 01-26-2008, 06:20 AM -
show a RTF FORMAT
By Jack in forum Advanced JavaReplies: 2Last Post: 07-04-2007, 03:37 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks