Results 1 to 20 of 23
- 05-16-2010, 06:34 PM #1
Member
- Join Date
- Apr 2010
- Location
- Cambridge
- Posts
- 30
- Rep Power
- 0
Components not appearing until hover over or altering size of window
Hello just a quick question this time;
Some of my components such as my JLabel arn't showing, however when i run system print, it possitively shows it is running fine, and when i alter the size of the window or hover over where the label should be, it finally appears.
Am i missing something within my panel code? Or within my component itself?
Thanks for any help!:p
- 05-16-2010, 08:12 PM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
I think that without code or a decent crystal ball, all we can make are wild-@rsed guesses, which I'm not too good at.
- 05-16-2010, 08:30 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 05-16-2010, 10:27 PM #4
Member
- Join Date
- Apr 2010
- Location
- Cambridge
- Posts
- 30
- Rep Power
- 0
Problem sorted,
No i did not use LayoutManager, i don't even know what that is :P
However i simply used the "updateUI();" command, now its all working smoothly.
Closed
- 05-16-2010, 11:11 PM #5
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
And that is completely wrong. There is never any need to use that method.However i simply used the "updateUI();" command,
Well, now is a good time to learn. Read the section from the Swing tutorial on Using Layout Managers.No i did not use LayoutManager, i don't even know what that is
Your basic problem is that you are making the frame visible 'before" you add components to it.
- 05-17-2010, 11:30 AM #6
Member
- Join Date
- Apr 2010
- Location
- Cambridge
- Posts
- 30
- Rep Power
- 0
- 05-17-2010, 05:03 PM #7
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
There is no need to ever use that method, so yes it is wrong to use it just because it happens to fix your problem.
It means you have a bad design of your program and you are using a hack to fix it. If you program was properly designed (ie. if you follow the examples in the Swing tutorial), then you would not have this problem and you would not have needed to post a question in the forum.
You can learn the proper way to use Swing or you can learn the wrong way. The proper way results in more stable less bug prone applications.
- 05-17-2010, 05:39 PM #8
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
- 05-17-2010, 07:02 PM #9
Member
- Join Date
- Apr 2010
- Location
- Cambridge
- Posts
- 30
- Rep Power
- 0
I have read the swing tutorials swiftly many times, especially when i am building an application.
I do not see what i did wrong within the code itself, however the problem was fixed using a piece of code.
Now, i understand that the problem probably shouldn't have occured in the first place. However as i do not know what i did wrong after reading the swing tutorials.
Therefore i am neither happy or unhappy with my outcome, it got me to where i need to be, therefore, for me updateUI(); is not "wrong".
Thread closed.
- 05-17-2010, 09:17 PM #10
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Again, show the code if you really want to know what you're doing wrong. Otherwise if you simply don't care, then sure, close the thread, but if so, I hope that you never become a professional programmer or my physician.
- 05-17-2010, 09:25 PM #11
Member
- Join Date
- Apr 2010
- Location
- Cambridge
- Posts
- 30
- Rep Power
- 0
Ok then heres my code;
Java Code:package map; import java.awt.*; import javax.swing.event.*; import java.awt.event.*; import javax.swing.*; public class Map extends JFrame implements ActionListener, ChangeListener { JPanel map; JPanel radiopanel; JPanel keypanel; JPanel distancepanel; JLabel maplabel; JLabel picture; JLabel from; JLabel to; JComboBox combo1; JComboBox combo2; ImageIcon mapicon; JRadioButton reception; JRadioButton leonardo; JRadioButton burma; JRadioButton australia; JRadioButton minden; JRadioButton library; JRadioButton edmund; JLabel choose; JLabel receptioninfo; JLabel leonardoinfo; JLabel australiainfo; JLabel burmainfo; JLabel mindeninfo; JLabel libraryinfo; JLabel edmundinfo; JLabel distanceis; ButtonGroup groupleft; String receptionString = "Reception"; String leonardoString = "Leonardo House"; String australiaString = "Australia House"; String burmaString = "Burma House"; String mindenString = "Minden House"; String libraryString = "Library"; String edmundString = "Edmund House"; String[] comboStrings = {"Reception", "Leonardo House", "Australia House", "Burma House", "Minden House", "Library", "Edmund House"}; String[] comboStrings2 = {"Reception", "Leonardo House", "Australia House", "Burma House", "Minden House", "Library", "Edmund House"}; public static void main(String[] args) { Map drawingFrame = new Map(); drawingFrame.setSize(900, 800); drawingFrame.createGUI(); drawingFrame.setTitle("Interactive Map"); drawingFrame.setVisible(true); } public void createGUI() { this.setDefaultCloseOperation(EXIT_ON_CLOSE); Container window = this.getContentPane(); map = new JPanel(); map.setLayout(null); map.setBackground(Color.LIGHT_GRAY); map.setBounds(0, 0, 500, 600); window.add(map); map.setVisible(true); mapicon = new ImageIcon("G:\\My Documents\\Programming\\Assignment 3\\map.png"); maplabel = new JLabel(mapicon); maplabel.setVerticalAlignment(JLabel.TOP); maplabel.setBounds(180, 0, 400, 800); map.add(maplabel); radiopanel = new JPanel(); radiopanel.setBounds(580, 50, 500, 150); radiopanel.setLayout(null); radiopanel.setBackground(Color.LIGHT_GRAY); map.add(radiopanel); choose = new JLabel("Please choose hotspots for information."); choose.setBackground(Color.LIGHT_GRAY); choose.setBounds(600, 20, 300, 20); map.add(choose); reception = new JRadioButton(receptionString); reception.setMnemonic(KeyEvent.VK_R); reception.setActionCommand(receptionString); reception.setSelected(false); reception.setBackground(Color.LIGHT_GRAY); reception.setBounds(10, 10, 100, 15); reception.setVisible(true); radiopanel.add(reception); leonardo = new JRadioButton(leonardoString); leonardo.setMnemonic(KeyEvent.VK_R); leonardo.setActionCommand(leonardoString); leonardo.setSelected(false); leonardo.setBackground(Color.LIGHT_GRAY); leonardo.setBounds(10, 30, 150, 15); radiopanel.add(leonardo); australia = new JRadioButton(australiaString); australia.setMnemonic(KeyEvent.VK_R); australia.setActionCommand(australiaString); australia.setSelected(false); australia.setBackground(Color.LIGHT_GRAY); australia.setBounds(10, 50, 150, 15); radiopanel.add(australia); burma = new JRadioButton(burmaString); burma.setMnemonic(KeyEvent.VK_R); burma.setActionCommand(burmaString); burma.setSelected(false); burma.setBackground(Color.LIGHT_GRAY); burma.setBounds(10, 70, 150, 15); radiopanel.add(burma); minden = new JRadioButton(mindenString); minden.setMnemonic(KeyEvent.VK_R); minden.setActionCommand(mindenString); minden.setSelected(false); minden.setBackground(Color.LIGHT_GRAY); minden.setBounds(10, 90, 150, 15); radiopanel.add(minden); edmund = new JRadioButton(edmundString); edmund.setMnemonic(KeyEvent.VK_R); edmund.setActionCommand(edmundString); edmund.setSelected(false); edmund.setBackground(Color.LIGHT_GRAY); edmund.setBounds(10, 110, 150, 15); radiopanel.add(edmund); library = new JRadioButton(libraryString); library.setMnemonic(KeyEvent.VK_R); library.setActionCommand(libraryString); library.setSelected(false); library.setBackground(Color.LIGHT_GRAY); library.setBounds(10, 130, 150, 15); radiopanel.add(library); ButtonGroup groupleft = new ButtonGroup(); groupleft.add(reception); groupleft.add(leonardo); groupleft.add(australia); groupleft.add(burma); groupleft.add(edmund); groupleft.add(library); groupleft.add(minden); reception.addActionListener(this); leonardo.addActionListener(this); australia.addActionListener(this); burma.addActionListener(this); edmund.addActionListener(this); library.addActionListener(this); minden.addActionListener(this); keypanel = new JPanel(); keypanel.setLayout(null); keypanel.setBackground(Color.LIGHT_GRAY); keypanel.setBounds(590, 220, 280, 150); map.add(keypanel); keypanel.grabFocus(); keypanel.setVisible(true); receptioninfo = new JLabel("Reception info here!"); receptioninfo.setBounds(0, 0, 240, 200); receptioninfo.setVerticalAlignment(JLabel.TOP); leonardoinfo = new JLabel("Leonardo info here!"); leonardoinfo.setBounds(0, 0, 240, 200); leonardoinfo.setVerticalAlignment(JLabel.TOP); australiainfo = new JLabel("Australia info here!"); australiainfo.setBounds(0, 0, 240, 200); australiainfo.setVerticalAlignment(JLabel.TOP); burmainfo = new JLabel("Burma info here!"); burmainfo.setBounds(0, 0, 240, 200); burmainfo.setVerticalAlignment(JLabel.TOP); edmundinfo = new JLabel("Edmund info here!"); edmundinfo.setBounds(0, 0, 240, 200); edmundinfo.setVerticalAlignment(JLabel.TOP); libraryinfo = new JLabel("Library info here!"); libraryinfo.setBounds(0, 0, 240, 200); libraryinfo.setVerticalAlignment(JLabel.TOP); mindeninfo = new JLabel("Minden info here!"); mindeninfo.setBounds(0, 0, 240, 200); mindeninfo.setVerticalAlignment(JLabel.TOP); distancepanel = new JPanel(); distancepanel.setLayout(null); distancepanel.setBounds(590, 400, 280, 300); distancepanel.setBackground(Color.LIGHT_GRAY); map.add(distancepanel); JComboBox chooseleft = new JComboBox(comboStrings); chooseleft.setSelectedIndex(0); chooseleft.addActionListener(this); chooseleft.setBackground(Color.LIGHT_GRAY); chooseleft.setBounds(0, 30, 250, 20); distancepanel.add(chooseleft); JComboBox chooseright = new JComboBox(comboStrings2); chooseright.setSelectedIndex(1); chooseright.addActionListener(this); chooseright.setBackground(Color.LIGHT_GRAY); chooseright.setBounds(0, 80, 250, 20); distancepanel.add(chooseright); from = new JLabel("From:"); from.setBounds(0, 0, 100, 30); distancepanel.add(from); to = new JLabel("To:"); to.setBounds(0, 50, 100, 30); distancepanel.add(to); distanceis = new JLabel("Distance:"); distanceis.setBounds(0, 110, 100, 30); distancepanel.add(distanceis); } public void actionPerformed(ActionEvent e) { if (reception.isSelected()) { keypanel.removeAll(); keypanel.add(receptioninfo); map.updateUI(); } else if (leonardo.isSelected()) { keypanel.removeAll(); keypanel.add(leonardoinfo); map.updateUI(); } else if (australia.isSelected()) { keypanel.removeAll(); keypanel.add(australiainfo); map.updateUI(); } else if (burma.isSelected()) { keypanel.removeAll(); keypanel.add(burmainfo); map.updateUI(); } else if (minden.isSelected()) { keypanel.removeAll(); keypanel.add(mindeninfo); map.updateUI(); } else if (library.isSelected()) { keypanel.removeAll(); keypanel.add(libraryinfo); map.updateUI(); } else if (edmund.isSelected()) { keypanel.removeAll(); keypanel.add(edmundinfo); map.updateUI(); } } public void stateChanged(ChangeEvent e) { } }
- 05-17-2010, 10:16 PM #12
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
To be honest, I'm surprised your program is displaying at all with all the null Layouts in there. I'm having trouble just doing a SSCCE...
However, the problem is you are adding components to the window while it is already visible in the actionPerformed(). You should call the validate() on the parent component (Your JFrame) to correctly lay out all the components again.
- 05-17-2010, 11:18 PM #13
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Agree: use layout managers and your problems should be solved. You'll also want to refactor that "God-class" into at least two if not more subclasses to help simplify and reduce redundancy.
- 05-18-2010, 07:21 AM #14
Member
- Join Date
- Apr 2010
- Location
- Cambridge
- Posts
- 30
- Rep Power
- 0
-
God class or God Object is a class that tries to do way too much. For more, please look here: God object - Wikipedia, the free encyclopedia
- 05-18-2010, 04:36 PM #16
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Not only is your solution to use updateUI wrong, you don't even know how to properly ask a question. No where in you question did you state that your where tying to components from a visible GUI and then add a new panel containing different components.
The better solution in this case is to use a Card Layout. Again if you read the tutorial about using layout managers as was suggested you would know about this layout manager. The tutorial even provides a working example, so how much easier can it be?
Seriously? That about it for a minute. You have been told NOT to use updateUI, don't you think any suggestion we make would be to replace that method call?validate(); Where do i add this?
Also, instead of validate I generally use
panel.revalidate();
panel.repaint();
just to make sure layout and repainting is done.
- 05-18-2010, 09:15 PM #17
Member
- Join Date
- Apr 2010
- Location
- Cambridge
- Posts
- 30
- Rep Power
- 0
Right listen, i've had enough of you treating me like im a complete prick.
Im new to the language just like im sure you were at one point in your life.
So if your not gonna help me and just pick at thing's i say and do, then your not welcome on my thread, go pick on someone elses thread.
- 05-19-2010, 12:54 AM #18
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Thats my point, I learned by listening to other peoples suggestions, not by disregarding them.Im new to the language just like im sure you were at one point in your life.
Everything I've said has been designed to help you learn and understand better programming and design techniques.So if your not gonna help me
Your the one with the attitude that says "if it works, thats fine by me I don't need to change it and learn anything else".
- 05-19-2010, 08:19 AM #19
Member
- Join Date
- Apr 2010
- Location
- Cambridge
- Posts
- 30
- Rep Power
- 0
- 05-19-2010, 08:28 AM #20
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
But you are arguing already; Camickr (and others) weren't 'digging into you'; they were criticizing your code; if you can find it, read "The Egoless Programmer" by Henry Weinberg. It's a healthy read. If you can't stand any critique on your code you're heading the wrong way.
kind regards,
Jos
Similar Threads
-
change text size in all GUI components
By itaipee in forum AWT / SwingReplies: 2Last Post: 08-07-2009, 04:59 AM -
JFrame 's components size and location problem
By petrosgraf in forum Threads and SynchronizationReplies: 5Last Post: 04-18-2009, 02:24 AM -
XTerm window appearing in full screen swing app
By clarose in forum AWT / SwingReplies: 1Last Post: 11-17-2008, 10:56 PM -
How to resize the components with the change in the size of the window?
By shaunak kiwalkar in forum SWT / JFaceReplies: 1Last Post: 06-06-2008, 07:55 AM -
gridbaglayout: increase/decrease size of components.
By newtojava7 in forum New To JavaReplies: 2Last Post: 01-28-2008, 07:22 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks