0

I am developing a GUI using GridBagLayout for a panel which contains several other panels. One of the panels contain only a single button. I want this to appear horizontally following the upper two panels and not occupy height more than the button's height.

But somehow my grids are divided in rows of equal heights and the button row occupies almost ten times more height than itself.

Being a newbie in Swing, I am not able to fix this. Please suggest me the solution to this problem.

Following is the source code:

class SplitPane extends JFrame  {

private static JPanel panel2;
private static JPanel panel5;
private static JScrollPane panel3;
private static JScrollPane panel4;
protected JSplitPane split;

public SplitPane(JChemPaintPanel p){

    JFrame f = new JFrame("");
    //f.setLayout(new GridLayout(3, 2, 10, 10 ));
    // f.addWindowListener(new JChemPaintPanel.AppCloser());
     //  f.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setPreferredSize(new Dimension(800, 800));
    JPanel pane = new JPanel(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    c.gridx=0;
    c.gridy=0;
    c.fill=GridBagConstraints.BOTH;
    c.weightx=1;
    c.weighty=1;
    c.gridheight=1;
    c.gridwidth=1;
    p.setPreferredSize( new Dimension( 100, 100 ) );
    p.setMaximumSize(new Dimension(100, 100));
    pane.add(p, c);

    c.gridx=1;
    c.gridy=0;
    c.fill=GridBagConstraints.BOTH;
    c.weightx=1;
    c.weighty=1;
    c.gridheight=1;
    c.gridwidth=1;
    pane.add(createPanel2(), c);

    c.gridx=0;
    c.gridy=1;
    c.gridheight=1;
    c.weightx=1;
    c.weighty=1;
    c.gridwidth=GridBagConstraints.REMAINDER;
    c.anchor=GridBagConstraints.CENTER;
    c.fill=GridBagConstraints.HORIZONTAL;
    pane.add(createPanel5(), c);

    c.gridx=0;
    c.gridy=2;
    c.gridheight=1;
    c.gridwidth=1;
    c.fill=GridBagConstraints.BOTH;
    c.weightx=1;
   c.weighty=1;
    pane.add(createPanel3(), c);

    c.gridx=1;
    c.gridy=2;
    c.gridheight=1;
    c.gridwidth=1;
    c.fill=GridBagConstraints.BOTH;
    c.weightx=1;
   c.weighty=1;
    pane.add(createPanel4(), c);

    f.add(pane);
    f.pack();
    Point point = GraphicsEnvironment.getLocalGraphicsEnvironment().getCenterPoint();
     import java.awt.BorderLayout;
    int w2 = 1000;
    int h2 = 1000;
    f.setLocation(point.x - w2, point.y - h2);
    f.setVisible(true);
}

CreatePanel2 - Is a simple text editor CreatePanel3 - Has a scrollable table CreatePanel4 - Another scrollable table CreatePanel5 - Panel having a single button

pas
  • 109
  • 1
  • 13
  • 2
    1) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. 2) See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Feb 02 '15 at 18:37
  • BTW - `f.setLocation(point.x - w2, point.y - h2);` is equivalent to `f.setLocationRelativeTo(null);`, but `f.setLocationByPlatform(true);` is a better place to put the GUI. – Andrew Thompson Feb 02 '15 at 18:40
  • Perhaps I'm just a noob but why would you say blank lines are redundant? Shouldn't we want things to be separated and easy to see so that maintenance would easier? @AndrewThompson – DreadHeadedDeveloper Feb 02 '15 at 18:42
  • *"easy to see"* You think adding useless white space makes things 'easier to see'? Go figure.. – Andrew Thompson Feb 02 '15 at 18:51
  • @AndrewThompson Ok, but separated blocks of code with a newline helps signify differences and helps group relevant code together. How is that a bad thing? – DreadHeadedDeveloper Feb 02 '15 at 19:07
  • @AndrewThompson Isn't that the blank lines, though may be redundant, are the part of writing clean, efficient and readable code? Sometimes professional way of coding. And sometimes also are the part of project rules of writing code. – pas Feb 02 '15 at 19:09
  • I totally agree with @DreadHeadedDeveloper – pas Feb 02 '15 at 19:09
  • Anyways, can we find a solution for my problem? – pas Feb 02 '15 at 19:11
  • *"..separated blocks of code with a newline.."* I repeat *"A **single** blank line of white space in source code is all that is ever needed."* **Two or more** is just wasted space. – Andrew Thompson Feb 02 '15 at 19:11
  • 1
    *"..can we find a solution for my problem?"* For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). That code would only require imports and a main method to make it an MCVE. – Andrew Thompson Feb 02 '15 at 19:13
  • @DreadHeadedDeveloper *"..where does the OP use 2 or more?"* Right [here](http://stackoverflow.com/posts/28284405/revisions).. – Andrew Thompson Feb 02 '15 at 19:15
  • I caught it right after i pressed enter so i deleted the comment, again, my apologies – DreadHeadedDeveloper Feb 02 '15 at 19:16
  • Which constraint of GridBagConstraints do I need to change to make the height of a specific row smaller than the others? – pas Feb 02 '15 at 19:25
  • So I removed all the setPreferredSize. The applet appears perfect when I launch it first time. But now the size of the panels is changing even if I am trying to write in the text editor part. The size changes randomly and disturbs the entire design. Any guesses why this might be happening? – pas Feb 02 '15 at 20:14
  • Wrap the textArea in a scrollpane – MadProgrammer Feb 02 '15 at 20:55
  • @MadProgrammer scrollpane contains a scrollable table – pas Feb 02 '15 at 23:27
  • *"But now the size of the panels is changing even if I am trying to write in the text editor part"* - Suggests that your text editor isn't – MadProgrammer Feb 02 '15 at 23:49
  • 2
    Your question is at the moment incomplete. For example what do `createPanel3`, `createPanel5` etc. do? Which panel is the panel with the button? Please reduce your program to a [minimal but complete example](http://stackoverflow.com/help/mcve) that reproduces the problem you are seeing. With layout questions it's also best to include an image that illustrates how you want the layout to appear to avoid confusion. – Radiodef Feb 03 '15 at 01:14
  • @Radiodef Sorry for the inconvenience caused. I will try to edit the question but cannot post the image as I am not having enough reputation. – pas Feb 03 '15 at 16:22

0 Answers0