Question

I have a TabbedPane inside of which there are Panels. I am dealing now with the first panel. Inside this panel there should be another small panel on the left(I just placed a textfield instead for reference) a textarea in the center and below it a row of buttons. But the buttons are placed far too low below from the textarea. Besides, if I resize the frame, the textarea just goes away, vanishes. The textfield is set ok, top left, the textarea more or less yes in the center, but it should not go away when resizing (it would make a weird effect for the user).

I have tried all variants south, west, whatever, no changes or it goes up too far anyways.

I have read somewhat similar questions but I dont see how their answers solve my case.

So, here is the code:

super("Proyecto");
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     this.setSize(1200, 800);

/* 1. CREATE PANEL*/

 /*MAIN PANEL BOOK READER*/
 JPanel panelbookreader = new JPanel();

 /*CREATE TEXTAREA*/
 JTextArea areatextobookreader = new JTextArea(20, 80);
 /* 2. ADD THAT COMPONENT TO ITS PARENT*/

 panelbookreader.add(areatextobookreader);

 /* 3. SET A LAYOUT FOR THE PANEL THAT WILL ORGANIZE COMPONENTS INSIDE*/
 panelbookreader.setLayout(new GridBagLayout());


 GridBagConstraints gbc = new GridBagConstraints();

 JTextField hold = new JTextField(20);
 gbc.gridx = 0;
 gbc.gridy = 0;
 gbc.gridwidth = 1;
 gbc.gridheight = 1;
 panelbookreader.add(hold, gbc);

 //TEXT AREA
 gbc.gridx = 1;
 gbc.gridy = 1;
 gbc.gridwidth = 0;
 gbc.gridheight = 1;
 gbc.fill = GridBagConstraints.CENTER;
 gbc.weightx = 1.0;
 gbc.weighty = 1.0;
 panelbookreader.add(areatextobookreader, gbc);

//BUTTONS
 gbc.gridx = 1;
 gbc.gridy = 2;
 gbc.gridwidth = 1;
 gbc.gridheight = 1;
 gbc.fill = GridBagConstraints.SOUTH;
 gbc.weightx = 0.0;
 gbc.weighty = 0.0;
 panelbookreader.add(openbook, gbc);


 /*ADD THE PARENT TO ITS PARENT OR TO GRANPARENT OF COMPONENT*/

 JTabbedPane tabulado = new JTabbedPane();
 tabulado.addTab("BOOK READER",panelbookreader);
Was it helpful?

Solution

Your weightx for JTextArea is 1.0 which is fine. But when you specify weighty also as 1.0, then there is no space for JButton. Make some changes in weighty of textarea and also specify weighty for jbutton.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top