Avatar of deleyd
deleyd
Flag for United States of America asked on

How to refactor these two nearly idential classes?

I have two classes with a lot of identical code. One class extends JLabel and the other class extends JTextField.

Normally I'd push the identical code up to a superclass. However in this case I can't do that because I don't control those classes.

Is there an alternative way to properly refactor out the duplicate code here?

ViewLabel
package Screen;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.border.Border;


public class ViewLabel extends JLabel implements FocusListener {

	private static final long serialVersionUID = 1L;

	//CONSTRUCTOR
	public ViewLabel() {
		this("                       ");
	}
	public ViewLabel(String s) {
		super(s);
		this.setBorder(null);
		this.setOpaque(true);
		this.setForeground(Color.BLACK);
		this.setBackground(Color.WHITE);
		Border border = BorderFactory.createLineBorder(Color.BLUE, 5);
		this.setBorder(border);
		//this.setBorder(null);  //DWD set border for testing on/off
		this.addFocusListener(this);
	}

	public void setFieldHighlighting(Color foreground, Color background) {
		this.setForeground(foreground);
		this.setBackground(background);		
	}
	public void highlightField() {
        setFieldHighlighting(Color.WHITE, Color.BLACK);		
	}
	public void unhighlightField() {
		setFieldHighlighting(Color.BLACK, Color.WHITE);
	}


	@Override
	public void focusGained(FocusEvent e) {
        highlightField();
	}

	@Override
	public void focusLost(FocusEvent e) {
		if (!e.isTemporary()) {       //a temporary loss is when we change windows, in which case we don't want to unhighlight the line.
            unhighlightField();
		}
	}

}

Open in new window

VIewTextField
package Screen;

import java.awt.Color;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.BorderFactory;
import javax.swing.JTextField;
import javax.swing.border.Border;


public class ViewTextField extends JTextField implements FocusListener {

	private static final long serialVersionUID = 1L;
	
	//CONSTRUCTOR
	public ViewTextField() {
		this("                       ");
	}
	public ViewTextField(String s) {
		super(s);
		this.setBorder(null);
		this.setOpaque(true);
		this.setForeground(Color.BLACK);
		this.setBackground(Color.WHITE);
		Border border = BorderFactory.createLineBorder(Color.BLUE, 5);
		this.setBorder(border);
		//this.setBorder(null);  //DWD set border for testing on/off
		this.addFocusListener(this);
	}

	public void setFieldHighlighting(Color foreground, Color background) {
		this.setForeground(foreground);
		this.setBackground(background);		
	}
	public void highlightField() {
        setFieldHighlighting(Color.WHITE, Color.BLACK);		
	}
	public void unhighlightField() {
		setFieldHighlighting(Color.BLACK, Color.WHITE);
	}


	@Override
	public void focusGained(FocusEvent e) {
        highlightField();
	}

	@Override
	public void focusLost(FocusEvent e) {
		if (!e.isTemporary()) {       //a temporary loss is when we change windows, in which case we don't want to unhighlight the line.
            unhighlightField();
		}
	}
	
}

Open in new window

Java

Avatar of undefined
Last Comment
dpearson

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
dpearson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck