Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding adding buttons inside BorderLayout at a specific location  RSS feed

 
Ravikiran Vishnuvajhala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to draw a graph and add few buttons at the bottom of the page. I am using BorderLayout and have put the Graph (using a JPanel) at the Center and trying to add buttons to the South region (using a JPanel with GridBagLayout). What I want to find out is - Can I specifically add the buttons to a particular location in the UI (specifying the pixels, or whatever units) or Swing takes care of doing it. Can we have any control over it? Or is it better achieved using GridBagLayout for the entire UI?

Thanks
Ravi
[ January 29, 2007: Message edited by: Ravikiran Vishnuvajhala ]
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really want to specify the pixel locations of every button, there are ways to do it. It is not recommended, though. For one, you don't know how big the buttons want to be, since it depends on the Look and Feel. (Maybe Windows Vistana or MacOS 10.6 will have really big buttons, and if you specify the pixel locations they will overlap each other or be ugly in some other way.)

The "Swing way" is to use a layout manager, though I wouldn't recommend GridBagLayout for most people. You say you are adding only a "few buttons," so how hard could it be. FlowLayout will center them them in a vertical line. If you want more than one line of buttons you can stack two or more FlowLayout panels or use something like GridLayout.
 
Ravikiran Vishnuvajhala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian,

I have tried what you suggested, but that didn't work. Let me explain you the problem in more detail and hopefully, that might help you suggest an answer for my problem. I want to create the following UI -


Please bear with me on the drawing. The single dash lines denote the boundary of the region. Please copy the diagram and paste it into a notepad or textpad to view it properly
To achieve this, I tried every possible layout, and nothing seems to have worked.

Basically, I am using a JPanels inside JApplet for each region. The panel that is used for graph region uses the pixels to draw the graph (with the reference of x and y as 0,0 at top left corner). I am not able to fix the same reference for the Legend. Whatever I do, the Legend seems to go far to the right i.e. I tried drawing the Legend using 0,0 as the starting point coordinate and that didn't work ( I can see my legend to the far right when expand the size of the applet), and then I tried to draw the Legend using the endpoint x-coordinate of the Graph Panel as the start point x-coordinate (y-coordinate would anyways start from 0)and that didn't work either.

One soln. I implemented was just draw the Graph and the Legend in the same panel and that worked, but I am not satisfied with the soln. because that is not a true Obect Oriented Approach.

Please let me know what you think.

Thanks
Ravi

[EJFH: Added CODE tags to preserve formatting ]
[ January 31, 2007: Message edited by: Ernest Friedman-Hill ]
 
Brian Cole
Author
Ranch Hand
Posts: 947
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ravikiran Vishnuvajhala:
To achieve this, I tried every possible layout


That I doubt.

There are possible UIs that are challenging to implement with the basic Java layout managers, but the one you present is fairly simple.



In fact, this is the kind of layout you would expect to see in any introductory book covering Java GUI programming. For example, page 281 (chapter 7) of this old AWT book.
[ January 31, 2007: Message edited by: Brian Cole ]
 
Ravikiran Vishnuvajhala
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brian,

I understand that Brian. It is very basic to miss out that layout. But, believe me, it does not seem to work in my case. May be I have not explained you the case correctly. I am in the middle of another project right now. I will send you the sample of my applet later once I am done with my current project and may be it will be clearer for you.

Thank you for your suggestions.

Thanks
Ravi
[ February 06, 2007: Message edited by: Ravikiran Vishnuvajhala ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!