Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vincenzopalazzo/5f6b548a2f65c3f299f9a54818b6082c to your computer and use it in GitHub Desktop.
Save vincenzopalazzo/5f6b548a2f65c3f299f9a54818b6082c to your computer and use it in GitHub Desktop.
Material-UI-Swing pre-release 6.1 description with material table style refactored.

Material-UI-Swing pre-release 6.1

Introduction

This version includes the complete refactoring to JButton and restored all change inside the JTable (the code of the table UI is equal to code the version 1.1.1_beta material-ui-swing version).

MaterialButtonUI Refactoring

The old version of MaterialButtonUI had a bad code inside the implementation and this caused the bad performance with a complex client application, how JMars, an example mouse hover event on Jbutton worked bad or ware very slow. The version 1.1.1 pre-release 6.1 (over to have an ugly name 😄) introduce a big refactoring inside the component UI, and change/simplifies the API of JButton if you want to create a personal UI.

as well as to disabled the mouse hovers on the Jbutton, you can disable the focus (borders dashed) and the button pressed effect.

With order.

Disabled mouse hover with personalUI

to paint the button with no mouse hover effect you can use this code, described in vincenzopalazzo/material-ui-swing issues.

Disabled borders dashed

to disabled border dashed there are two possible methods, like:

  • set the button not focusable or with button.setFocusable(false);
  • create a personal implementation of the method paintFoucs, like the following code
 @Override
    protected void paintFocus(Graphics g, AbstractButton b, Rectangle viewRect, Rectangle textRect, Rectangle iconRect) {
        paintBorderButton(g, b);
        
    }

Disable button pressed effect

To disabled button pressed effect you can override inside the personal UI component the method called paintButtonPressed with code like the following:

@Override
    protected void paintButtonPressed(Graphics g, AbstractButton b) {
        g.setColor(background);
        g.fillRoundRect(0, 0, b.getWidth(), b.getHeight(), arch, arch);
        paintBorderButton(g, b);
    }

Refactoring JTable

In the material-ui-swing 1.1.1 pre-release6.1 include refactoring to JTable, before this update the proprieties inside UIDefault was:

		UIManager.put("Table.alternateRowColor", true);
		UiManager.put("Table.alternateRowBackground", Color.RED);

and inside the MaterialTableCellRenderer the code was:

public class MaterialTableCellRenderer extends DefaultTableCellRenderer {

	@Override
	public Component getTableCellRendererComponent (JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
		JComponent component = (JComponent) super.getTableCellRendererComponent (table, value, isSelected, hasFocus, row, column);
		// hides yellow selection highlight
		this.setHorizontalAlignment (SwingConstants.CENTER);
		this.setVerticalAlignment (SwingConstants.CENTER);

		if(value instanceof Boolean){
			TableCellRenderer renderer = new MaterialTableCellRendererCheckBox();
			return renderer.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
		}

		boolean alternativeRow = UIManager.getBoolean("Table.alternateRowColor");
		Color alternativeRowColor = UIManager.getColor("Table.alternateRowBackground");
		Color normalColor = UIManager.getColor("Table.background");
		if(alternativeRow){
			if(!isSelected){
				if(row%2 == 1) {
					component.setBackground(alternativeRowColor);
					setDefaultCellRenderWithAllType(table, value, isSelected, hasFocus, row, column, alternativeRowColor);
				}else{
					component.setBackground(normalColor);
					setDefaultCellRenderWithAllType(table, value, isSelected, hasFocus, row, column, normalColor);
				}
				component.setForeground(table.getForeground());
			}else {
				component.setForeground(table.getSelectionForeground());
			}
		}
		return component;
	}

	// This method sets a MaterialCellRender at the particular class
	// With this class not working correctly the color alternate in the Jtable
	// in particular the IconImage without this code the cell is painted not correctly or
	// in the cell did print the path of the image

	protected void setDefaultCellRenderWithAllType(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column, Color color) {
		if(table == null){
			throw new IllegalArgumentException("Table is null");
		}

		Component component = table.getDefaultRenderer(ImageIcon.class).getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
		component.setBackground(color);
	}
}

Now the code is refactorized and the UIManager proprieties are changed in

table.put("Table.alternateRowColor", Color.RED);

Now the paint row is managed to DefaultTableCellRenderer and if you create the PersonalTableCellRenderer, you can extend the API class (DefaultTableCellRenderer) and not MaterialTableCellRenderer

Conclusion

Now the implementation of JButton is a lot of personalizable but it is possible to increase the modularization of the component MaterialButtonUI. For the moment the version remains this because another refactoring can introduce a few bugs inside the component behavior

Other changes are described inside the CHANGELOG file

Document License

Sponsors this work

To support my work in this version you can use

Donation Custom badge

or

Github donations

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment