swing Using Swing for Graphical User Interfaces Setting Parameters for Components


Example

Components have various parameters that can be set for them. They vary from component to component, so a good way to see what parameters can be set for components is to start typing componentName.set, and let your IDE's autocomplete (If you use an IDE) suggest methods. The default shortcut in many IDEs, if it doesn't show up automatically, is CTRL + Space.

Autocomplete in IntelliJ IDEA

Common parameters that are shared between all components

DescriptionMethod
Sets the smallest size that the component can be (only if the layout manager honors the minimumSize property)setMinimumSize(Dimension minimumSize)
Sets the biggest size that the component can be (only if the layout manager honors the maximumSize property)setMaximumSize(Dimension maximumSize)
Sets the perferred size of the component (only if the layout manager honors the preferredSize property)setPreferredSize(Dimension preferredSize)
Shows or hides the componentsetVisible(boolean aFlag)
Sets whether the component should respond to user inputsetEnabled(boolean enabled)
Sets the font of textsetFont(Font font)
Sets the text of the tooltipsetToolTipText(String text)
Sets the Backgroundcolor of the componentsetBackground(Color bg)
Sets the Foregroundcolor (font color) of the componentsetForeground(Color bg)

Common parameters in other components

Common ComponentsDescriptionMethod
JLabel, JButton, JCheckBox, JRadioButton, JToggleButton, JMenu, JMenuItem, JTextArea, JTextFieldSets the text displayedsetText(String text)
JProgressBar, JScrollBar, JSlider, JSpinnerSet's a numerical value between the component's min and max valuessetValue(int n)
JProgressBar, JScrollBar, JSlider, JSpinnerSet's the smallest possible value that the value property can besetMinimum(int n)
JProgressBar, JScrollBar, JSlider, JSpinnerSet's the biggest possible value that the value property can besetMaxmimum(int n)
JCheckBox, JToggleBoxSet's whether the value is true or false (Eg: Should a checkbox be checked?)setSelected(boolean b)