gpt4 book ai didi

java - 如何在 SwingX 中使用相对单元格突出显示?

转载 作者:太空宇宙 更新时间:2023-11-04 14:37:42 25 4
gpt4 key购买 nike

我想突出显示 org.jdesktop.swingx.JXTreeTable 中相对于内部值的单元格,如“突出显示(扩展)”下的 SwingX 展示中所示:

i.imgur.com/iw89kqn.png

展示中提供了一些代码,但我无法让它工作,它没有突出显示任何内容。我无法真正理解荧光笔在哪里绑定(bind)到值(也许提供的代码不完整)。而且也没有可用的文档。

这是展示中的代码:

这是我的代码:

TreeFrame.java

private void initialize( ProfileThread profileThread )
{
try
{
UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName() );
}
catch ( Exception e )
{
e.printStackTrace();
}

setBounds( 0, 0, 800, 600 );
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

setTitle( "ProfileLog" );

NoRootTreeTableModel model = new NoRootTreeTableModel(
profileThread.getProfileThreadElements(), this );
JXTreeTable treeTable = new JXTreeTable( model );

MattePainter painter = new MattePainter( PaintUtils.setAlpha( Color.RED, 125 ) );
highlighter = new RelativePainterHighlighter( painter );

highlighter.setHighlightPredicate( HighlightPredicate.ALWAYS );

treeTable.addHighlighter( highlighter );

JScrollPane treeView = new JScrollPane( treeTable );
getContentPane().add( treeView );

setVisible( true );
}

public void setCurrentDuration( long duration )
{
DurationRelativizer relativizer = createDurationRelativizer( duration );
highlighter.setRelativizer( relativizer );
}

/**
* Creates and returns a relativizer with the given intermediate value.
*
*/
private DurationRelativizer createDurationRelativizer( long duration )
{
return new DurationRelativizer( 0, true, 15000, duration );
}

public static class DurationRelativizer extends NumberRelativizer
{

public DurationRelativizer( int column, boolean spreadColumns, Number max, Number current )
{
super( column, spreadColumns, max, current );
}

@Override
protected Number getNumber( ComponentAdapter adapter )
{
if ( !( adapter.getValue( getValueColumn() ) instanceof ProfileThreadElement ) )
{
return null;
}
return ( (ProfileThreadElement) adapter.getValue( getValueColumn() ) ).getDuration();
}
}

我从示例中获取了相关代码片段,并根据我的要求进行了更改。我的RelativePainterHighlighter 类与展示中的完全相同。

这是我的 ProfileThreadElement 类:

ProfileThreadElement.java

import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

import javax.swing.tree.TreeNode;

public class ProfileThreadElement implements TreeNode
{
private Date start;
private Date end;
private long duration;
private String text;
private String result;

private ProfileThreadElement parent;
private List<ProfileThreadElement> children;

/**
* Instantiates a new profile thread element.
*
* @param timestamp the timestamp
* @param text the text
*/
public ProfileThreadElement( Date timestamp, String text )
{
super();
this.start = timestamp;
this.text = text;
this.children = new ArrayList<ProfileThreadElement>();
}

/**
* Close.
*
* @param timestamp the timestamp
* @param duration the duration
* @param result the result
*/
public void close( Date timestamp, long duration, String result )
{
this.end = timestamp;
this.duration = duration;
this.result = result;

}

/**
* Adds the children.
*
* @param child the child
*/
public void addChildren( ProfileThreadElement child )
{
// if ( children == null )
// {
// children = new ArrayList<ProfileThreadElement>();
// }

children.add( child );
child.setParent( this );
}

/**
* @param parent the parent to set
*/
void setParent( ProfileThreadElement parent )
{
this.parent = parent;
}

/**
* Checks if is open.
*
* @return true, if is open
*/
public boolean isOpen()
{
return end == null;
}

/**
* @return the start
*/
public Date getStart()
{
return start;
}

/**
* @return the end
*/
public Date getEnd()
{
return end;
}

/**
* @return the duration
*/
public long getDuration()
{
return duration;
}

/**
* @return the text
*/
public String getText()
{
return text;
}

/**
* @return the result
*/
public String getResult()
{
return result;
}

/**
* @return the parent
*/
public ProfileThreadElement getParent()
{
return parent;
}

/**
* @return the children
*/
public List<ProfileThreadElement> getChildren()
{
return children;
}

/*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#children()
*/
@Override
public Enumeration<ProfileThreadElement> children()
{
return new Enumeration<ProfileThreadElement>()
{
Iterator<ProfileThreadElement> iterator = children.iterator();

@Override
public boolean hasMoreElements()
{
return iterator.hasNext();
}

@Override
public ProfileThreadElement nextElement()
{
return iterator.next();
}
};
}

/*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#getAllowsChildren()
*/
@Override
public boolean getAllowsChildren()
{
return !children.isEmpty();
}

/*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#getChildAt(int)
*/
@Override
public TreeNode getChildAt( int childIndex )
{
return children.get( childIndex );
}

/*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#getChildCount()
*/
@Override
public int getChildCount()
{
return children.size();
}

/*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#getIndex(javax.swing.tree.TreeNode)
*/
@Override
public int getIndex( TreeNode node )
{
return children.indexOf( node );
}

/*
* (non-Javadoc)
*
* @see javax.swing.tree.TreeNode#isLeaf()
*/
@Override
public boolean isLeaf()
{
return children.isEmpty();
}

@Override
public String toString()
{
StringBuffer sb = new StringBuffer();

sb.append( duration );
sb.append( " ms " );
sb.append( text );

return sb.toString();
}
}

