1

I am trying to make a 2x2 grid layout that has a JLabel on the top left, and three buttons on the other three spaces. When I do this, I get the unexpected result of one big button (filling up the entire JDialog) that says "Do you want to push me". I don't know why this result shows up, please help, Thanks!

    public void sinceyoupressedthecoolbutton() {

        JDialog replacementwindow = new JDialog(); //Like a window
        JButton best = new JButton("best");
        JButton first = new JButton("FIRST");
        JButton second = new JButton("Second");
        replacementwindow.setLayout(new GridLayout(2,3,0,0)); //Row, column, distance horizontally, distance vertical
        JPanel panel = new JPanel();
        replacementwindow.add(panel); //adding the JPanel itself
        replacementwindow.add(first);
        replacementwindow.add(second);
        replacementwindow.add(best);
        replacementwindow.setSize(500, 500);
        replacementwindow.setTitle("NEW WINDOW!");
        replacementwindow.setVisible(true);

    }
  • No your edited code is NOT what I suggested. You add the components to the "panel" and then you add the components to the "replacementWindow". Don't add the components directly to the dialog. – camickr Aug 19 '15 at 22:20
  • After trying your method, the grid look was lost..? When I added the components to the panel it looked like multiple buttons just lined up. Why was that? –  Aug 19 '15 at 22:26
  • 1
    `it looked like multiple buttons just lined up` - because you didn't set the layout manager to be the GridLayout. The default layout manager for a JPanel is a FlowLayout. Last answer, you need to take time to read the tutorial to learn some basics. Ask me a question about something you read and I will answer but I'm not here to teach you the basics, that what the examples from the tutorial are for. You can even look at the `How to Use a GridLayout` demo from the tutorial for working code. Learn proper techniques from the tutorials. – camickr Aug 19 '15 at 22:29
  • Alright, I really do appreciate the time and effort you put into giving me your detailed explanations. If I have any questions after reading your suggested tutorials, I'll be sure to let you know! Thank you, I hit like on your answer! –  Aug 19 '15 at 22:33

2 Answers2

2

Don't add components to a button. You add components to a panel.

So the basic code should be:

JDialog dialog = new JDialog(...);
JPanel panel = new JPanel( new GridLayout(...) );
panel.add(label);
panel.add(button1);
...
dialog.add(panel);

Also, variable names should NOT start with an upper case character! "Yes" does not follow Java standards. The other variables do. Be consistent!

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Is this because the JPanel is within the JDialog? And if so, shouldn't it work even either way, if I add it to the JDialog or the JPanel? –  Aug 19 '15 at 22:08
  • When you add components to the "dialog" you add the components to the "content pane" of the dialog which is a JPanel. It is better to use the approach I showed above or use `dialog.setContentPane(panel)` instead of adding comopnents directly to the dialog. Read the Swing tutorial on [Using Top Level Containers](http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html) for more information. The tutorial talks about a JFrame but the structure is the same for a JDialog. – camickr Aug 19 '15 at 22:12
  • `shouldn't it work even either way, if I add it to the JDialog or the JPanel?` - it will work but it is not the way experienced programmer would do it. The components belong to the panel, not the dialog, so add the components to the panel. `Read the tutorial and download other examples to learn how to better structure your code!` – camickr Aug 19 '15 at 22:23
  • A quote from the tutorial link I gave you: `This means that if you want to take advantage of the content pane's JComponent features, you need to either typecast the return value or create your own component to be the content pane. Our examples generally take the second approach, since it's a little cleaner.` – camickr Aug 19 '15 at 22:25
2

It's because you set the layout of your JButton, and not of your JDialog

Change

label.setLayout(new GridLayout(2,2,0,0));

to

YES.setLayout(new GridLayout(2,2,0,0));

Also, your variable called label is a JButton, you probably want to change that.

Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35
  • Than you so much, now I got the basic grid going! I am still kind of confused on this line though: [code] JPanel frame = new JPanel(); YES.add(frame); //adding the frame itself [code] What is the point of the JPanel? –  Aug 19 '15 at 22:12
  • 1
    @Billybobsteven `I am still kind of confused ` - because your variable names are terrible. Your "frame" is not a frame it is a JPanel. Most people would think it was a JFrame. That is why I gave you some (slightly) better variable names to use. Even my name should be more descriptive, but I have no idea what your application is doing so I can't suggest more descriptive names. – camickr Aug 19 '15 at 22:14
  • @Billybobsteven I will not repeat what camickr already said. But the "point" of the JPanel is what you want to do with it... So you probably want to add the JLabel in it, right? Even though you don't need an extra JPanel for that... – Lukas Rotter Aug 19 '15 at 22:18
  • I'm extremely sorry for the variable name confusion, but I have edited my question with more descriptive variable names. Do you mind explaining two things to me? 1) Why we need to add the JPanel to the JDialog, and 2) Why is the output doing nothing about the top left square in the grid? –  Aug 19 '15 at 22:20
  • @Billybobsteven 1) Pretty much got answered in the comments of the other answer. 2) What do you mean by that? You're adding a blank JPanel to the top left, so obviously nothing will be displayed there. – Lukas Rotter Aug 19 '15 at 22:25
  • You're right! That is what I was confused about, and when I deleted the JPanel it wen't away. Now I decided to fill the bottom right part of the grid layout with a JLabel, and it works fine, but I was wondering how I could center the JLabel within that portion of the JLabel itself. Do you know how? –  Aug 19 '15 at 22:31
  • @Billybobsteven `yourLabel.setHorizontalAlignment(JLabel.CENTER);` – Lukas Rotter Aug 19 '15 at 22:34
  • Thank you so much! I have marked you as best answer, and hit the like button. It's great people like you who help us beginners learn so much faster! Thanks, Billybobsteven –  Aug 19 '15 at 22:35