1

I am maintaining a system which requires me to make components in the dialog resizable, the dialog box calls out a java class Panel.

What is supposed to happen: enter image description here

What is currently happening: enter image description here

Note: The image on the bottom layer represents the resized one. While the image at the top layer is the dialog box which is not yet resized.

As you can see, the component JPanel(the one with the black border) is not resized. I am trying to achieve what happend to the bottom layer image of the first attachment.

I tried to apply the answer in How to dynamically control auto-resize components in Java Swing and pattern it in current code but since my panel is only called in a dialog box so there are limitations. The problem is that the components and its hierarchy have been already implemented, I just have to make it auto-resize.

Here is my current outline:

enter image description here

gengencera
  • 426
  • 2
  • 15
  • 1
    I am sorry, but it is unclear what issue you face based on the images above. Is it a problem with the top window or the bottom one? Is it possible you could describe the issue you are having either with text or a video/animated image? – Devon Rutledge May 02 '18 at 04:42
  • I am sorry @Devon Rutledge. I have edited the question, thanks for the comment. The image on the bottom layer represents the resized one. While the image at the top layer is the dialog box which is not yet resized. – gengencera May 02 '18 at 04:53
  • Components will automatically resize if you use the appropriate layout managers. Post a proper [mcve] demonstrating your problem. – camickr May 02 '18 at 15:02
  • @camickr. I have updated the question. I included the current structure. I have tried other Layouts (Absolute,Flow,Grid,Card,Box) – gengencera May 07 '18 at 07:56

1 Answers1

0

If you want a simple solution you can use a layout manager as described here. Or, if you wish to avoid a layout manager(like me) then you can have your program resize your elements every time there is a resize event. Here is some sudo-code

frameOrPanel.addComponentListener(new ComponentAdapter() {
   public void componentResized(ComponentEvent componentEvent) {
    element.setLocation(frameOrPanel.getWidth()*1/4, frameOrPanel.getHeight*1/4);
    element.setSize(frameOrPanel.getWidth()*1/2, frameOrPanel.getHeight()*1/2);
   }
});

You will have to add unimplemented methods.

Note: the 1/4 and 1/2 is merely a ratio you can change those to fit your application.

Devon Rutledge
  • 143
  • 1
  • 11
  • Thanks for the answer, I applied it already and it works but Is it okay that I compute location and width/height of every component during resize? – gengencera May 07 '18 at 08:46
  • 1
    Considering you shouldn't be resizing that often it is a very small amount of compute power. – Devon Rutledge May 07 '18 at 13:08
  • (1-) `if you wish to avoid a layout manager(like me)` - yes, well that is the wrong approach. You are reinventing the wheel. Swing was designed to be used with layout managers. – camickr May 07 '18 at 14:15
  • 1
    The link he gave already explained layout managers. I was specifically explaining how to implement a specific method for resizing. This doesn't deserve a down vote because it answers the question as it is supposed to be answered. – Devon Rutledge May 07 '18 at 14:27
  • 1
    @camickr, I removed the listener temporarily for componentresized. As you can see I updated the question, I used Layout Manager (panel with BorderLayout) but still it does not work. I also stated this that the answers in the link I provided does not work for me. I have demonstrated my problem well because as of Devon's answer, he understood it and answered and it worked. If you have any approach that will work you are free to answer. – gengencera May 08 '18 at 02:25
  • @gengencera, No you have not updated the question. You refuse to post your code so we can't help. The simple answer is to use layout managers properly. If you are not interested in learning that is your choice. I can't force you to learn how to code Swing properly. – camickr May 08 '18 at 04:17
  • `because it answers the question as it is supposed to be answered.` - no it doesn't answer the question. The proper answer (as you already stated in your answer) is to use Layout Managers effectively. There is absolutely no reason to use a ComponentListener and resize the component manually. That is the job of the layout manager plain and simple. I will continue to down vote answers that teach people to reinvent the wheel when that is already a well developed API that will solve the problem. – camickr May 08 '18 at 04:25
  • 1
    To be clear for the link he gave the accepted answer was one that used a resize listener I was helping him/her implement what they wanted to implement. Although I suggested they use a layout manager because that is the best solution. But, in order to truly answer the question being asked I had to give a simple example of how to solve the issue they were having with the solution they were implementing. And as pointed out it doesn't help to just complain. If you wish for people to do something better you have to show them how. – Devon Rutledge May 08 '18 at 04:51
  • @camickr Have you not seen that I used Layout manager but still it is not working? I posted the current structure for you to see. Not all questions are required to have a code posted. As stated in the question I have already used the answer provided in the link (which suggests using Layout manager) but it didn't work. On what part can you not comprehend that what you're suggesting is not working and Devon provided a solution which is working? – gengencera May 08 '18 at 06:10
  • @gengencera, No I have not seen that you are using a layout manager because you have NOT POSTED ANY CODE!!! Posting a picture of your IDE property page doesn't tell us anything about the context of how the code is used. It takes about 10 lines of code to create a proper [mcve]. You created JPanel and set its background color to RED. Then you create a JFrame and add the panel to the frame. Then as you resize the frame the size of the panel will change. I can't guess what more help you need until you post actual code demonstrating your problem. – camickr May 08 '18 at 14:23
  • `in order to truly answer the question being asked...` - the question being is how to resize the components when the dialog is resized. The answer is to use layout managers. Until the OP provides a reasonable question with code showing the problem we can't suggest which layout manager (or combination of layout managers) to use. Using a ComponentListener may appear to solve the problem but it will lead to issues down the road is the OP continues to design applications like this. The point of the forum is to give answers to solve the problem now and prevent problems in the future. – camickr May 08 '18 at 14:31
  • `If you wish for people to do something better you have to show them how.` - that is NOT how the forum works. The OP is asking for help. The OP has been give a suggestion. The OP claims the suggestion doesn't work. So it is up to the OP to post the code showing they made an EFFORT to solve the problem. Once they do this, then we can offer specific advice. Your solution uses setSize() and setLocation() which implies the OP is using a null layout which is the opposite of using a layout manager. If the OP wants better help they need to provide 1) a better question 2) code showing the effort. – camickr May 08 '18 at 15:00
  • Two things 1. If you think this is a big issue you should move it to meta. 2. There is nothing wrong with my answer, I understand that the question leaves much to be desired but my answer is the accepted answer, so it does answer the question being asked as well as suggesting better coding practice by first suggesting a layout. – Devon Rutledge May 08 '18 at 15:23
  • Then as you resize the frame the size of the panel will change WRONG! If that would just be it then I don't have a problem at the first place. Please read again the context carefully. I'll say it again though, I am only maintaining this system. Therefore, It was designed like this, I have limitations. I used Layout manager as you can see. Codes are client's property and not mine to post. Also, you don't have to guess what help I needed more because I have already accepted an answer which helped me already. – gengencera May 09 '18 at 04:53