JScrollPane cannot fit the content inside properly - java

I am using Netbeans Design to design a user interface.
I have a JPanel result_container inside a JScrollPane jsp_result to display the search result.
This is how I display the search result using loop:
public void displayResult(boolean isEmpty)
{
int count = al_isbn.size();
JButton[] btn_detail = new JButton[count];
JLabel[] lbl_num = new JLabel[count];
JLabel[] lbl_isbn = new JLabel[count];
JLabel[] lbl_book_name = new JLabel[count];
JLabel[] lbl_availability = new JLabel[count];
JLabel lbl_head_num = new JLabel("No.");
JLabel lbl_head_isbn = new JLabel("ISBN");
JLabel lbl_head_name = new JLabel("Book Name");
JLabel lbl_head_availability = new JLabel("Availability");
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
Dimension size;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.anchor = GridBagConstraints.NORTHWEST;
result_container.setLayout(gbl);
result_container.removeAll();
result_container.removeAll();
frmSize = this.getPreferredSize();
size = result_container.getPreferredSize();
Insets insets = this.getInsets();
if(isEmpty)
{
jsp_result.remove(result_container);
}
else
{
if(count > 0)
{
/**********set empty spacing**********/
JPanel pane_isbn = new JPanel(new FlowLayout());
pane_isbn.setPreferredSize(new Dimension(100, pane_isbn.getHeight()));
JPanel pane_name = new JPanel(new FlowLayout());
pane_name.setPreferredSize(new Dimension(300, pane_name.getHeight()));
JPanel pane_category = new JPanel(new FlowLayout());
pane_category.setPreferredSize(new Dimension(150, pane_category.getHeight()));
JPanel pane_btn = new JPanel(new FlowLayout());
pane_btn.setPreferredSize(new Dimension(100, pane_btn.getHeight()));
gbc.gridx = 0;
gbc.gridy = 0;
//gbl.setConstraints(lbl_head_num, gbc);
//result_container.add(lbl_head_num);
gbc.gridx = 1;
gbl.setConstraints(pane_isbn, gbc);
result_container.add(pane_isbn);
gbc.gridx = 2;
gbl.setConstraints(pane_name, gbc);
result_container.add(pane_name);
gbc.gridx = 4;
gbl.setConstraints(pane_btn, gbc);
result_container.add(pane_btn);
/**********set heading**********/
gbc.gridx = 0;
gbc.gridy = 1;
gbl.setConstraints(lbl_head_num, gbc);
result_container.add(lbl_head_num);
gbc.gridx = 1;
gbl.setConstraints(lbl_head_isbn, gbc);
result_container.add(lbl_head_isbn);
gbc.gridx = 2;
gbl.setConstraints(lbl_head_name, gbc);
result_container.add(lbl_head_name);
gbc.gridx = 3;
gbl.setConstraints(lbl_head_availability, gbc);
result_container.add(lbl_head_availability);
//display result row by row in gridbaglayout
for(int i = 0; i < count; ++i)
{
lbl_num[i] = new JLabel(i+1 + ". ");
lbl_isbn[i] = new JLabel(al_isbn.get(i));
lbl_book_name[i] = new JLabel(al_book_name.get(i));
lbl_availability[i] = new JLabel(al_availability.get(i).equals("TRUE") ? "Yes":"No");
btn_detail[i] = new JButton("Detail");
btn_detail[i].addActionListener(this);
btn_detail[i].setName(al_isbn.get(i));
gbc.gridx = 0;
gbc.gridy = i+2;
gbc.anchor = GridBagConstraints.NORTHEAST;
gbl.setConstraints(lbl_num[i], gbc);
result_container.add(lbl_num[i]);
gbc.gridx = 1;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbl.setConstraints(lbl_isbn[i], gbc);
result_container.add(lbl_isbn[i]);
gbc.gridx = 2;
gbl.setConstraints(lbl_book_name[i], gbc);
result_container.add(lbl_book_name[i]);
gbc.gridx = 3;
gbc.anchor = GridBagConstraints.CENTER;
gbl.setConstraints(lbl_availability[i], gbc);
result_container.add(lbl_availability[i]);
gbc.gridx = 4;
gbl.setConstraints(btn_detail[i], gbc);
result_container.add(btn_detail[i]);
if(action.equals("D")) //the action is delete and it is performed by admin
{
//declare delete button
JButton[] btn_delete = new JButton[count];
btn_delete[i] = new JButton("Delete");
btn_delete[i].addActionListener(this);
btn_delete[i].setName(al_isbn.get(i));
//add delete button
gbc.gridx = 5;
gbl.setConstraints(btn_delete[i], gbc);
result_container.add(btn_delete[i]);
}
else if(action.equals("E"))
{
//declare edit button
JButton[] btn_edit = new JButton[count];
btn_edit[i] = new JButton("Edit");
btn_edit[i].addActionListener(this);
btn_edit[i].setName(al_isbn.get(i));
//add edit button
gbc.gridx = 5;
gbl.setConstraints(btn_edit[i], gbc);
result_container.add(btn_edit[i]);
}
}
}
else
{
result_container.add(new JLabel("No result found."));
}
}
this.revalidate();
this.repaint();
}
And this is how the interface looks:
I scroll the scroll bar to the end already, but it always cannot fully display the last row no matter how many record I have.
This is the size of my panels:
this(the main JFrame): this.setPreferredSize(new Dimension(750, 250));
JScrollPane jsp_result: jsp_result.setPreferredSize(new Dimension(700, 200));
JPanel result_container: result_container.setPreferredSize(new Dimension(700, 200));

Related

Make components immune to GridBagConstrains.fill in GridBagLayout

I have a little Java swing GUI with a GridBagLayout. I have set the GridBagConstraints.fill = GridBagConstraints.BOTH but don't want that my buttons or text fields to get resized vertically. Furthermore, I only want my JTextArea to be resized and tried putting in maximum size for the components I don't want to size up, but it is not working. Any ideas on how I can make the GBC.fill only applicable for some components?
Reproducible example:
public class Main {
static JFrame frame = new JFrame("Create New Entry");
static JPanel wrapper = new JPanel();
static JTextField title = new JTextField(20);
static JLabel titleLabel = new JLabel("Title");
static JTextField username = new JTextField(20);
static JLabel usernameLabel = new JLabel("Username");
static JTextField email = new JTextField(20);
static JLabel emailLabel = new JLabel("Email Address");
static JPasswordField password = new JPasswordField(20);
static JLabel passwordLabel = new JLabel("Password");
static JButton generatePassword = new JButton("Generate Password");
static JPasswordField confirmPassword = new JPasswordField();
static JLabel confirmPasswordLabel = new JLabel("Confirm Password");
static JToggleButton showPassword = new JToggleButton("Show Password");
static JButton submit = new JButton("Submit Entry");
static JLabel notesLabel = new JLabel("Notes");
static JTextArea textArea = new JTextArea(5, 0);
static JScrollPane scrollPane = new JScrollPane(textArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
public static void main(String[] args) {
GridBagConstraints gbc = new GridBagConstraints();
wrapper.setLayout(new GridBagLayout());
gbc.gridwidth = GridBagConstraints.RELATIVE;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.insets = new Insets(10, 10, 10, 10);
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 0;
wrapper.add(titleLabel, gbc);
gbc.gridx = 1;
gbc.gridwidth = 2;
wrapper.add(title, gbc);
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 1;
wrapper.add(usernameLabel, gbc);
gbc.gridx = 1;
gbc.gridwidth = 2;
wrapper.add(username, gbc);
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 2;
wrapper.add(emailLabel, gbc);
gbc.gridx = 1;
gbc.gridwidth = 2;
wrapper.add(email, gbc);
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 3;
wrapper.add(passwordLabel, gbc);
gbc.gridx = 1;
wrapper.add(password, gbc);
gbc.gridx = 2;
wrapper.add(generatePassword, gbc);
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 4;
wrapper.add(confirmPasswordLabel, gbc);
gbc.gridx = 1;
wrapper.add(confirmPassword, gbc);
gbc.gridx = 2;
wrapper.add(showPassword, gbc);
gbc.gridx = 0;
gbc.gridy = 5;
wrapper.add(notesLabel, gbc);
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 1;
gbc.gridwidth = 2;
wrapper.add(scrollPane, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 3;
gbc.gridy = 6;
wrapper.add(submit, gbc);
password.setPreferredSize(new Dimension(300, 26));
confirmPassword.setPreferredSize(new Dimension(300, 26));
title.setPreferredSize(new Dimension(300, 26));
username.setPreferredSize(new Dimension(300, 26));
email.setPreferredSize(new Dimension(300, 26));
frame.add(wrapper);
frame.pack();
frame.setMinimumSize(frame.getSize());
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Any ideas on how I can make the GBC.fill only applicable for some components?
Yes. Don't have all components use a GridBagConstraints object whose fill field is .BOTH. Each component should be added with its own GBC object, and the ones you want filled both horizontally and vertically should have the fill property set to .BOTH, and the components that should only expand horizontally should have GBC fill field of .HORIZONTAL. When I use the GridBagLayout, I often create helper method(s) for creating constraint objects, methods that know what settings to use based on parameters passed into them, and I suggest that you consider doing the same.
As Abra mentions, yes, you can modify a GridBagConstraints (GBC) object, by changing the settings of one or more fields as necessary, and I sometimes do this too, but more often, I use helper methods to create GBC objects on the fly. It just works better for me that way.
For example:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.*;
public class Main2 extends JPanel {
private static final long serialVersionUID = 1L;
private static final int FIELD_COLS = 20;
private static final int TA_ROWS = 10;
private static final int INGS_GAP = 10;
private JLabel titleLabel = new JLabel("Title");
private JLabel userNameLabel = new JLabel("Username");
private JLabel emailAddrLabel = new JLabel("EmailAddress");
private JLabel passwordLabel = new JLabel("Password");
private JLabel confirmPasswordLabel = new JLabel("Confirm Password");
private JLabel notesLabel = new JLabel("Notes");
private JTextField titleField = new JTextField(FIELD_COLS);
private JTextField userNameField = new JTextField(FIELD_COLS);
private JTextField emailField = new JTextField(FIELD_COLS);
private JPasswordField passwordField = new JPasswordField(FIELD_COLS);
private JPasswordField confirmPasswordField = new JPasswordField(FIELD_COLS);
private JTextArea NotesArea = new JTextArea(TA_ROWS, FIELD_COLS);
private JScrollPane textAreaScrollPane = new JScrollPane(NotesArea);
private JButton generatePasswordBtn = new JButton("Generate Password");
private JButton showPasswordBtn = new JButton("Show Password");
private JButton submitEntryBtn = new JButton("Submit Entry");
public Main2() {
setLayout(new GridBagLayout());
// basic GBC use
int row = 0;
GridBagConstraints gbc = createGbc(0, row, GridBagConstraints.HORIZONTAL);
add(titleLabel, gbc);
gbc = createGbc(1, row, GridBagConstraints.HORIZONTAL, 2, 1);
add(titleField, gbc);
row++;
gbc = createGbc(0, row, GridBagConstraints.HORIZONTAL);
add(userNameLabel, gbc);
gbc = createGbc(1, row, GridBagConstraints.HORIZONTAL, 2, 1);
add(userNameField, gbc);
row++;
gbc = createGbc(0, row, GridBagConstraints.HORIZONTAL);
add(emailAddrLabel, gbc);
gbc = createGbc(1, row, GridBagConstraints.HORIZONTAL, 2, 1);
add(emailField, gbc);
row++;
gbc = createGbc(0, row, GridBagConstraints.HORIZONTAL);
add(passwordLabel, gbc);
gbc = createGbc(1, row, GridBagConstraints.HORIZONTAL);
add(passwordField, gbc);
gbc = createGbc(2, row, GridBagConstraints.HORIZONTAL);
add(generatePasswordBtn, gbc);
row++;
gbc = createGbc(0, row, GridBagConstraints.HORIZONTAL);
add(confirmPasswordLabel, gbc);
gbc = createGbc(1, row, GridBagConstraints.HORIZONTAL);
add(confirmPasswordField, gbc);
gbc = createGbc(2, row, GridBagConstraints.HORIZONTAL);
add(showPasswordBtn, gbc);
// here we set the GBC weighty to non-0 value to allow vertical expansion
// of added components
row++;
int txtAreaRows = TA_ROWS;
gbc = createGbc(0, row, GridBagConstraints.HORIZONTAL);
gbc.anchor = GridBagConstraints.WEST;
gbc.weighty = 1.0;
add(notesLabel, gbc);
gbc = createGbc(1, row, GridBagConstraints.BOTH, 2, txtAreaRows);
gbc.weighty = 1.0;
add(textAreaScrollPane, gbc);
textAreaScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
row += txtAreaRows;
gbc = createGbc(0, row, GridBagConstraints.HORIZONTAL);
add(new JLabel(""), gbc);
gbc = createGbc(1, row, GridBagConstraints.HORIZONTAL, 2, 1);
add(submitEntryBtn, gbc);
}
private static GridBagConstraints createGbc(int x, int y, int fill, int width, int height) {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = x;
gbc.gridy = y;
gbc.gridwidth = width;
gbc.gridheight = height;
gbc.fill = fill;
// allow horizontal expansion *unless* at left-most position in row
gbc.weightx = x == 0 ? 0.0 : 1.0;
gbc.weighty = 0.0; // default to no vertical expansion
gbc.insets = new Insets(INGS_GAP, INGS_GAP, INGS_GAP, INGS_GAP);
return gbc;
}
private static GridBagConstraints createGbc(int x, int y, int fill) {
return createGbc(x, y, fill, 1, 1);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
Main2 mainPanel = new Main2();
JFrame frame = new JFrame("GUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
}
}

Jpanel with toolbar and title

I am new to Java Swing.I want to design one JToolBar. The JToolBar should be placed on the right of JPanel with the minimize, restore and close icons just like the one shown in the image below:-
panelCont.setLayout(cl);
JPanel titlePanel = new JPanel();
titlePanel.setLayout(new BorderLayout());
JPanel panel1 = new JPanel();
panel1.createToolTip();
panel1.setBorder(BorderFactory.createTitledBorder("Add Products"));
JPanel panel2 = new JPanel();
panel2.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JLabel lab = new JLabel("Add Company");
JTextField tf = new JTextField(20);
JButton btn = new JButton("Add");
gbc.gridx = 2;
gbc.gridy = 1;
gbc.weightx= 0.5;
gbc.ipadx = 0;
// gbc.insets = new Insets(20, 10, 10, 0);
panel2.add(lab, gbc);
gbc.gridx = 2;
gbc.gridy = 2;
gbc.weightx= 0.5;
gbc.ipadx = 0;
// gbc.insets = new Insets(20, 10, 10, 0);
panel2.add(tf, gbc);
gbc.gridx = 2;
gbc.gridy = 3;
gbc.weightx= 0.5;
gbc.ipadx = 0;
// gbc.insets = new Insets(20, 10, 10, 0);
panel2.add(btn, gbc);
titlePanel.add(BorderLayout.NORTH, panel1);
titlePanel.add(BorderLayout.CENTER, panel2);
panelCont.add(titlePanel, "1");
// panelCont.set
cl.show(panelCont, "1");
// frame1.add(BorderLayout.CENTER , titlePanel);
frame1.add(BorderLayout.CENTER , panelCont);
frame1.setVisible(true);
}
i have written that but didn't get the desired output please help me. Thanks in advance.

GridBagLayout fail to work on gridy

I have a problem with getting the GridBagLayout work as expected.
The gridy of the GridBagConstraints didn't seem to function as expected at all for atomIndexLabel (JLabel), atomIndexExample (JLabel) and atomIndexAddField (JTextField). I want to align them to approximately the middle of the table, but when running, they stick to the top, just like gridy is set to 0, 1 and 2.
I tried to add an emptyElement, but it didn't work. My code is below. Any suggestions?
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.table.DefaultTableModel;
public class Test extends JPanel {
private static final long serialVersionUID = -7810531559762481489L;
private JLabel atomIndexLabel, atomIndexExample, atomIndexNotification;
private JTextField atomIndexAddField;
private JButton atomIndexAddButton, atomIndexRemoveButton;
private AtomIndexButtonListener atomIndexButtonListener;
private JTable atomIndexTable;
private JScrollPane atomIndexTableScrollPane;
private DefaultTableModel atomIndexTableModel;
public Test() {
this.atomIndexLabel = new JLabel("Input one or multiple atom indexs:");
this.atomIndexExample = new JLabel("Example: 1 2 3 4 5");
this.atomIndexAddField = new JTextField(20);
this.atomIndexAddButton = new JButton("Add Atom Index");
this.atomIndexTable = new JTable(1, 1);
this.atomIndexTableModel = new DefaultTableModel() {
private static final long serialVersionUID = -6458638313466319330L;
#Override
public boolean isCellEditable(int row, int column) {
return false;
}
};
this.atomIndexTable.setModel(atomIndexTableModel);
this.atomIndexTableModel.addColumn("Atom_Index");
this.atomIndexTableScrollPane = new JScrollPane(atomIndexTable);
this.atomIndexTableScrollPane.setPreferredSize(new Dimension(100, 170));
this.atomIndexRemoveButton = new JButton("Remove Selected");
this.atomIndexNotification = new JLabel("No input atom index");
this.atomIndexNotification.setHorizontalAlignment(JLabel.CENTER);
setupLayout();
this.atomIndexButtonListener = new AtomIndexButtonListener();
this.atomIndexAddButton.addActionListener(atomIndexButtonListener);
this.atomIndexRemoveButton.addActionListener(atomIndexButtonListener);
this.setBorder(new TitledBorder("Atom Index Input"));
}
private void setupLayout() {
this.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
/*JComponent emptyElement = new JLabel("");
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridwidth = 1;
gbc.gridheight = 6;
gbc.anchor = GridBagConstraints.LAST_LINE_START;
this.add(emptyElement, gbc);*/
gbc.gridx = 0;
gbc.gridy = 6;
gbc.gridwidth = 4;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
this.add(atomIndexLabel, gbc);
gbc.gridx = 0;
gbc.gridy = 7;
gbc.gridwidth = 2;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
this.add(atomIndexExample, gbc);
gbc.gridx = 0;
gbc.gridy = 8;
gbc.gridwidth = 4;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
this.add(atomIndexAddField, gbc);
gbc.gridx = 0;
gbc.gridy = 11;
gbc.gridwidth = 4;
gbc.gridheight = 1;
gbc.anchor = GridBagConstraints.CENTER;
gbc.insets = new Insets(10, 0, 0, 0);
this.add(atomIndexAddButton, gbc);
gbc.gridx = 5;
gbc.gridy = 0;
gbc.gridwidth = 6;
gbc.gridheight = 12;
gbc.insets = new Insets(10, 10, 0, 0);
this.add(atomIndexTableScrollPane, gbc);
gbc.gridx = 5;
gbc.gridy = 12;
gbc.gridwidth = 6;
gbc.gridheight = 1;
gbc.insets = new Insets(10, 10, 0, 0);
this.add(atomIndexRemoveButton, gbc);
gbc.gridx = 0;
gbc.gridy = 13;
gbc.gridwidth = 10;
gbc.gridheight = 1;
gbc.insets = new Insets(20, 0, 0, 0);
this.add(atomIndexNotification, gbc);
}
private class AtomIndexButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("For Test only");
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new Test());
frame.setSize(1000, 900);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new FlowLayout());
frame.setVisible(true);
}
}

How to take these labels in exactly required position

I am trying to create a small GUI with some lables and txtAreas. I did it by using absolute positioning, but I want to go on gridbag layout. I am trying from last 3 days, but couldnt get these lables as required position. either stucking in the middle around, or they stucking near the border. Please help to get them in these positions.
public void initUIPanel()
{
jf = new JFrame();
jf.setTitle("Mortgage Calculator");
jf.setLocation(100,200);
jf.setSize(400,500);
jf.setVisible (true);
//jf.setResizable(false);
JPanel panel = new JPanel();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTHWEST;
loanAmount = new JTextField(15);
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(loanAmount, gbc);
panel.add(loanAmount);
loanTerm = new JTextField(15);
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(loanTerm, gbc);
panel.add(loanTerm);
amount = new JLabel("Loan Amount");
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(amount, gbc);
panel.add(amount);
term= new JLabel("Loan Term");
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(term, gbc);
panel.add(term);
currency = new JLabel ("AUD");
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(currency, gbc);
panel.add(currency);
numOfYear = new JLabel ("Year");
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(numOfYear, gbc);
panel.add(numOfYear);
JPanel middlePanel = new JPanel ();
middlePanel.setBorder ( new TitledBorder ( new EtchedBorder (), "Display Area" ) );
txtResult = new JTextArea();
gbc.gridx = 0;
gbc.gridy = 0;
gbl.setConstraints(txtResult, gbc);
panel.add(txtResult);
jf.add(panel,"Center");
//panel.setBounds(200,200,200,20);
jf.setVisible(true);
}
It is giving everything in 2 lines. all messed.
What I want is
First Line: Loan Amount: ............(txt Area).... "AUD"
Second LIne: Loan Term: .............(Txt Area......Years
Then Txtbox
Thank you
You haven't set the layout of the panel:
panel.setLayout(gbl);
And, as already said in the comments, all the components have the same gridx and gridy, which is obviously not right.
Here's a complete example which, if I understand correctly, does what you want:
import javax.swing.*;
import java.awt.*;
public class GblTest extends JFrame {
public GblTest() {
add(createPanel(), BorderLayout.NORTH);
}
private JPanel createPanel() {
JPanel p = new JPanel();
p.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.anchor = GridBagConstraints.BASELINE_LEADING;
p.add(new JLabel("Loan amount"), c);
c.gridx++;
p.add(new JTextField(15), c);
c.gridx++;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
p.add(new JLabel("AUD"), c);
c.gridx = 0;
c.gridy++;
c.fill = GridBagConstraints.NONE;
c.weightx = 0.0;
p.add(new JLabel("Loan term"), c);
c.gridx++;
p.add(new JTextField(15), c);
c.gridx++;
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 1.0;
p.add(new JLabel("Years"), c);
return p;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
GblTest test = new GblTest();
test.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
test.pack();
test.setVisible(true);
}
});
}
}

JRadio Buttons are "Hiding" in GridBagLayout

Please have a look at the following code
package normal;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Form extends JFrame
{
private JLabel heightLabel, weightLabel, waistLabel, neckLabel, hipsLabel,genderLabel,valuesLabel,bfPercentageLabel;
private JLabel logoLabel;
private ImageIcon logo;
private JTextField heightTxt, weightTxt, waistTxt, neckTxt, hipsTxt;
private JRadioButton maleRadio, femaleRadio, inchesRadio, cmRadio;
private ButtonGroup genderGroup, valuesGroup;
private JComboBox percentageCombo;
private JPanel centerPanel, northPanel, southPanel;
public Form()
{
//Declaring instance variables
heightLabel = new JLabel("Height: ");
weightLabel = new JLabel("Weight: ");
waistLabel = new JLabel("Waist: ");
neckLabel = new JLabel("Neck: ");
hipsLabel = new JLabel("Hips: ");
genderLabel = new JLabel("Gender: ");
valuesLabel = new JLabel("Values in: ");
logoLabel = new JLabel();
logo = new ImageIcon(getClass().getResource("/images/calc_logo_final_2_edit.gif"));
logoLabel.setIcon(logo);
heightTxt = new JTextField(10);
weightTxt = new JTextField(10);
waistTxt = new JTextField(10);
neckTxt = new JTextField(10);
hipsTxt = new JTextField(10);
maleRadio = new JRadioButton("Male");
femaleRadio = new JRadioButton("Female");
genderGroup = new ButtonGroup();
genderGroup.add(maleRadio);
genderGroup.add(femaleRadio);
inchesRadio = new JRadioButton("Inches");
cmRadio = new JRadioButton("Centimeters");
valuesGroup = new ButtonGroup();
valuesGroup.add(inchesRadio);
valuesGroup.add(cmRadio);
percentageCombo = new JComboBox();
percentageCombo.addItem("No Value is Set");
this.add(createNorthPanel(),"North");
this.add(createCenterPanel(),"Center");
this.setResizable(false);
this.pack();
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private JPanel createNorthPanel()
{
northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
northPanel.add(logoLabel);
return northPanel;
}
private JPanel createCenterPanel()
{
centerPanel = new JPanel();
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
centerPanel.setLayout(gbl);
//creating a jpanel for gender radio buttons
JPanel genderPanel = new JPanel();
genderPanel.setLayout(new FlowLayout());
genderPanel.add(genderLabel);
genderPanel.add(maleRadio);
genderPanel.add(femaleRadio);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(15,5,0,0);
centerPanel.add(heightLabel,gbc);
gbc.gridx = 2;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(15,5,0,0);
centerPanel.add(heightTxt,gbc);
gbc.gridx = 3;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(15,5,0,0);
centerPanel.add(weightLabel,gbc);
gbc.gridx = 4;
gbc.gridy = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(15,5,0,0);
centerPanel.add(weightTxt,gbc);
gbc.gridx = 1;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(15,5,0,0);
centerPanel.add(waistLabel,gbc);
gbc.gridx = 2;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(15,5,0,0);
centerPanel.add(waistTxt,gbc);
gbc.gridx = 3;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(15,5,0,0);
centerPanel.add(neckLabel,gbc);
gbc.gridx = 4;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(15,5,0,0);
centerPanel.add(neckTxt,gbc);
gbc.gridx = 5;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(15,5,0,0);
centerPanel.add(hipsLabel,gbc);
gbc.gridx = 6;
gbc.gridy = 2;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(15,5,0,0);
centerPanel.add(hipsTxt,gbc);
gbc.gridx = 1;
gbc.gridy = 3;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(15,5,0,0);
centerPanel.add(genderLabel,gbc);
gbc.gridx = 2;
gbc.gridy = 3;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(15,5,0,0);
centerPanel.add(maleRadio,gbc);
gbc.gridx = 3;
gbc.gridy = 3;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(15,-10,0,0);
centerPanel.add(femaleRadio,gbc);
gbc.gridx = 1;
gbc.gridy = 4;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(50,5,0,0);
centerPanel.add(valuesLabel,gbc);
return centerPanel;
}
}
As you can see, the JRadio button "female" is hiding a part of it, and once you move your cursor, it shows up completely. I guess this is happening because it is using minus spacing in "insets".
However I did it like that to reduce the gap between 2 radio buttons. Male is in gridx = 2, and female is in gridx = 3, which is a massive space between the buttons.So I used minus space in insets to reduce the space, unfortunately it came like this.
I tried adding the JLabel, maleRadio and femaleRadio into a seperate JPanel which is having flowlayout, and put it into gbc.gridx = 2; gbc.gridy = 3;. It made everything worst by matching all the cells in gridy = 3 into the width of new JPanel.
Please help me to reduce the gap between these 2 JRadio buttons, without having any issue. Thank you.
Dont extend JFrame rather create an instance and use that.
Also I see you add your radio buttons to a panel but you dont add the panel rather you re-add the radio buttons to centerpanel? choose one way lose the other (though I think this might have occurred in trying to mend the problem?)
An SSCCE most importantly is compilable (via correct syntax and no compile error) and runnable (via a main method and no runtime exceptions (unless thats the problem :P) - like your reading of the image - please find a way to include resources i.e link to an URL with the logo or make a method return a simple image the same size as logo or simply leave it out).
The problem is here:
gbc.gridx = 3;
gbc.gridy = 3;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(15, -10, 0, 0);
centerPanel.add(femaleRadio, gbc);
-10 should definitely not be there (maybe typo?) as this will cause it to overlap or in this case underlap another component, rather use anything greater than or equal to 0:
gbc.gridx = 3;
gbc.gridy = 3;
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(15, 10, 0, 0);
centerPanel.add(femaleRadio, gbc);
which would give us:
UPDATE:
Also it is important to note GridBagContsraints like gridx etc start at 0 and not 1.
+1 to #Gagandeeps balis comment on re-using values which have been set already and are the same in GridBagConstraints, here is your code with all talked about fixes:
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
class Form {
private JLabel heightLabel, weightLabel, waistLabel, neckLabel, hipsLabel, genderLabel, valuesLabel, bfPercentageLabel;
private JLabel logoLabel;
private ImageIcon logo;
private JTextField heightTxt, weightTxt, waistTxt, neckTxt, hipsTxt;
private JRadioButton maleRadio, femaleRadio, inchesRadio, cmRadio;
private ButtonGroup genderGroup, valuesGroup;
private JComboBox percentageCombo;
private JPanel centerPanel, northPanel, southPanel;
public Form() {
//Declaring instance variables
heightLabel = new JLabel("Height: ");
weightLabel = new JLabel("Weight: ");
waistLabel = new JLabel("Waist: ");
neckLabel = new JLabel("Neck: ");
hipsLabel = new JLabel("Hips: ");
genderLabel = new JLabel("Gender: ");
valuesLabel = new JLabel("Values in: ");
logoLabel = new JLabel();
//logo = new ImageIcon(getClass().getResource("/images/calc_logo_final_2_edit.gif"));
//logoLabel.setIcon(logo);
heightTxt = new JTextField(10);
weightTxt = new JTextField(10);
waistTxt = new JTextField(10);
neckTxt = new JTextField(10);
hipsTxt = new JTextField(10);
maleRadio = new JRadioButton("Male");
femaleRadio = new JRadioButton("Female");
genderGroup = new ButtonGroup();
genderGroup.add(maleRadio);
genderGroup.add(femaleRadio);
inchesRadio = new JRadioButton("Inches");
cmRadio = new JRadioButton("Centimeters");
valuesGroup = new ButtonGroup();
valuesGroup.add(inchesRadio);
valuesGroup.add(cmRadio);
percentageCombo = new JComboBox();
percentageCombo.addItem("No Value is Set");
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createNorthPanel(), "North");
frame.add(createCenterPanel(), "Center");
frame.setResizable(false);
frame.pack();
frame.setVisible(true);
}
private JPanel createNorthPanel() {
northPanel = new JPanel();
northPanel.setLayout(new FlowLayout());
northPanel.add(logoLabel);
return northPanel;
}
private JPanel createCenterPanel() {
centerPanel = new JPanel(new GridBagLayout());
GridBagLayout gbl = new GridBagLayout();
centerPanel.setLayout(gbl);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(15, 5, 0, 0);
gbc.gridx = 0;
gbc.gridy = 0;
centerPanel.add(heightLabel, gbc);
gbc.gridx = 1;
centerPanel.add(heightTxt, gbc);
gbc.gridx = 2;
centerPanel.add(weightLabel, gbc);
gbc.gridx = 3;
centerPanel.add(weightTxt, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
centerPanel.add(waistLabel, gbc);
gbc.gridx = 1;
centerPanel.add(waistTxt, gbc);
gbc.gridx = 2;
centerPanel.add(neckLabel, gbc);
gbc.gridx = 3;
centerPanel.add(neckTxt, gbc);
gbc.gridx = 4;
centerPanel.add(hipsLabel, gbc);
gbc.gridx = 5;
centerPanel.add(hipsTxt, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
centerPanel.add(genderLabel, gbc);
gbc.gridx = 1;
centerPanel.add(maleRadio, gbc);
gbc.gridx = 2;
centerPanel.add(femaleRadio, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.insets = new Insets(50, 5, 0, 0);
centerPanel.add(valuesLabel, gbc);
return centerPanel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Form();
}
});
}
}

Categories