1

In my code, My okButton is in bad appear, so large and long, How fix this problem?

public class d7Table extends JFrame {

public JTable table;
public JButton okButton;

public d7Table() {

        table = new JTable(myTableModel(res));
        okButton = new JButton("Ok");

    add(new JScrollPane(table), BorderLayout.CENTER);
    add(okButton, BorderLayout.PAGE_END);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(800, 600);
    this.setLocation(300, 60);
    this.setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new d7Table();
        }
    });
}
}

I remove Irrelevant codes. enter image description here

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Sajad
  • 2,273
  • 11
  • 49
  • 92
  • Use `okButton.setPreferredSize()` – Elist Jul 21 '13 at 21:34
  • @Elist i add `okButton.setPreferredSize(new Dimension(20, 30));` But not effect – Sajad Jul 21 '13 at 21:35
  • 3
    @Elist Using setPreferredSize is a seriously bad idea, you've not taken into account in factors governing the how the size of the buttons calculated, besides which, the button is under the control of a layout manager that will ignore the preferred size of its components – MadProgrammer Jul 21 '13 at 21:38
  • 1
    My mistake. As others have said, that's how BorderLayout behave. – Elist Jul 21 '13 at 21:38

3 Answers3

5

You've added the button to the SOUTH position of a BorderLayout. This is the default behaviour of BorderLayout.

To fix it, create another JPanel, add your button to it, then add the panel to the SOUTH position instead

Take a look at

The approach mentioned above is commonly known as compound layouts, as you use a series of containers with different layout managers to achieve the desired effect.

JPanel buttonPane = new JPanel(); // FlowLayout by default
JButton okayButton = new JButton("Ok");
buttonPanel.add(okayButton);
add(okayButton, BorderLayout.SOUTH);
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Can you explain more?! – Sajad Jul 21 '13 at 21:38
  • 1
    @Sajjad: you will want to read the layout manager tutorial as it is all stated quite well there. 1+ for the answer. – Hovercraft Full Of Eels Jul 21 '13 at 21:39
  • 2
    @Sajjad: regarding `" I'm trying to learn through trial and error and unfortunately I do not have the patience to study from first! "` -- If you truly don't have the time or patience to "study from first", then you will fail to learn Java and are just wasting your and our time asking questions here. Please prove me wrong and show that you are willing to put in the required time and effort. -1 to your question for this terrible comment and reflection of your work ethic. – Hovercraft Full Of Eels Jul 21 '13 at 21:49
  • @HovercraftFullOfEels I've read all of this in the past, and now i just forgot them! – Sajad Jul 21 '13 at 21:52
  • @HovercraftFullOfEels My mean was that I study all of this at past, But not forgot it's details, Sorry for bad comment! – Sajad Jul 21 '13 at 21:58
  • @Sajjad: you were given the link to the [Layout Manager Tutorial](http://docs.oracle.com/javase/tutorial/uiswing/layout/index.html) in [my answer to your question on July 17th](http://stackoverflow.com/a/17710660/522444). If you had read it then, you wouldn't even be asking this question. Again, please stop wasting our time and please read or re-read the tutorial. – Hovercraft Full Of Eels Jul 21 '13 at 22:27
  • 1
    I remember when I started swing last year, I was just things with the matter of luck without studying, this was worked until I create my first project, unfortunately, I couldn't finish it because in some step my codes and classes were like a jungle impossible to understand what I did. @Sajjad: I tell you that story of mine for not go through this way, it was a disaster made me leave Java. – Azad Jul 22 '13 at 02:28
  • @HovercraftFullOfEels Thanks for attention, I try to re-read the tutorial. – Sajad Jul 22 '13 at 15:15
  • 2
    @Azad I sincerely thank you for your guidance, My Muslim brother. – Sajad Jul 22 '13 at 15:19
4

Because the default layout of JFrame is BorderLayout, and PAGE_END means the bottom of the frame horizontally like this:

enter image description here

You have to change the layout of the frame, but don't do that, just create a panel, and add the components to it then add the panel to the container.

JPanel p = new JPanel();
p.add(okButton);
add(p,BorderLayout.PAGE_END);

Here some links may help you understand more about layout managers that usually used:

Azad
  • 5,047
  • 20
  • 38
3
import java.awt.*;
import javax.swing.*;

public class TableAndButton extends JFrame {

public JTable table;
public JButton okButton;

public TableAndButton() {
    table = new JTable();
    okButton = new JButton("Ok");

    add(new JScrollPane(table), BorderLayout.CENTER);
    JPanel bottomPanel = new JPanel();
    bottomPanel.add(okButton);
    add(bottomPanel, BorderLayout.PAGE_END);

    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    //this.setSize(800, 600);  better to call pack()
    this.pack();
    //this.setLocation(300, 60);  better to..
    this.setLocationByPlatform(true);
    this.setVisible(true);
}

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            new TableAndButton();
        }
    });
}
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433