还有我的树表模型:

NoRootTreeTableModel.java

public class NoRootTreeTableModel extends AbstractTreeTableModel
{
private final static String[] COLUMN_NAMES = { "Duration (ms)", "Function Call", "Class" };

private List<ProfileThreadElement> profileThreadElements;
private TreeFrame frame;

public NoRootTreeTableModel( List<ProfileThreadElement> profileThreadElements, TreeFrame frame )
{
super( new Object() );
this.profileThreadElements = profileThreadElements;
this.frame = frame;
}

@Override
public int getColumnCount()
{
return COLUMN_NAMES.length;
}

@Override
public String getColumnName( int column )
{
return COLUMN_NAMES[column];
}

@Override
public boolean isCellEditable( Object node, int column )
{
return false;
}

@Override
public Object getValueAt( Object node, int column )
{
if ( node instanceof ProfileThreadElement )
{
Matcher matcher;
Pattern pattern;

ProfileThreadElement element = (ProfileThreadElement) node;

switch ( column )
{
case 0: // duration
frame.setCurrentDuration( element.getDuration() );
return element.getDuration();
case 1: // function
pattern = Pattern.compile( "\\.[a-z][a-zA-Z]+\\(.*\\)" );
matcher = pattern.matcher( element.getText() );
if ( matcher.find() )
return matcher.group().substring( 1 );
else
return "";
case 2: // class
pattern = Pattern.compile( ".+?(?=\\.[a-z][a-zA-Z]+\\(.*\\))" );
matcher = pattern.matcher( element.getText() );
if ( matcher.find() )
return matcher.group().replace( "class ", "" );
else
return "";
}
}
return null;
}

@Override
public int getChildCount( Object parent )
{
if ( parent instanceof ProfileThreadElement )
{
ProfileThreadElement element = (ProfileThreadElement) parent;
return element.getChildren().size();
}
return profileThreadElements.size();
}

@Override
public Object getChild( Object parent, int index )
{
if ( parent instanceof ProfileThreadElement )
{
ProfileThreadElement element = (ProfileThreadElement) parent;
return element.getChildren().get( index );
}
return profileThreadElements.get( index );
}

@Override
public int getIndexOfChild( Object parent, Object child )
{
ProfileThreadElement element = (ProfileThreadElement) parent;
return element.getChildren().indexOf( (ProfileThreadElement) child );
}
}

我认为最大的问题是我不知道在哪里调用 TreeFrame.setCurrentDuration(...) 方法。展示演示中缺少此内容。有人可以进行相对突出显示吗?

感谢任何帮助,提前致谢!

PS:如果我遗漏了什么或者您需要更多信息,请告诉我!

最佳答案

我已经删除了不需要的所有内容,并注意到 doHighlight(..) 从未被调用。原因是 canHighlight(..) 返回 false,因为我的相对器为空。

关于java - 如何在 SwingX 中使用相对单元格突出显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25361427/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com