Page 1 of 2 12 LastLast
Results 1 to 20 of 23
  1. #1
    goffy is offline Member
    Join Date
    Apr 2010
    Location
    Cambridge
    Posts
    30
    Rep Power
    0

    Default 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

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    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.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by goffy View Post
    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
    You did use a LayoutManager did you?

    kind regards,

    Jos

  4. #4
    goffy is offline Member
    Join Date
    Apr 2010
    Location
    Cambridge
    Posts
    30
    Rep Power
    0

    Default

    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

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    However i simply used the "updateUI();" command,
    And that is completely wrong. There is never any need to use that method.

    No i did not use LayoutManager, i don't even know what that is
    Well, now is a good time to learn. Read the section from the Swing tutorial on Using Layout Managers.

    Your basic problem is that you are making the frame visible 'before" you add components to it.

  6. #6
    goffy is offline Member
    Join Date
    Apr 2010
    Location
    Cambridge
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    And that is completely wrong. There is never any need to use that method.



    Well, now is a good time to learn. Read the section from the Swing tutorial on Using Layout Managers.

    Your basic problem is that you are making the frame visible 'before" you add components to it.
    The method updateUI(); worked so obviously it isn't wrong lol..
    But i'l have a read of the link.

  7. #7
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    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.

  8. #8
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by goffy View Post
    The method updateUI(); worked so obviously it isn't wrong lol..
    When I told my sister-in-law she was fat, she stopped talking to me, so this worked too, but is just as wrong as your statement above. ;)

  9. #9
    goffy is offline Member
    Join Date
    Apr 2010
    Location
    Cambridge
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    When I told my sister-in-law she was fat, she stopped talking to me, so this worked too, but is just as wrong as your statement above. ;)
    Quote Originally Posted by camickr View Post
    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.
    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.

  10. #10
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    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.

  11. #11
    goffy is offline Member
    Join Date
    Apr 2010
    Location
    Cambridge
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by curmudgeon View Post
    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.
    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) {
        }
    }

  12. #12
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    307
    Rep Power
    6

    Default

    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.

  13. #13
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    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.

  14. #14
    goffy is offline Member
    Join Date
    Apr 2010
    Location
    Cambridge
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by StormyWaters View Post
    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.
    Quote Originally Posted by curmudgeon View Post
    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.
    Thanks for your help guys but i don't understand a few things here;
    validate(); Where do i add this?
    My parent component as in the window/frame setup?
    what is a god-class?

    Thanks :)

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

  16. #16
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    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?

    validate(); Where do i add this?
    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?

    Also, instead of validate I generally use

    panel.revalidate();
    panel.repaint();

    just to make sure layout and repainting is done.

  17. #17
    goffy is offline Member
    Join Date
    Apr 2010
    Location
    Cambridge
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    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?

    Also, instead of validate I generally use

    panel.revalidate();
    panel.repaint();

    just to make sure layout and repainting is done.
    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.

  18. #18
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    Im new to the language just like im sure you were at one point in your life.
    Thats my point, I learned by listening to other peoples suggestions, not by disregarding them.

    So if your not gonna help me
    Everything I've said has been designed to help you learn and understand better programming and design techniques.

    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".

  19. #19
    goffy is offline Member
    Join Date
    Apr 2010
    Location
    Cambridge
    Posts
    30
    Rep Power
    0

    Default

    Quote Originally Posted by camickr View Post
    Thats my point, I learned by listening to other peoples suggestions, not by disregarding them.



    Everything I've said has been designed to help you learn and understand better programming and design techniques.

    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".
    Digging into me as if i commited a crime is NOT helping me.
    So im not going to argue.

  20. #20
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by goffy View Post
    Digging into me as if i commited a crime is NOT helping me.
    So im not going to argue.
    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

Page 1 of 2 12 LastLast

Similar Threads

  1. change text size in all GUI components
    By itaipee in forum AWT / Swing
    Replies: 2
    Last Post: 08-07-2009, 05:59 AM
  2. JFrame 's components size and location problem
    By petrosgraf in forum Threads and Synchronization
    Replies: 5
    Last Post: 04-18-2009, 03:24 AM
  3. XTerm window appearing in full screen swing app
    By clarose in forum AWT / Swing
    Replies: 1
    Last Post: 11-17-2008, 11:56 PM
  4. Replies: 1
    Last Post: 06-06-2008, 08:55 AM
  5. gridbaglayout: increase/decrease size of components.
    By newtojava7 in forum New To Java
    Replies: 2
    Last Post: 01-28-2008, 08:22 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
  •