Skip to content

Instantly share code, notes, and snippets.

@vincenzopalazzo
Last active March 21, 2020 23:23
Show Gist options
  • Save vincenzopalazzo/a47e35ad4a4fdaf594710ee58097aba9 to your computer and use it in GitHub Desktop.
Save vincenzopalazzo/a47e35ad4a4fdaf594710ee58097aba9 to your computer and use it in GitHub Desktop.
JMARS_WITH_MATERIAL-UI_SWING_TABLE_CHANGELOG.md

Change log inside JMARS Beta 5.0.15 with material-ui-swing 1.1.1 official

JTable

This change require this other change

Inside the class ThemeTable you must change the configuration from this code

 UIManager.put("Table.alternateRowColor", this.isAlternaterowcolor());
UIManager.put("Table.alternateRowBackground", this.getAlternaterowbackground());

to this code

 UIManager.put("Table.alternateRowColor", this.getAlternaterowbackground());

Why? You can find the answer to the your question here

To correct work of this JTable configuration style, you should did this change

Now if you did all passage correclty you should be see the correct effect, inside the table with layer KRC.

ThemeProvider refactoring

The Swing L&F Work witht the ResurcesUI instances, you can find motivation here

The method getAlternaterowbackground() should return the ColorUIResources and not an Color because the Color is ignorate to Swing.

You can use the following code inside ThemeProvaider

public static Color parse(String input) {
        Pattern c = Pattern.compile("rgb *\\( *([0-9]+), *([0-9]+), *([0-9]+) *\\)");
        Matcher m = c.matcher(input);

        if (m.matches()) 
        {
            return new ColorUIResource(Integer.valueOf(m.group(1)),  // r
                             Integer.valueOf(m.group(2)),  // g
                             Integer.valueOf(m.group(3))); // b 
        }

        return null;  
    }

CraterTable Table with refactoring

Before this reafctoring the CraterTable used a personal renderer but shoult be instancent the new DefaultTableCellRenderer and not the TableCellRenderer because this last, is an interface and you shourd be implement all swing logic that is contains inside the DefaultTableCellRenderer

The new code is

TableCellRenderer diameterRenderer = new DefaultTableCellRenderer() {
   	
   		public Component getTableCellRendererComponent(JTable table, Object value,
   				boolean isSelected, boolean hasFocus, int row, int column) {
   			if (value instanceof String) {
   				setText(""+value);
   			}else{
   				double val = (Double)value;
   				if (val>500) {
   					setText(diamterFormatter.format(val / 1000.0) + " km");
   				} else {
   					setText(diamterFormatter.format(val) + " m");
   				}
   			}

   			return this;
   		}
   	};

Also, this the previus code you can lose some L&F personalizzations, like the text centered.

So for material library you shoul be create a new MaterialTableCellRenderer, but for refactoring the JMars code only one time, you should create a JMarsTableCellRenderer (This is a Template pattern), the code of the class is very, very basic

package edu.asu.jmars.ui.jmarscomponent;

import mdlaf.components.table.MaterialTableCellRenderer;

import javax.swing.*;
import java.awt.*;

public class JMarsTableCellRenderer extends MaterialTableCellRenderer {

  @Override
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
      //Insert here some common personalizing.
      super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
      return this;
  }
}

Now the final code of CraterTableCellRendere is the following:

TableCellRenderer diameterRenderer = new JMarsTableCellRenderer(){
			public Component getTableCellRendererComponent(JTable table, Object value,
														   boolean isSelected, boolean hasFocus, int row, int column) {
				super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
				if (value instanceof String) {
					setText(""+value);
				}else{
					double val = (Double)value;
					if (val>500) {
						setText(diamterFormatter.format(val / 1000.0) + " km");
					} else {
						setText(diamterFormatter.format(val) + " m");
					}
				}

				return this;
			}
		};

Why? because inside the Crater TableCellRendere you can call the super and you should be add the common personalizzation inside the JMarsTableCellRenderer without refactoring in all classes. It is cool, right? 😄

Material-UI-Swing version

The version for the last change inside this document you can found here, if you are testing this change should be download this version.

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