Java Utililty Methods JComponent Properties

List of utility methods to do JComponent Properties

Description

The list of methods to do JComponent Properties are organized into topic(s).

Method

JPanelbuildComponentPanel(JComponent component, int hgap, int vgap, boolean isOpaque)
Adds the specified JComponent to a JPanel with a left flow layout.
JPanel p = new JPanel();
if (component == null)
    return p;
if (hgap < 0)
    hgap = 0;
if (vgap < 0)
    vgap = 0;
p.setLayout(new FlowLayout(FlowLayout.LEFT, hgap, vgap));
...
JPanelbuildComponentPanelRight(JComponent component, int hgap, int vgap, boolean isOpaque)
Adds the specified JComponent to a JPanel with a right flow layout.
JPanel p = new JPanel();
if (component == null)
    return p;
if (hgap < 0)
    hgap = 0;
if (vgap < 0)
    vgap = 0;
p.setLayout(new FlowLayout(FlowLayout.RIGHT, hgap, vgap));
...
voiddisableAll(JComponent component)
disable All
component.setEnabled(false);
for (Component cmp : component.getComponents()) {
    if (cmp instanceof JComponent) {
        disableAll((JComponent) cmp);
voiddisposeParentWindow(JComponent component)
dispose Parent Window
Window window = SwingUtilities.windowForComponent(component);
if (window != null)
    window.dispose();
voiddoDisable(JComponent component)
do Disable
if (component.isEnabled())
    component.setEnabled(false);
MapgetProperties(JComponent component)
Convenience method for obtaining most non-null human readable properties of a JComponent.
Map<Object, Object> retVal = new HashMap<Object, Object>();
Class<?> clazz = component.getClass();
Method[] methods = clazz.getMethods();
Object value = null;
for (Method method : methods) {
    if (method.getName().matches("^(is|get).*") && method.getParameterTypes().length == 0) {
        try {
            Class returnType = method.getReturnType();
...
MapgetProperties(JComponent component)
Convenience method for obtaining most non-null human readable properties of a JComponent.
Class<?> clazz = component.getClass();
Method[] methods = clazz.getMethods();
Map<Object, Object> retVal = new HashMap<>(methods.length);
Object value = null;
for (Method method : methods) {
    if (method.getName().matches("^(is|get).*") && (method.getParameterTypes().length == 0)) {
        try {
            Class<?> returnType = method.getReturnType();
...
MapgetProperties(JComponent component)
Convenience method for obtaining most non-null human readable properties of a JComponent.
Map<Object, Object> retVal = new HashMap<>();
Class<?> clazz = component.getClass();
Method[] methods = clazz.getMethods();
Object value = null;
for (Method method : methods) {
    if (method.getName().matches("^(is|get).*") && method.getParameterTypes().length == 0) {
        try {
            Class<?> returnType = method.getReturnType();
...
RectanglegetVisibleBoundsOnScreen(JComponent component)
Returns the specified component's visible bounds within the screen.
Rectangle visibleRect = component.getVisibleRect();
Point onScreen = visibleRect.getLocation();
SwingUtilities.convertPointToScreen(onScreen, component);
visibleRect.setLocation(onScreen);
return visibleRect;
voidinvert(JComponent c)

Makes a Swing component inverted by swapping its foreground and background colors.

Color invBackground = c.getForeground();
Color invForeground = c.getBackground();
c.setBackground(invBackground);
c.setForeground(invForeground);