JComponent Placement

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2008
Posts: 207
Reputation: llemes4011 is an unknown quantity at this point 
Solved Threads: 13
llemes4011 llemes4011 is offline Offline
Posting Whiz in Training

JComponent Placement

 
0
  #1
Jul 2nd, 2009
Hi. When I add a JComponent to my JFrame, it isn't the size of the remaining space in the frame. I want it to work in a way so that when i add a JComponent, it (and it's contents) stretches to fill the remaining space in the screen... Is there anyway to do that?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: JComponent Placement

 
0
  #2
Jul 2nd, 2009
Originally Posted by llemes4011 View Post
Hi. When I add a JComponent to my JFrame, it isn't the size of the remaining space in the frame. I want it to work in a way so that when i add a JComponent, it (and it's contents) stretches to fill the remaining space in the screen... Is there anyway to do that?
This will depend on your LayoutManager. What LayoutManager are you using?
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 207
Reputation: llemes4011 is an unknown quantity at this point 
Solved Threads: 13
llemes4011 llemes4011 is offline Offline
Posting Whiz in Training

Re: JComponent Placement

 
0
  #3
Jul 2nd, 2009
I'm not... But I found a way that works. I have a thread that has static copies of the JFrame and the JComponent, and is always watching to see if the height or width is changing, then resizes the JComponent. I don't know it that is the way it's supposed to be done (probably not), but it is working. I only have 2 Components in the frame, so i think this way should be fine....
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,631
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 470
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: JComponent Placement

 
0
  #4
Jul 3rd, 2009
VernonDozier>What LayoutManager are you using?
llemes4011>I'm not... But I found a way that works.
Can I see the way you have used?
Do you know BorderLayout, GridBagLayout & GridBagConstraints?
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 207
Reputation: llemes4011 is an unknown quantity at this point 
Solved Threads: 13
llemes4011 llemes4011 is offline Offline
Posting Whiz in Training

Re: JComponent Placement

 
0
  #5
Jul 3rd, 2009
Originally Posted by adatapost View Post
VernonDozier>What LayoutManager are you using?
llemes4011>I'm not... But I found a way that works.
Can I see the way you have used?
Do you know BorderLayout, GridBagLayout & GridBagConstraints?
Yeah here's the code:
  1. package JEmail.util;
  2.  
  3. import java.util.*;
  4. import java.awt.*;
  5. import javax.swing.*;
  6.  
  7. public class FrameResizeHandler extends Thread
  8. {
  9. //consists of 2 JSplitPanes (one nested)
  10. private static ContentViewer cv;
  11. //components to resize
  12. private static ArrayList<JComponent> components;
  13. // JFrame to resize to
  14. private static JFrame watching;
  15.  
  16. private static int width; // JFrame width to check against
  17. private static int height; // JFrame height to check against
  18.  
  19. /**
  20.   * I used an ArrayList and JComponents because I want to
  21.   * get it so that it can deal with multiple resizings at once
  22.   */
  23. public FrameResizeHandler(JFrame jf, JComponent jc){
  24. watching = jf;
  25. components = new ArrayList<JComponent>();
  26. components.add(jc);
  27. width = watching.getWidth();
  28. height = watching.getHeight();
  29. }
  30.  
  31. public void run(){
  32. while(true){
  33. if(width!=watching.getWidth() || height!=watching.getHeight()){
  34. if(components.get(0) instanceof ContentViewer){
  35. // I know what the first element is so that's why it looks like this
  36. cv = (ContentViewer)components.get(0);
  37. // adjust the JComponents' size
  38. cv.setNewSize(watching.getWidth()-watching.getInsets().left-watching.getInsets().right,
  39. watching.getHeight()-watching.getInsets().top-watching.getInsets().bottom-57);
  40. }
  41. width = watching.getWidth(); // re-adjust the dimensions to check against
  42. height = watching.getHeight();
  43. }
  44. }
  45. }
  46. }

Note that this is still VERY sloppy. It doesn't completely work (It doesn't fix the size when the MAXIMIZE button is pressed), and is slow. I need to put in checks to determine when the thread actually goes through its loop so it doesn't eat up processor space (WHICH IT DOES! >=[ )

But Yes. I do know about BorderLayout, and I hate GridBagLayout. It's a total pain in the butt. But it does have its perks. I used it in another section of my program and it worked well there. I couldn't get it to work here.
Last edited by llemes4011; Jul 3rd, 2009 at 5:09 am. Reason: Added Comments
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,631
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 470
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: JComponent Placement

 
0
  #6
