Reputation: 11025
I want to customize the height and width of the JTextField objects. I have tried with the setSize method, passing width and height as dimensions and as int as well. But none of them seems to work. Am I missing something, like some mandatory method call on the panel or something so that the size customization would be effective? Please help. Thanks in advance.
EDIT: Here is a bit of the code:
public class WestPanel extends JPanel{
private JLabel dateL;
private JTextField date;
public WestPanel(){
setBackground(Color.white);
setLayout(new GridLayout(1,2,0,0));
dateL=new JLabel("Date: ");
date=new JTextField("dd/mm/yyyy");
date.setSize(60,10);
add(dateL);
add(date);
//....remaining code....//
Upvotes: 1
Views: 26775
Reputation: 1028
JTextField can not be set size, infact, you should use a JTextArea instead.
Upvotes: 1
Reputation: 51445
I'm not sure this answers the original poster's questions, but hopefully it will be helpful to other Swing developers.
Most people want the labels and components to line up, like in the following dialog I created.
I use the Swing layout manager GridBagLayout to create this type of layout. Rather than lots of explanation, here's the code that created this dialog.
package com.ggl.business.planner.view;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import com.ggl.business.planner.model.BusinessPlannerModel;
import com.ggl.business.planner.view.extended.EscapeDialog;
import com.ggl.business.planner.view.extended.JFontChooser;
public class OptionsDialog {
protected static final Insets entryInsets = new Insets(0, 10, 4, 10);
protected static final Insets spaceInsets = new Insets(10, 10, 4, 10);
protected static final Insets noInsets = new Insets(0, 0, 0, 0);
protected static final Insets iconInsets = new Insets(0, 4, 0, 0);
protected BusinessPlannerFrame frame;
protected BusinessPlannerModel model;
protected EscapeDialog dialog;
protected JButton activityTextFontButton;
protected JButton connectorTextFontButton;
protected JSpinner borderSizeSpinner;
protected SpinnerNumberModel spinnerNumberModel;
protected boolean okPressed;
public OptionsDialog(BusinessPlannerModel model, BusinessPlannerFrame frame) {
this.model = model;
this.frame = frame;
createPartControl();
}
protected void createPartControl() {
dialog = new EscapeDialog();
dialog.setTitle("Business Planner Options");
dialog.setLayout(new GridBagLayout());
int gridy = 0;
gridy = createBorderFields(gridy);
gridy = createFontFields(gridy);
gridy = createButtonFields(gridy);
dialog.pack();
dialog.setBounds(dialogBounds());
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setVisible(true);
}
protected int createBorderFields(int gridy) {
JLabel borderSizeLabel = new JLabel("Border size:");
borderSizeLabel.setHorizontalAlignment(SwingConstants.LEFT);
addComponent(dialog, borderSizeLabel, 0, gridy, 1, 1, spaceInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
spinnerNumberModel = new SpinnerNumberModel(model.getActivityBorder(), 1, 5, 1);
borderSizeSpinner = new JSpinner(spinnerNumberModel);
addComponent(dialog, borderSizeSpinner, 1, gridy++, 4, 1, spaceInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
return gridy;
}
protected int createFontFields(int gridy) {
JLabel boxtextFontLabel = new JLabel("Activity text font:");
boxtextFontLabel.setHorizontalAlignment(SwingConstants.LEFT);
addComponent(dialog, boxtextFontLabel, 0, gridy, 1, 1, spaceInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
Font font = model.getActivityFont();
activityTextFontButton = new JButton(getFontText(font));
activityTextFontButton.setFont(font);
activityTextFontButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
JFontChooser fontChooser = new JFontChooser();
fontChooser.setSelectedFont(model.getActivityFont());
int result = fontChooser.showDialog(dialog);
if (result == JFontChooser.OK_OPTION) {
Font font = fontChooser.getSelectedFont();
String text = getFontText(font);
model.setActivityFont(font);
activityTextFontButton.setText(text);
activityTextFontButton.setFont(font);
JButton dummy = new JButton(text);
setButtonSizes(activityTextFontButton,
connectorTextFontButton, dummy);
dialog.validate();
dialog.pack();
}
}
});
addComponent(dialog, activityTextFontButton, 1, gridy++, 4, 1, spaceInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
JLabel connectortextFontLabel = new JLabel("Connector text font:");
connectortextFontLabel.setHorizontalAlignment(SwingConstants.LEFT);
addComponent(dialog, connectortextFontLabel, 0, gridy, 1, 1, spaceInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
font = model.getConnectorFont();
connectorTextFontButton = new JButton(getFontText(font));
connectorTextFontButton.setFont(font);
connectorTextFontButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
JFontChooser fontChooser = new JFontChooser();
fontChooser.setSelectedFont(model.getConnectorFont());
int result = fontChooser.showDialog(dialog);
if (result == JFontChooser.OK_OPTION) {
Font font = fontChooser.getSelectedFont();
String text = getFontText(font);
model.setConnectorFont(font);
connectorTextFontButton.setText(text);
connectorTextFontButton.setFont(font);
JButton dummy = new JButton(text);
setButtonSizes(activityTextFontButton,
connectorTextFontButton, dummy);
dialog.validate();
dialog.pack();
}
}
});
addComponent(dialog, connectorTextFontButton, 1, gridy++, 4, 1, spaceInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
setButtonSizes(activityTextFontButton, connectorTextFontButton);
return gridy;
}
protected String getFontText(Font font) {
StringBuilder builder = new StringBuilder();
builder.append(font.getName());
builder.append(", ");
builder.append(font.getSize());
builder.append(" points, ");
if (font.isPlain()) {
builder.append("plain");
} else if (font.isBold()) {
builder.append("bold ");
} else if (font.isItalic()) {
builder.append("italic");
}
return builder.toString();
}
protected int createButtonFields(int gridy) {
JPanel buttondrawingPanel = new JPanel();
buttondrawingPanel.setLayout(new FlowLayout());
JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
//TODO Add edits to make sure fields are filled correctly
setModel();
okPressed = true;
dialog.setVisible(false);
}
});
dialog.setOkButton(okButton);
buttondrawingPanel.add(okButton);
JButton cancelButton = new JButton("Cancel");
cancelButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent event) {
okPressed = false;
dialog.setVisible(false);
}
});
buttondrawingPanel.add(cancelButton);
setButtonSizes(okButton, cancelButton);
addComponent(dialog, buttondrawingPanel, 0, gridy++, 5, 1, spaceInsets,
GridBagConstraints.LINE_START, GridBagConstraints.HORIZONTAL);
return gridy;
}
protected void addComponent(Container container, Component component,
int gridx, int gridy, int gridwidth, int gridheight,
Insets insets, int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(gridx, gridy,
gridwidth, gridheight, 1.0D, 1.0D, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
protected void setButtonSizes(JButton ... buttons) {
Dimension preferredSize = new Dimension();
for (JButton button : buttons) {
Dimension d = button.getPreferredSize();
preferredSize = setLarger(preferredSize, d);
}
for (JButton button : buttons) {
button.setPreferredSize(preferredSize);
}
}
protected Dimension setLarger(Dimension a, Dimension b) {
Dimension d = new Dimension();
d.height = Math.max(a.height, b.height);
d.width = Math.max(a.width, b.width);
return d;
}
protected void setModel() {
model.setActivityBorder(spinnerNumberModel.getNumber().intValue());
}
protected Rectangle dialogBounds() {
int margin = 200;
Rectangle bounds = dialog.getBounds();
Rectangle f = frame.getFrame().getBounds();
bounds.x = f.x + margin;
bounds.y = f.y + margin;
return bounds;
}
public boolean isOkPressed() {
return okPressed;
}
}
The EscapeDialog class I extend just lets me use the Esc key to close the dialog, as if I left clicked on the Cancel button.
There are two things I'll make note of. The first is the addComponent method, which simplifies adding components to a GridBagLayout.
The second is the setButtonSizes method, which makes all of the button sizes uniform. Even though they are JButton components, and not JTextField components, you can do something similar if you want to make JTextField components the same size.
Upvotes: 5
Reputation: 168815
As suggested in comments, use size hints in the text field constructor, and an appropriate layout manager.
import java.awt.*;
import javax.swing.*;
public class WestPanel extends JPanel {
private JLabel dateL;
private JTextField date;
public WestPanel(){
setBackground(Color.white);
setLayout(new FlowLayout());
dateL=new JLabel("Date: ");
date=new JTextField("dd/mm/yyyy",6);
add(dateL);
add(date);
}
public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
JPanel gui = new JPanel(new BorderLayout());
gui.add(new WestPanel(), BorderLayout.LINE_START);
gui.setBackground(Color.ORANGE);
JOptionPane.showMessageDialog(null, gui);
}
};
SwingUtilities.invokeLater(r);
}
}
Upvotes: 3
Reputation: 32391
The setSize()
method only works when setting the layout manager to null.
Upvotes: 3
Reputation: 44240
Let the layout manager take care of the dimensions of your Swing components, but if you absolutely must, use setPreferredSize
in combination with a layout manager that respects that property.
Upvotes: 6
Reputation: 6590
Size of your components in Swing will depend on the type of layout manager you are using. If you want full control of UI, you can use a Freeflow layout.
Read the full story here: http://docs.oracle.com/javase/tutorial/uiswing/layout/using.html
Upvotes: 2