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 9 of 9

Thread: Layout problems

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Layout problems

    Hi!

    I have often problems with Layouts. How can I set component at exact location.
    I thought I can do
    button.setLocation(20,40) but some strange things happens all the time. The problem is with this
    Layouts (Flow, Grid, Border...). In C# I think there are not these problems.
    Can I turn off all these layouts and put things much simpler. At this time I have this code and when I run this
    the table is not showed at window?

    public class nothing extends JFrame {

    private JTable tabpredmet;

    public nothing () {
    super("govno");
    setLayout(null);
    setSize(400, 700);
    setVisible(true);

    String[][] data = new String[][]{{"Srpski jezik"}, {"Engleski jezik"}, {"Ruski jezik"}};
    String[] col = {"Predmet"};
    tabpredmet = new JTable(data, col);
    tabpredmet.setPreferredScrollableViewportSize(new Dimension(200, 200));
    tabpredmet.setFillsViewportHeight(true);
    // JScrollPane jscrollpane = new JScrollPane(tabpredmet);
    JScrollPane p = new JScrollPane(tabpredmet);
    add(p);
    p.setVisible(true);
    p.setLocation(50, 50);

    }

    public static void main(String[] args) {
    nothing a = new nothing ();
    a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);




    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: Layout problems

    You can use a null layout, but this is a very bad practice.

    This is in the tutorial which is the first link for googling "java layouts": Doing Without a Layout Manager (Absolute Positioning) (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Layout problems

    I saw this but there is a very little explained here

    --- Update ---

    If I use managers then I have to put components where java lets me put it.
    This layouts restrict my choice of putting and arranging components as I want.

    --- Update ---

    I wounder what bill gates thinks about this concept

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

    Default Re: Layout problems

    With GridBagLayout you can end up with just about any layout you want, but with the added complication of placing everything exactly where you want it. (not that such a thing is too difficult)

    Layout managers attempt to achieve the look you desire, but you have to manage the managers. You have to set up the hierarchy of containers in a manner such that the manager can apply it's specific rule set to it's specific contents. Depending on the complication involved this may require multiple JPanels or JScrollPanes etc to be contained within even other JPanels for example. I know this sounds confusing, I can not find the right words to express my thoughts. Perhaps by example... but the code you posted does not demonstrate enough complexity.

    Looking over the code posted:
    1) The first thing I see is setLayout(null). If you want the layout managers to be responsible for the positioning, then don't fire them before the job starts. Remove this line.
    2) The second thing I notice is the use of setSize and setVisible at the top of the constructor, before all contents have been added. Add all components prior to setting the JFrame size and showing. Move these two lines of code down to the last thing the JFrame constructor is responsible for.

    After those changes are made, post the code again if you have any more questions.

    Please see the announcements page for the use of code tags when posting code, as well as other useful tips on the use of the forum.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: Layout problems

    Quote Originally Posted by lijepdan View Post
    I saw this but there is a very little explained here

    --- Update ---

    If I use managers then I have to put components where java lets me put it.
    This layouts restrict my choice of putting and arranging components as I want.

    --- Update ---

    I wounder what bill gates thinks about this concept
    Sure, using a null layout allows you to put components wherever you want. And that might seem simpler for trivial projects. But for involved guis, what happens when the window is resized? Or when you want to insert another component? With a null layout, both of those things (which happen all the time) will be a nightmare. Using a layout takes care of that for you.

    It's really up to you, but using a null layout is almost never the way to go.

    Also keep in mind that you can (and almost always will for any non-trivial gui) use nested layouts to put components pretty much wherever you want them, without the nightmares involved with resizing or insertion.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Mar 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Layout problems

    Thanks for help. I am starting now with this and it is confusing. And I can't find at one place all stuff I need to know.
    There are a lot of tutorials like the one mentioned above at docs.oracle.com/javase/tutorial/ but these are simple stuff.

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

    Default Re: Layout problems

    Did you read post #4?
    Have you tried what was suggested?
    Where is the revised code? What is the new question?

    If you follow the directions in post #4 using the code in post #1 you should see some changes in the display. If this is not suitable, talk more about what you want and don't forget to post the revised code.

  8. #8
    Junior Member
    Join Date
    Mar 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Layout problems

    I fixed the problem in my code, but that code is not very important to me. It was just a little practice. I just wanted to show little example of what
    I don't understand. I started to learning layout managers.

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Layout problems

    Hi!

    I have been working past time with this and I got pretty ok. I practiced a lot with BorderLayout, BoxLayout and GridBagLayout managers.
    This things are not complicated as they seemed to me at the beginning.

Similar Threads

  1. swing layout problems
    By xchan in forum AWT / Java Swing
    Replies: 6
    Last Post: April 21st, 2013, 05:29 PM
  2. Is there a layout for this?
    By gkffjcs in forum AWT / Java Swing
    Replies: 2
    Last Post: September 27th, 2011, 09:15 AM
  3. Replies: 1
    Last Post: April 14th, 2011, 07:50 AM
  4. Grid bag layout inside grid bag layout
    By kiddkoder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 29th, 2011, 08:07 AM
  5. Replies: 0
    Last Post: November 27th, 2010, 09:47 PM