Jul 3rd, 2009
Use BorderLayout and add a component with CENTER position. Do not forget to issue validate() method of container after component has been added to that container.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 207
Reputation: llemes4011 is an unknown quantity at this point 
Solved Threads: 13
llemes4011 llemes4011 is offline Offline
Posting Whiz in Training

Re: JComponent Placement

 
0
  #7
Jul 5th, 2009
I tried that and it didn't resize it. ( I tried it as a JPanel too...)
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,631
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 470
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: JComponent Placement

 
0
  #8
Jul 5th, 2009
Please post complete code with bb code tags.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 207
Reputation: llemes4011 is an unknown quantity at this point 
Solved Threads: 13
llemes4011 llemes4011 is offline Offline
Posting Whiz in Training

Re: JComponent Placement

 
0
  #9
Jul 5th, 2009
Okay, Here's what I have:

  1. package JEmail.util;
  2.  
  3. import java.awt.*;
  4. import javax.swing.*;
  5.  
  6. public class ContentViewer extends JPanel
  7. {
  8. public ContentViewer(){
  9. createAndShowGUI();
  10. }
  11.  
  12. public void createAndShowGUI(){
  13.  
  14. WindowComponents.vertSplitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
  15. WindowComponents.horiSplitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
  16. WindowComponents.files = new JTree();
  17. WindowComponents.tabs = new JTabbedPane();
  18. WindowComponents.display = new JPanel();
  19. WindowComponents.tabs.add(new InboxTab(), "Inbox");
  20. WindowComponents.tabs.add(new ContactsTab(), "Contacts");
  21. WindowComponents.tabs.add(new NotesTab(), "Notes");
  22.  
  23. WindowComponents.vertSplitPane.setTopComponent(WindowComponents.tabs);
  24. WindowComponents.vertSplitPane.setBottomComponent(WindowComponents.display);
  25. WindowComponents.vertSplitPane.setDividerLocation(.5);
  26. WindowComponents.horiSplitPane.setLeftComponent(WindowComponents.files);
  27. WindowComponents.horiSplitPane.setRightComponent(WindowComponents.vertSplitPane);
  28.  
  29. this.add(WindowComponents.horiSplitPane);
  30.  
  31. }
  32. }

  1. package JEmail;
  2. import JEmail.util.*;
  3. import javax.swing.*;
  4. import java.awt.*;
  5. /**
  6.  * Write a description of class JMail_Runner here.
  7.  *
  8.  * @author (your name)
  9.  * @version (a version number or a date)
  10.  */
  11. public class JMail_Runner{
  12.  
  13. private static final int PWIDTH = 600;
  14. private static final int PHEIGHT = 500;
  15.  
  16. JFrame jMailWindow;
  17. JEmailMenuBar menuBar;
  18. ContentViewer content;
  19. FrameResizeHandler frh;
  20.  
  21. Insets windowInsets;
  22.  
  23. public JMail_Runner(){
  24. createAndShowGUI();
  25. }
  26.  
  27. public void createAndShowGUI(){
  28. try{ // Set the L&F to match the System - will have option to change later.
  29. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  30. }catch(Exception e){
  31. e.printStackTrace();
  32. }
  33.  
  34. jMailWindow = new JFrame("JEmail");
  35. jMailWindow.setPreferredSize(new Dimension(PWIDTH, PHEIGHT));
  36. jMailWindow.setLocation(150,100);
  37. jMailWindow.setSize(PWIDTH, PHEIGHT);
  38.  
  39. Container pane = jMailWindow.getContentPane();
  40. pane.setLayout(new BorderLayout());
  41.  
  42. menuBar = new JEmailMenuBar();
  43. content = new ContentViewer();
  44.  
  45. // The MenuBar stretches fine... just not the content object
  46. pane.add(menuBar, BorderLayout.NORTH);
  47. pane.validate();
  48. pane.add(content, BorderLayout.CENTER);
  49. pane.validate();
  50.  
  51. jMailWindow.setVisible(true);
  52. }
  53.  
  54. public static void main(String[] args) {
  55. //Schedule a job for the event-dispatching thread:
  56. //creating and showing this application's GUI.
  57. javax.swing.SwingUtilities.invokeLater(new Runnable() {
  58. public void run() {
  59. new JMail_Runner();
  60. }
  61. });
  62. }
  63. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,631
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 470
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: JComponent Placement

 
0
  #10
Jul 5th, 2009
As per your post I did't find any ambiguity in your code. I have added JMenuBar and ContentView and some components into ContentView - There is no problem.

I am not sure about JEmailMenuBar class. If it is JMenuBar then use setJMenuBar method of JFrame.
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC