Related
I have old Java NetBenas project that contains UI classes derived from JPanel. Code editor does not contain Design button in these classes. Design button appears only in newly created JPanel fom. Is it possible somehow tell NetBeans to open old forms in Design mode?
Form that can't be opened in Design mode:
public class About extends javax.swing.JPanel /*JFrame*/ {
private ClassLoader cl = getClass().getClassLoader();
public ImageIcon logo = new ImageIcon(
cl.getResource("icons/logo.gif"));
public ImageIcon ball = new ImageIcon(
cl.getResource("icons/ball.gif"));
private JPanel centerPanel;
private JPanel topPanel;
private JLabel productName = new JLabel("<html><font face=\"verdana\" size=10>"+
"SocketTest",logo,JLabel.CENTER);
private JTextArea readme = new JTextArea();
private JScrollPane jsp;
String html="<html><font face=\"verdana\" size=\"2\">";
private JLabel versionText = new JLabel(html+"Version",ball,JLabel.LEFT);
private JLabel version = new JLabel(html+": 3.0.0", JLabel.LEFT);
private JLabel licenceText = new JLabel(html+"Licence",ball,JLabel.LEFT);
private JLabel licence = new JLabel(html+": GNU Lesser General Public License", JLabel.LEFT);
private JLabel authorText = new JLabel(html+"Author",ball,JLabel.LEFT);
private JLabel author = new JLabel(html+": Akshathkumar Shetty", JLabel.LEFT);
private JLabel copyrightText = new JLabel(html+"Copyright © 2003-2008 Akshathkumar Shetty",ball,JLabel.LEFT);
private JLabel websiteText = new JLabel(html+"Website",ball,JLabel.LEFT);
private JLabel website = new JLabel(html+": http://sockettest.sourceforge.net/", JLabel.LEFT);
private JLabel readmeText = new JLabel(html+"ReadMe",ball,JLabel.LEFT);
private GridBagConstraints gbc = new GridBagConstraints();
/** Creates a new instance of About */
public About() {
//Container cp = getContentPane();
Container cp = this;
topPanel = new JPanel();
topPanel.setLayout(new GridBagLayout());
gbc.insets = new Insets( 2, 2, 2, 2 );
gbc.weighty = 0.0;
gbc.weightx = 0.0;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.gridwidth = 3;
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.NONE;
topPanel.add(productName, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridy = 1;
gbc.gridx = 0;
gbc.weightx = 0.0;
topPanel.add(versionText, gbc);
gbc.gridx = 1;
topPanel.add(version, gbc);
gbc.gridx = 2;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
topPanel.add(Box.createHorizontalGlue(), gbc);
gbc.gridy = 2;
gbc.gridx = 0;
gbc.weightx = 0.0;
topPanel.add(licenceText, gbc);
gbc.gridx = 1;
topPanel.add(licence, gbc);
gbc.gridx = 2;
gbc.weightx = 1.0;//1.0
gbc.fill = GridBagConstraints.HORIZONTAL;
topPanel.add(new JLabel(), gbc);
gbc.gridy = 3;
gbc.gridx = 0;
gbc.weightx = 0.0;
topPanel.add(authorText, gbc);
gbc.gridx = 1;
topPanel.add(author, gbc);
gbc.gridx = 2;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
topPanel.add(Box.createHorizontalGlue(), gbc);
gbc.gridy = 4;
gbc.gridx = 0;
gbc.weightx = 0.0;
topPanel.add(websiteText, gbc);
gbc.gridx = 1;
topPanel.add(website, gbc);
gbc.gridx = 2;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
topPanel.add(Box.createHorizontalGlue(), gbc);
gbc.gridy = 5;
gbc.gridx = 0;
gbc.weightx = 0.0;
gbc.gridwidth = 2;
topPanel.add(copyrightText, gbc);
gbc.gridwidth = 1;
gbc.gridx = 2;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
topPanel.add(Box.createHorizontalGlue(), gbc);
gbc.gridy = 6;
gbc.gridx = 0;
gbc.weightx = 0.0;
gbc.gridwidth = 1;
topPanel.add(readmeText, gbc);
gbc.gridx = 1;
topPanel.add(new JLabel(" "), gbc);
gbc.gridx = 2;
gbc.weightx = 1.0;
gbc.fill = GridBagConstraints.HORIZONTAL;
topPanel.add(Box.createHorizontalGlue(), gbc);
centerPanel = new JPanel();
readme.setText("Loading... readme");
try {
String cont = Util.readFile("readme.txt",(Object)About.this);
readme.setText(cont);
} catch (IOException e){
System.err.println("Error reading readme.txt "+e);
readme.append("\r\nFailed : "+e.getMessage());
}
readme.setEditable(false);
readme.setLineWrap(true);
readme.setWrapStyleWord(true);
jsp = new JScrollPane(readme);
centerPanel.setLayout(new BorderLayout());
centerPanel.add(jsp);
centerPanel.setBorder(BorderFactory.createEmptyBorder(0,9,0,9));
cp.setLayout(new BorderLayout(0,10));
cp.add(topPanel,BorderLayout.NORTH);
cp.add(centerPanel,BorderLayout.CENTER);
setBorder(BorderFactory.createEmptyBorder(10,10,10,10));
}
}
My form is very simple I just want to have labels next to text fields. The labels are default centering and it makes the form look weird. I want to the labels exactly next to the textfield. I have played with the horizontal alignment of the labels and textfields but it did not change anything.
Here is my code:
JPanel root = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets= new Insets(0,0,0,0);
newVehicleRecord.setLayout(new BorderLayout());
newVehicleRecord.add(root,BorderLayout.PAGE_START);
JLabel title = new JLabel("New Vehicle Record - Customer ID:" + customerIDInfo.getText());
title.setFont(fontTitle);
gbc.weightx = 0;
gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth= 2;
root.add(title,gbc);
gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth= 1;
root.add(Box.createVerticalStrut(15),gbc);
gbc.gridx = 0; gbc.gridy = 2;
JLabel classificationLabel = new JLabel("Classification:");
classificationLabel.setHorizontalAlignment(JLabel.RIGHT);
root.add(classificationLabel,gbc);
gbc.gridx = 1; gbc.gridy = 2;
JTextField classificationTextField = new JTextField(10);
classificationTextField.setHorizontalAlignment(JTextField.LEFT);
root.add(classificationTextField,gbc);
gbc.gridx = 0; gbc.gridy = 3;
JLabel modelLabel = new JLabel("Model:");
root.add(modelLabel,gbc);
gbc.gridx = 1; gbc.gridy = 3;
JTextField modelTextField = new JTextField(10);
root.add(modelTextField,gbc);
gbc.gridx = 0; gbc.gridy = 4;
JLabel makeLabel = new JLabel("Make:");
root.add(makeLabel,gbc);
gbc.gridx = 1; gbc.gridy = 4;
JTextField makeTextField = new JTextField(10);
root.add(makeTextField,gbc);
I get the following display: http://prntscr.com/6j3iki
As you can see there is a lot of empty space between the label and the textfield which I don't want.
I want to the labels exactly next to the textfield.
You need to play with the anchor constraint:
gbc.anchor = GridBagConstraints.LINE_END;
panel.add(label, gbc);
gbc.anchor = GridBagConstraints.LINE_START;
panel.add(textField, gbc);
Also you would probably want something like:
gbc.insets = new Insets(5, 10, 5, 10);
so the right edge of the label has some space between the left edge of the text field.
Read the section from the Swing tutorial on How to Use GridBagLayout for more information on all the constraints.
Something like this:
gbc.gridx = 0; gbc.gridy = 3;
gbc.anchor = GridBagConstraints.EAST;
JLabel modelLabel = new JLabel("Model:");
root.add(modelLabel,gbc);
So you need to add the line
gbc.anchor = GridBagConstraints.EAST;
to each your label.
Another possibility:
gbc.gridx = 0; gbc.gridy = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
JLabel modelLabel = new JLabel("Model:");
modelLabel.setHorizontalAlignment(SwingConstants.RIGHT);
root.add(modelLabel,gbc);
I was trying to make a keyboard layout, where one can specify the buttons' width. Therefore, I made an attempt with GridBagLayout but without success.
To illustrate my problem I did a simple example, where I expected to obtain this (Button2, Button4, Button5, Button6):
but instead I get Button4 and Button6 of double width.
The code is:
package views;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestLayout extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new TestLayout().setVisible(true);
}
});
}
public TestLayout() {
JButton btn;
setBounds(0, 0, 444, 111);
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.gridy = 1;//ROW 1
btn = new JButton("Button 1");gbc.gridx = 0;gbc.gridwidth = 1;add(btn, gbc);
btn = new JButton("Button 2");gbc.gridx = 1;gbc.gridwidth = 2;add(btn, gbc);
btn = new JButton("Button 3");gbc.gridx = 3;gbc.gridwidth = 1;add(btn, gbc);
btn = new JButton("Button 4");gbc.gridx = 4;gbc.gridwidth = 2;add(btn, gbc);
gbc.gridy = 2;//ROW 2
btn = new JButton("Button 5");gbc.gridx = 0;gbc.gridwidth = 2;add(btn, gbc);
btn = new JButton("Button 6");gbc.gridx = 2;gbc.gridwidth = 2;add(btn, gbc);
btn = new JButton("Button 7");gbc.gridx = 4;gbc.gridwidth = 1;add(btn, gbc);
btn = new JButton("Button 8");gbc.gridx = 5;gbc.gridwidth = 1;add(btn, gbc);
}
}
Moreover, my goal is to define the keyboard something like this, with no correlation between rows, which I couldn't achieved with this Layout manager:
I took this matter over to Why does this GridBagLayout not appear as planned? & camickr solved it using a dummy row of components, each 1 gridwidth wide.
These 2 images show:
At the bottom, the 'minimalist' version of the code that uses a 1px tall transparent image.
At the top, the more obvious version that uses a solid black image that is 5 px tall.
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.*;
public class KeyBoardLayout {
private JComponent ui = null;
KeyBoardLayout(boolean lowImpact) {
initUI(lowImpact);
}
public void initUI(boolean lowImpact) {
if (ui != null) {
return;
}
ui = new JPanel(new GridBagLayout());
ui.setBorder(new EmptyBorder(4, 4, 4, 4));
GridBagConstraints gbc = new GridBagConstraints();
gbc.weightx = .5;
gbc.weighty = .5;
gbc.fill = GridBagConstraints.BOTH;
/* This code adds a dummy (invisible) row of components, 1 per
single gridwidth column. It has the effect of forcing the GBL width
to the size we would expect, proportional to each gridwidth assigned.
The problem with this (simple) approach is that the perfect
width will change according to PLAF and the content/preferred
size of the visible components. */
// TODO! improve on use of 'magic numbers'
int w = 30; // adjust width per requirement
int h = lowImpact ? 1 : 5; // 1 for small height/border, 5 for large
// TYPE_INT_RGB for black
// TYPE_INT_ARGB for invisible
int t = lowImpact ?
BufferedImage.TYPE_INT_ARGB :
BufferedImage.TYPE_INT_RGB;
// an icon for the dummy row
ImageIcon ii = new ImageIcon(new BufferedImage(w, h, t));
ui.setBorder(new CompoundBorder(
ui.getBorder(), new EmptyBorder(0, 0, h, 0)));
// put a 'padding cell' in each column of the top row
// to force the layout to respect each individual column
for (int i = 0; i < 22; i++) {
gbc.gridx = i;
gbc.gridy = 4;
ui.add(new JLabel(ii));
}
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 3;
ui.add(new JButton("1,1 (3)"), gbc);
gbc.gridx = 3;
gbc.gridwidth = 2;
ui.add(new JButton("2,1 (2)"), gbc);
gbc.gridx = 5;
ui.add(new JButton("3,1 (2)"), gbc);
gbc.gridx = 7;
ui.add(new JButton("4,1 (2)"), gbc);
gbc.gridx = 9;
ui.add(new JButton("5,1 (2)"), gbc);
gbc.gridx = 11;
ui.add(new JButton("6,1 (2)"), gbc);
gbc.gridx = 13;
ui.add(new JButton("7,1 (2)"), gbc);
gbc.gridx = 15;
gbc.gridwidth = 3;
ui.add(new JButton("8,1 (3)"), gbc);
gbc.gridx = 18;
gbc.gridwidth = 4;
ui.add(new JButton("9,1 (4)"), gbc);
gbc.gridx = 0;
gbc.gridy = 2;
ui.add(new JButton("1,2 (4)"), gbc);
gbc.gridx = 4;
gbc.gridwidth = 2;
ui.add(new JButton("2,2 (2)"), gbc);
gbc.gridx = 6;
ui.add(new JButton("3,2 (2)"), gbc);
gbc.gridx = 8;
ui.add(new JButton("4,2 (2)"), gbc);
gbc.gridx = 10;
ui.add(new JButton("5,2 (2)"), gbc);
gbc.gridx = 12;
ui.add(new JButton("6,2 (2)"), gbc);
gbc.gridx = 14;
ui.add(new JButton("7,2 (2)"), gbc);
gbc.gridx = 16;
ui.add(new JButton("8,2 (2)"), gbc);
gbc.gridx = 18;
gbc.gridwidth = 4;
ui.add(new JButton("9,2 (4)"), gbc);
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 5;
ui.add(new JButton("1,3 (5)"), gbc);
gbc.gridx = 5;
gbc.gridwidth = 2;
ui.add(new JButton("2,3 (2)"), gbc);
gbc.gridx = 7;
ui.add(new JButton("3,3 (2)"), gbc);
gbc.gridx = 9;
ui.add(new JButton("4,3 (2)"), gbc);
gbc.gridx = 11;
ui.add(new JButton("5,3 (2)"), gbc);
gbc.gridx = 13;
ui.add(new JButton("6,3 (2)"), gbc);
gbc.gridx = 15;
ui.add(new JButton("7,3 (2)"), gbc);
gbc.gridx = 17;
ui.add(new JButton("8,3 (2)"), gbc);
gbc.gridx = 19;
gbc.gridwidth = 3;
ui.add(new JButton("9,3 (3)"), gbc);
gbc.gridx = 0;
gbc.gridy = 4;
gbc.gridwidth = 3;
ui.add(new JButton("1,4 (3)"), gbc);
gbc.gridx = 3;
ui.add(new JButton("2,4 (3)"), gbc);
gbc.gridx = 6;
gbc.gridwidth = 10;
ui.add(new JButton("3,4 (10)"), gbc);
gbc.gridx = 16;
gbc.gridwidth = 3;
ui.add(new JButton("4,4 (3)"), gbc);
gbc.gridx = 19;
ui.add(new JButton("5,4 (3)"), gbc);
gbc.gridx = 0;
gbc.gridy = 4;
gbc.gridwidth = 1;
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
for (int ii = 0; ii < 2; ii++) {
KeyBoardLayout o = new KeyBoardLayout(ii==0);
JFrame f = new JFrame("Keyboard Layout");
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
}
};
SwingUtilities.invokeLater(r);
}
}
I have to make this for school:
This is the code I have so far:
import javax.swing.*;
import java.awt.*;
public class AddressBookGui1 extends JFrame {
public AddressBookGui1(){
GridBagLayout gbl = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
setLayout(gbl);
JLabel label;
JButton button;
JTextField textField;
JTextArea textArea = new JTextArea(10, 20);
gbc.weightx = 1;
label = new JLabel("text");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
add(label ,gbc);
textField = new JTextField();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 0;
add(textField ,gbc);
label = new JLabel("text");
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.gridwidth = 1;
add(label ,gbc);
textField = new JTextField();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 2;
add(textField, gbc);
label = new JLabel("text");
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.gridwidth = 1;
add(label ,gbc);
textField = new JTextField();
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 1;
gbc.gridy = 2;
gbc.gridwidth = 2;
add(textField, gbc);
label = new JLabel("text");
gbc.weightx = 1;
gbc.anchor = GridBagConstraints.FIRST_LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 1;
add(label ,gbc);
gbc.weightx = 1;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridwidth = 2;
gbc.gridx = 1;
gbc.gridy = 3;
add(textArea, gbc);
gbc.weightx = 1;
button = new JButton("text");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 4;
add(button ,gbc);
gbc.weightx = 1;
button = new JButton("text");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 3;
gbc.gridy = 4;
add(button ,gbc);
}
public static void main(String[] args){
AddressBookGui1 frame = new AddressBookGui1();
frame.setTitle("Address Book");
frame.setSize(400, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
(I still need to deal with padding and insets. I've gotten those to work in a much simpler program so I think I have a handle on that stuff)
I've tried the GridBagLayout Oracle tutorial and I'm not sure what I'm doing wrong. Can someone help me make it look more like it is supposed to? Specifically to make the text fields and text area span over 2 cells.
Few things I noticed about your code.
Do not use setSize() of JFrame. This will cause abnormal behavior.
Instead, let the frame size itself according to the size of its
components. If you want the frame to be bigger, adjust not the size
of the frame but the components inside it. You can either
setpreferredSize or override the getpreferredsize of the component if
you really want to adjust is size since GridBagLayout is one of those layout managers that respects the
preferredSize of the component. Use pack() to remove the unecessary
space.
Do not extend a JFrame, make your UI class to have a main panel and
add all the components there. provide a getter for that panel (e.g.
getUI()) for extracting the UI of that class.
Always reinstantiate the GridBagConstraints object whenever it is
going to be applied to another component. This way it is more
readable.
Use insets to put padding around the component.
Do not reuse same reference to different components;
Use Initial Thread
This is not standard but it I find it really helpful when working
with GridBagLayout, in setting the constraints of gbc, make it in
alphabetical order.
To solve your problem, here is the modified code with the things I pointed out about applied.
public class AddressBook {
private JPanel pnlMain;
public AddressBook() {
pnlMain = new JPanel();
pnlMain.setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
JLabel lblName = new JLabel("Name");
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(0, 10, 0, 0);
gbc.weightx = 1;
pnlMain.add(lblName, gbc);
JTextField txtName = new JTextField();
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 3;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.insets = new Insets(5, 0, 0, 10);
gbc.weightx = 1;
pnlMain.add(txtName, gbc);
JLabel lblPhone = new JLabel("Phone");
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.insets = new Insets(0, 10, 0, 0);
gbc.weightx = 1;
pnlMain.add(lblPhone, gbc);
JTextField txtPhone = new JTextField();
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 3;
gbc.gridx = 1;
gbc.gridy = 1;
gbc.insets = new Insets(5, 0, 0, 10);
gbc.weightx = 1;
pnlMain.add(txtPhone, gbc);
JLabel lblEmail = new JLabel("Email");
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.insets = new Insets(0, 10, 0, 0);
gbc.weightx = 1;
pnlMain.add(lblEmail, gbc);
JTextField txtEmail = new JTextField();
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 3;
gbc.gridx = 1;
gbc.gridy = 2;
gbc.weightx = 1;
gbc.insets = new Insets(5, 0, 0, 10);
pnlMain.add(txtEmail, gbc);
JLabel lblAddress = new JLabel("Address");
gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 3;
gbc.insets = new Insets(0, 10, 0, 0);
gbc.weightx = 1;
pnlMain.add(lblAddress, gbc);
JTextArea txtAreaAddress = new JTextArea(10, 20);
JScrollPane pane = new JScrollPane(txtAreaAddress);
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.NORTH;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridwidth = 3;
gbc.gridx = 1;
gbc.gridy = 3;
gbc.insets = new Insets(5, 0, 0, 10);
gbc.weightx = 1;
pnlMain.add(pane, gbc);
JButton btnSave = new JButton("Save");
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.WEST;
gbc.fill = GridBagConstraints.NONE;
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = 4;
gbc.insets = new Insets(10, 10, 10, 0);
gbc.weightx = 1;
pnlMain.add(btnSave, gbc);
JButton btnCancel = new JButton("Cancel");
gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.EAST;
gbc.gridwidth = 1;
gbc.gridx = 3;
gbc.gridy = 4;
gbc.insets = new Insets(10, 0, 10, 10);
gbc.weightx = 1;
pnlMain.add(btnCancel, gbc);
}
public JPanel getUI(){
return pnlMain;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Address Book");
frame.getContentPane().add(new AddressBook().getUI());
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
});
}
}
gbc.gridwidth is the parameter that allows a component to span more than one column. For example, if you have 3 columns and 4 rows and you want a label to occupy the complete top row, then you need to assign the first cell for the label. and set gbc.gridwidth = 3;
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();
}
});
}
}