Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: How to get visible size of jpanel

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default How to get visible size of jpanel

    a.jpg
    So, i noticed a little misbehavior in java with all the sizes of jpanel. When i set bounds of jpanel, i.e., setbounds(0, 0, 100, 100), then the first point's (0,0) position is normal, but the second point is not point (100,100), because the size actually includes window decorations and/or shadows - a real mess, dont know what excatly is included, and the last visible point is something like (80,80) ... How can i get window decoration size, or how can i set visible size, not real size ?


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: How to get visible size of jpanel

    You have to make a difference between the window size and the panel size. setBounds is hardly needed as LayoutManagers take care of that stuff.
    What are you actually trying to do? Your approach smells like a misuse of the API.

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to get visible size of jpanel

    I dont like layout managers and the way they work, so i dont use them, i do everything manually, it is much easier to place elements in the right place manualy, layout manager is like stupid AI in games Actually, this is not a big deal, i could just calculate myself java's miscalculations, but i just wondered if there is something in java, that determines size of window borders.

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to get visible size of jpanel

    Quote Originally Posted by startas View Post
    How can i get window decoration size, or how can i set visible size, not real size ?
    Sounds like a bug introduced by the the code itself. Post the code in question where we can have a look

  5. #5
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: How to get visible size of jpanel

    There are no miscalculations, there are wrong excpectations on your part. If you don't use LayoutManagers, you have to come up with these silly hacks. I wonder what your GUI looks like on different devices with different screen sizes and users resizing the top level component
    You have to get the panel's location in all parent containers and have to do the math yourself.

  6. #6
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to get visible size of jpanel

    It will be just a program for myself, so no different pcs, especially devices, and screens. I'm just starting to build my programs gui, but initial code looks like this :
    import java.awt.Dimension;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public class MyProgram{
        private JFrame frame;
        private JPanel panel;
        private JLabel label;
        private int frameWidth = 1300;
        private int frameHeight = 700;
        private ImageIcon backgroundImage;
     
        public MyProgram() {
            initUI();
        }
     
     
        public void initUI() {
            frame = new JFrame("my program");
            backgroundImage = new ImageIcon("images/background.jpg"); 
            panel = new JPanel();
            label = new JLabel(backgroundImage);
     
            panel.setLayout(null);
            frame.setLayout(null);
            frame.add(label);
     
            frame.setBounds(10, 10, frameWidth, frameHeight);
            panel.setBounds(0, 0, frameWidth, frameHeight);
     
            panel.setSize(new Dimension(frameWidth, frameHeight));
            frame.setSize(new Dimension(frameWidth, frameHeight));
            label.setSize(new Dimension(frameWidth, frameHeight));
            label.setBounds(0, 0, frameWidth, frameHeight);
            frame.setResizable(false);
     
     
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setVisible(true);
        }
     
        public static void main(String[] args) {
            new MyProgram();
        }
    }

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to get visible size of jpanel

    The panel is never added to the frame, fix that first...

  8. #8
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to get visible size of jpanel

    Ok, my bad, update : it's not the point if it is jpanel or jlabel or jbutton or whatever, the point is, i can set frame's size i.e. to 1300x700, but i cant fit other elements into frame's work place 1:1, my element's (buttons, labels, other things...) starting point (0,0) gets good position, as you can see in picture above - it's point number 1, but i dont know how to get/set the last point in frame's work place (xxx,yyy) - point number 2 in picture above - without manual calculations / tweaks like frameWidth-15 or whatever i will get from experiments.

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to get visible size of jpanel

    See post #5.
    Learn to use layout managers, it is not hard, and very helpful when used correctly.

    Do not force the size of the frame. Set the preferred size of the JPanel, add everything to the panel, add the panel to the frame, call frame.pack().
    The frame will size itself around the panel and give itself room for the decorations, the top left corner of the displayable area will be 0,0

  10. #10
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to get visible size of jpanel

    If i have a simple JTabbedPane tabbedPane, and i add a few tabs to it with tabbedPane.addTab("name", JPanel panel), is it possible to add the same one element, i.e. JButton, to all of those panels-tabs ? Because i simply tried to do to all panels panelX.add(button), but button is visible only on the last panel i added it to, not on all panels.

  11. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to get visible size of jpanel

    s it possible to add the same one element, i.e. JButton, to all of those panels-tabs ?
    No it is not. A component can only be in one place at any given point in time. If added to a new/different container, it will remove itself from its previous container (as shown by your tests). I do not have the documentation handy at this point, but this rule is printed somewhere in there, and should be easy enough to discover with a search engine and enough keywords.

  12. #12
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to get visible size of jpanel

    So the only variant to keep one element over every other element is to add that element directly to frame, i.e. JFrame frame.add(button, 0) ?

  13. #13
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to get visible size of jpanel

    What do you mean by "over every other element"?
    ---What ever, the answer will still be 'No'
    JFrame is a Container and will function like all other Containers in terms of how things are treated when added, (Other factors do come in to play like LayoutManager)

  14. #14
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to get visible size of jpanel

    Well, visually. I have a few panels-tabs, tabs has their own elements, and i need a way to add somewhere element, which will be visible over all other elements, so far the only way i found is to add element directly to jframe to position 0, frame.add(element, 0).

  15. #15
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to get visible size of jpanel

    I do not understand the effect you are trying to achieve, perhaps a screenshot of what you want, or some code to show what you are attempting to accomplish?

  16. #16
    Member
    Join Date
    Aug 2013
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: How to get visible size of jpanel

    I'm making a simple game, and part of the game is elements, that randomly appears/disappears at random positions, i realised these elements via jbutton, and i'm looking for the best way to keep these elements always on top over the rest of graphics. The part of appearing of element is realised via a simple frame.add(button, 0), and part of removing button is realised via frame.remove(button).
    There is nothing to not understand, open task manager and set "always on top", you will see what i'm trying to do.

  17. #17
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to get visible size of jpanel

    You have an endearing personality, even in this medium.

  18. #18
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: How to get visible size of jpanel

    "on top" and "over" could mean the y or z ordering depending on the context, and you were never specific.
    Look at the glass pane and the layered pane

Similar Threads

  1. Jpanel preffered size exceed the nactual screen size
    By manish.ctae@gmail.com in forum AWT / Java Swing
    Replies: 2
    Last Post: October 31st, 2012, 01:29 AM
  2. Adjusting Size of JPanel to Boundaries of Painting
    By aussiemcgr in forum Java Theory & Questions
    Replies: 6
    Last Post: November 22nd, 2010, 01:17 PM
  3. Limit File Size or Request Size
    By tarek.mostafa in forum Java Servlet
    Replies: 3
    Last Post: June 12th, 2010, 04:28 PM
  4. Limit File Size or Request Size
    By tarek.mostafa in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: June 11th, 2010, 07:21 AM