Site Sponsors:
Java: Custom / Owner Drawn Listbox Woes 
In Java, many things are difficult. Some things are easy.

Interestingly, sometimes the difficult things seem to work better than the simple things!

Take, for example, the need to draw a custom listbox. Unlike much of the rendering framework, the task starts out deceptively simple. To render a listbox item differently, all we need do is to create our own rendition of a ListCellRenderer:


public class PrettyList01 implements ListCellRenderer {
}


Once created, our interface need only return a JLabel from a simple member function getListCellRendererComponent. --In fact, getListCellRendererComponent even has a method to create a JLabel for us.

While everything works swimmingly whilst working with text, things get a bit odd when working with the JLabelian setIcon method:



It seems that - when using the Oracle / Sun JDK (as tested on both Windows on Linux) 'Labelian animation will only work when the background color for the listbox, not the label, is changed. Once changed, 'Labelian animation woks okay - but there is a flaky caveat: When we select the LAST item in the list, our hacked-in color-munge becomes the default color for the entire box (sigh).

If we DON'T change the default list-background color, there are no problems - except that the icons will no longer work!

Here is the code:


package pretty_list01;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;

/**
* Sunday Morning Research Session.
*
* @author profnagy
*/
public class PrettyList01 implements ListCellRenderer {

private TitledBorder focused = null;
private Border unFocused = new EmptyBorder(15, 1, 1, 1);
private DefaultListCellRenderer rend = new DefaultListCellRenderer();
private Font zFontSel = null;
private Color clrBack = null;
private Color clrFore = null;
private Color clrTitle = null;
private Icon iconSel = null;
private Icon icon = null;

public PrettyList01(Color clrFore, Color clrBack, Icon icon, Icon iconSel, String sTitle) {
this.clrFore = clrFore;
this.clrBack = clrBack;
this.clrTitle = clrBack;
focused = new TitledBorder(LineBorder.createBlackLineBorder(), sTitle);
this.focused.setTitleColor(clrTitle);
java.net.URL url = null;
Class loader = getClass();
this.icon = icon;
this.iconSel = iconSel;
}

public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel render = (JLabel) rend.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (cellHasFocus) {
if (zFontSel == null) {
Font f1 = list.getFont();
zFontSel = new Font(f1.getFontName(), f1.getStyle(), f1.getSize() - 5);
}
render.setFont(zFontSel);
render.setBorder(focused);
render.setIcon(iconSel);
render.setBackground(clrFore);
render.setForeground(Color.red);
// Oracle JDK 1.x - 1.7 - Ubuntu
// ==========
// Phew! -If we keep the color the same, ths icons will NOT animate.
// Yet if we change the color, the LAST item in the list will color badly!
list.setBackground(Color.white);
} else {
render.setBorder(unFocused);
render.setIcon(icon);
render.setBackground(clrBack);
render.setForeground(clrFore);
list.setBackground(render.getBackground());
}
render.setIconTextGap(20);
return render;
}
}


Feel free to download the project.

Cheers!

-Rn





[ view entry ] ( 900 views )   |  permalink  |  related link
Publishing VB.NET Training on YouTube 
"Designed for the total beginner, this VB.NET Boot Camp is the first segment of a multi-part presentation. In this first session, we will discover how to write Visual Basic .NET (VB.NET) Programs from the console - other tools not required!

(video)

Targeted to help new software developers get started writing their own software, this first session also includes a light overview of the software development and testing process."

Enjoy,

-Rn




[ view entry ] ( 1655 views )   |  permalink  |  related link
UMM02: Sharing Ideas on Project Planning & Management  
Here is the second and closing session (after UMM01.) This final session includes discussions around high, medium, and low-level best software design and project definition / team building practices. -Ideas leading to an introductory review of the most commonly used UML Diagram types.

While I do indeed plan to make some changes, for the moment you can view the video on YouTube.

[ view entry ] ( 2175 views )   |  permalink  |  related link

<<First <Back | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | Next> Last>>