-4

I'm having two different issues during working with GUI. The first one is: how can i get container components? I want to get 5 components from BorderLayout (and prefer to get them with knowledge of there placement in the layout, e.g : CENER,NORTH,SOUTH,EAST,WEST The second one is: I want to "calculate" gui layout, while container is resized and not only when at the moment it have stooped resizing. How can i do it?

For the first issue, i tried to use getComponenets() method, but it returns many components without any information on the component place in layout. For the second issue, when i am implementing the method componentResized() , the event is fired only when resize process end, but i want to calculate layout for each change in the container

    public class GridLayoutFrame extends JFrame  
{
    LabelAndJtextAreaPanel topPanel;
    //KeyboardbuttonPanel buttomPanel ;

    public GridLayoutFrame ()
    {
        super("Typing Application");
        setLayout(new GridLayout());
        topPanel = new LabelAndJtextAreaPanel();
        add(topPanel);

        addComponentListener(new ComponentAdapter() {
            public void componentResized(ComponentEvent e) {
                System.out.println("componentResized\n");
                // want to put here many calculation during JFrame is resized
            }
        }
        );
        */
        setSize(300,200);
        setVisible(true);

    }

    public static void main(String[] args)
    {
        GridLayoutFrame frame = new GridLayoutFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}
Eitanos30
  • 1,331
  • 11
  • 19
  • Can you post your code? – omoshiroiii Aug 02 '19 at 21:36
  • *when i am implementing the method componentResized() , the event is fired only when resize process end,* - I get multiple events. Post your [mre] demonstrating the problem. – camickr Aug 02 '19 at 21:42
  • i added a slice from the code... I want that for every change in JFrame i will calculate JButtons that are inside JPanel with FlowLayout. i'm trying to build application with gui that demonstrate computer keyboard. and since in each row there are different numbers of keys, and some of them are in different size i decided to use FlowLayout for each "row" in keyboard. i want that the key buttons will enlarge/decrease according to JFrame change of size. Isn't a way to listen to JFrame size change during the size change and not only at the moment the changed ends? – Eitanos30 Aug 02 '19 at 22:57
  • That is NOT an MRE. The code doesn't event compile so how can you test it? For me using JDK8 on Windows 7 I constantly have the componentResized(...) method invoked as I resize the frame. – camickr Aug 02 '19 at 23:11
  • @camick, it works only when resizing is finished, but during resized nothing happens! i want the while standing of the JFrame edge, pressing the left button on mouse (without releasing) and moving the mouse, i will able to make calculation for each move, and not only when releasing the mouse! – Eitanos30 Aug 02 '19 at 23:15
  • 1) It seems illogical that the program sould 'need' to 'get' those components given it was what put them there in the first place! Keep a reference to them. 2) The question text mentions `BorderLayout` the code uses `GridLayout` and one of you comments mentions `FlowLayout`. Which is it? 3) *"I'm having two different issues"* SO Q&As should focus on a single question at a time. By doing so, a helper who can solve the 'one question' (but perhaps not others) is encouraged to provide one answer.Please [edit] to focus on one, & move the other to a new thread with [mre[ focusing on each, – Andrew Thompson Aug 03 '19 at 01:24
  • (1-) @Eitanos30 1) how do you know it doesn't work, the posted code doesn't even compile. Unless you can post code that compiles, you haven't tested the code. 2) I already told you it DOES work for me. So maybe it is a version/platform issue. Or maybe there is something else in your code causing the problem. If you don't post and proper [mre] and state your version/platform, then others can't test your code. 3) you have been given answer to your first question, so don't forget to "accept" the answer. – camickr Aug 03 '19 at 03:52

1 Answers1

1

I want to get 5 components from BorderLayout (and prefer to get them with knowledge of there placement in the layout, e.g : CENER,NORTH,SOUTH,EAST,WEST

Well, only the BorderLayout knows what constraints you have used. So that information would be found in the BorderLayout API. Look at the various getter methods.

You would use:

BorderLayout layout = (BorderLayout)panel.getLayout();

to access the layout manager.

Then you would request the component for each of the constraints using the appropriate method from the API.

i'm trying to build application with gui that demonstrate computer keyboard. and since in each row there are different numbers of keys, and some of them are in different size i decided to use FlowLayout for each "row" in keyboard

A better solution is to let a layout manager do the resizing.

Take a look at: Why does this GridBagLayout not appear as planned? for an example.

You would just need to add:

gbc.weightx = 1.0f;

to the example to allow for dynamic horizontal resizing.

Edit:

Attempting to answer questions related to the other link.

Look at the picture in the other link. You will see each row contains 5 to 8 buttons, which means the GridBagLayout only know about the size of 8 individual columns.

Add up the numbers in the (..) which indicate the gridwidth for each row. The total is 22.

The GridBagLayout doesn't know how to turn 8 columns into 22 columns. It doesn't know what widths to use for each column.

So the suggested solution is to add 22 dummy components in a single row. Now each column will have a width so specifying"gridwidth" will now mean something to the GridBagLayout.

In the code you will see I tried to assign the dummy buttons to gridy=4, but the code was wrong:

ui.add(new JButton()); //wrong
//ui.add(new JButton(), gbc); // what I should have had

Now the default behaviour is you don't specify a GridBagConstraint, is to just display each component on the first row (ie. gridy=0).

So the problem is now you have two rows of buttons that are trying to be displayed at gridy=0, which is obviously wrong.

At the time I didn't realize I forget to specify the "gbc" for the dummy buttons so I just adjusted the gridy for each row of buttons which is why you see code like:

//gbc.gridy = 0;
gbc.gridy = 1;

I just shifted each row of the keyboard buttons down 1, so the dummy buttons can display at gridy=0.

If you want to see the buttons at the bottom change the code as follows:

//gbc.gridy = 4;
//ui.add(new JButton());
gbc.gridy = 5;
ui.add(new JButton(), gbc);

Now components will exist for gridy=1, 2, 3, 4, 5. There are no components in gridy=0.

Somehow GridBagLayout "lerans" cell size from the "for" loop

Yes, because each button has its own size. Since 22 buttons were added on a single row the GridBagLayout knows the minimum width of all 22 columns.

So now if you add a button at gridx=1, with a gridwidth=2, the width of the button is simply the width of column 1 and column2, which in this case is simply the width of the dummy button in each column.

However, the "edited" solution is even better because you don't need the dummy button to determine the width of each of the 22 columns.

The size of the array defines the number of columns.

The value assigned to each entry in the array is the "minimum width" of the column (it can be different for each column).

So now, even though you don't have the dummy components, the GridBagLayout knows how to calculate a width for each column and using "gridwidth" will work as expected.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • i read the example you sent a link for. It seems that it can help me. But i have some question about the example: 1.How can i allow dynamic vertical resizing that will keep the buttons proportion (gbc.weighty=1.0f)? 2. I read the java api, but i didn't understand in which scale variable gridx and gridy are counted 3. I tried to understand from the api, the meaning of the gridwidth and gridheight attributes but i didn't understand the explanation at all, maybe because i don't understand the meaning of "component's display area" which is used there. I will be glad to understand the meaning? – Eitanos30 Aug 04 '19 at 19:54
  • I have difficult to understand the code and i don't want to bother anymore so thanks and i will approved your answer – Eitanos30 Aug 04 '19 at 21:29
  • Read the Swing tutorial on [How to Use GridBagLayout](https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html#gridbag) for more information about the constraints and working examples. And don't forget to "accept" the answer by clicking on the checkmark, since your original question was answered. – camickr Aug 04 '19 at 23:51
  • Can you please explain why gridy in the example starts from 1 and not from 0? in addition the for loop, it doesn’t override the forth row button? I copy the code and ran the example and I got first button line (small ones), should they be there? In addition JPanel border doesn’t exist at all and not as it is shown in the print screen.. can you guess why? – Eitanos30 Aug 05 '19 at 12:09
  • Ok, i understand. But in all the discussion in the link, i didn't find a reference that explain why in the first code you post, you get the keyboard that the keboards doesn't fit the size you asked for. e.g: in the first row all keybards are at the same size, and only the button that is before the last one is bigger than the others? Is threr logical explantion or it is Layout Manager unpredictable behviour? What does the "for" loop or alternatively code you suggested, that makes the keyboards in the correct size? – Eitanos30 Aug 06 '19 at 12:06
  • ,I asked you about things that seems not clear between the first code (that is part of your first post) and the screenshot you added there, and according to code it's not clear why button (beside from the button on the first line) didn't get the size you set in "gridwidth". The reason why it's is painting the "dummy" button in line one i already understood. I didn't ask you in the last comment nothing about the new code! Unfortunately,it seems All you are care about is the "accept', so you got it. The reason why i didn't response, can be found in the post you have insulting me again. – Eitanos30 Aug 11 '19 at 18:11
  • I'm taking your O-R-I-G-N-A-L code (the one you started the post with!!!) as his. i'm getting the same paint as you got in the screenshot you attach in the same first post! In these code, there aren't any dummies buttons. To these code you added also (second post) a "for" loop, it seems that this code fixed the button size issue (the issue that your start the post for). So i asked you (implicitly) if the "for" loop is the fix for the situation in the beginning when JButton was paint while ignoring _gridwidth_ value. Anyway thanks again, i won't ask nothing more because you find me unclear. – Eitanos30 Aug 11 '19 at 20:40
  • @Eitanos30 1) I haven't posted a screenshot of the frame in any of these postings 2) the original code I posted in my anwer in the other link shows the buttons at their preferred "gridwidth", which is why I posted the answer. 3) I added the code following the "Edit" because I recently found a way to improve the code without using dummy components. and the buttons are still displayed at the proper "gridwidth". – camickr Aug 11 '19 at 21:53
  • @camickr.I am talking on the other link in all of my comments.Sorry for confuse .All the latest comments are related to problem to understand your original code. Didn’t you post him because you get buttons that aren’t at the size you wished when you used the gridwidth? All I tried to understand if you realize why on the first solution you suggested (where by mistake you forgot sending ‘gbc‘ variable to add method),adding the “dummy” buttons makes it all right?In other word, how does this workaround works?I can promise that before asking you,I run debug ten times but it didn’t help me – Eitanos30 Aug 11 '19 at 22:30
  • It seems like a "magic". Somehow GridBagLayout "lerans" cell size from the "for" loop. – Eitanos30 Aug 11 '19 at 22:39
  • @Eitanos30 *All the latest comments are related to problem to understand your original code* - and that is why I asked you to "accept" the answer because these question are not related to your original question. People in the forum should know that the original suggestion solved the original problem. See edit for more info about the original code. – camickr Aug 12 '19 at 00:32
  • Additional explanation about the code in the link is super. I hope people will find it useful as I did – Eitanos30 Aug 12 '19 at 06:58