下面列出了javax.swing.JPanel#getComponent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Evaluates simple expression during debugging session.
*/
public void evaluateExpression() throws IllegalAccessException, InvocationTargetException, InterruptedException, InvalidExpressionException {
TopComponentOperator variablesView = new TopComponentOperator(new ContainerOperator(MainWindowOperator.getDefault(), VIEW_CHOOSER), "Variables");
JToggleButtonOperator showEvaluationResultButton = new JToggleButtonOperator(variablesView, 0);
showEvaluationResultButton.clickMouse();
TopComponentOperator evaluationResultView = new TopComponentOperator("Evaluation Result");
new Action("Debug|Evaluate Expression...", null).perform();
TopComponentOperator expressionEvaluator = new TopComponentOperator("Evaluate Expression");
JEditorPaneOperator expressionEditor = new JEditorPaneOperator(expressionEvaluator);
new EventTool().waitNoEvent(1000);
expressionEditor.setText("\"If n is: \" + n + \", then n + 1 is: \" + (n + 1)");
JPanel buttonsPanel = (JPanel) expressionEvaluator.getComponent(2);
JButton expressionEvaluatorButton = (JButton) buttonsPanel.getComponent(1);
assertEquals("Evaluate code fragment (Ctrl + Enter)", expressionEvaluatorButton.getToolTipText());
expressionEvaluatorButton.doClick();
JTableOperator variablesTable = new JTableOperator(evaluationResultView);
assertValue(variablesTable, 0, 2, "\"If n is: 50, then n + 1 is: 51\"");
assertEquals("\"If n is: \" + n + \", then n + 1 is: \" + (n + 1)", variablesTable.getValueAt(0, 0).toString().trim());
}
protected void addListenersToComponent(JComponent comp) {
if (comp instanceof JTextComponent) {
JTextComponent textComp = (JTextComponent) comp;
textComp.getDocument().addDocumentListener(this);
}
if (comp instanceof JComboBox) {
JComboBox<?> comboComp = (JComboBox<?>) comp;
comboComp.addActionListener(this);
}
if (comp instanceof JCheckBox) {
JCheckBox checkComp = (JCheckBox) comp;
checkComp.addActionListener(this);
}
if (comp instanceof JPanel) {
JPanel panelComp = (JPanel) comp;
for (int i = 0; i < panelComp.getComponentCount(); i++) {
Component child = panelComp.getComponent(i);
if (!(child instanceof JComponent))
continue;
addListenersToComponent((JComponent) child);
}
}
}
@Override
public void mouseReleased(MouseEvent e) {
startPt = null;
// no dragging
if (!window.isVisible() || draggedOperation == null) {
return;
}
int addIndex = -1;
// get the index of the preview element
if (currentTargetPanel != null) {
JPanel operationsPanel = this.currentTargetPanel.getOperationsPanel();
for (int i = 0; i < operationsPanel.getComponentCount(); i++) {
Component comp = operationsPanel.getComponent(i);
if (comp.equals(this.panelPreview)) {
addIndex = i;
break;
}
}
// remove preview from panel
currentTargetPanel.removeComponent(this.panelPreview);
}
if (addIndex != -1) {
currentTargetPanel.addComponent(this.draggedOperation, addIndex);
}
this.draggedOperation = null;
prevRect = null;
this.startPt = null;
this.window.setVisible(false);
this.currentTargetPanel = null;
}
@Override
public void installUI( JComponent c ) {
// must get UI defaults here because installDefaults() is invoked after
// installComponents(), which uses these values to create popup button
padding = UIManager.getInsets( "ComboBox.padding" );
arrowType = UIManager.getString( "Component.arrowType" );
borderColor = UIManager.getColor( "Component.borderColor" );
disabledBorderColor = UIManager.getColor( "Component.disabledBorderColor" );
disabledBackground = UIManager.getColor( "ComboBox.disabledBackground" );
buttonBackground = UIManager.getColor( "ComboBox.buttonBackground" );
buttonArrowColor = UIManager.getColor( "ComboBox.buttonArrowColor" );
buttonDisabledArrowColor = UIManager.getColor( "ComboBox.buttonDisabledArrowColor" );
buttonHoverArrowColor = UIManager.getColor( "ComboBox.buttonHoverArrowColor" );
super.installUI( c );
LookAndFeel.installProperty( datePicker, "opaque", false );
// hack JXDatePicker.TodayPanel colors
// (there is no need to uninstall these changes because only UIResources are used,
// which are automatically replaced when switching LaF)
JPanel linkPanel = datePicker.getLinkPanel();
if( linkPanel instanceof JXPanel && linkPanel.getClass().getName().equals( "org.jdesktop.swingx.JXDatePicker$TodayPanel" ) ) {
((JXPanel)linkPanel).setBackgroundPainter( null );
linkPanel.setBackground( UIManager.getColor( "JXMonthView.background" ) );
if( linkPanel.getComponentCount() >= 1 && linkPanel.getComponent( 0 ) instanceof JXHyperlink ) {
JXHyperlink todayLink = (JXHyperlink) linkPanel.getComponent( 0 );
todayLink.setUnclickedColor( UIManager.getColor( "Hyperlink.linkColor" ) );
todayLink.setClickedColor( UIManager.getColor( "Hyperlink.visitedColor" ) );
}
}
}
@SuppressWarnings("unchecked")
public boolean open() {
JPanel panel = initializePanel();
JScrollPane scrollPane = (JScrollPane) panel.getComponent(0);
JTable table = (JTable) scrollPane.getViewport().getView();
if (JOptionPane.showConfirmDialog(null, panel, "Edit Array", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
list.clear();
for (int row = 0; row < table.getRowCount(); row++) {
Object o = table.getValueAt(row, 2);
list.add(o);
}
return true;
}
return false;
}
@Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
JPanel jPanel = (JPanel) super.getTableCellEditorComponent(table, value, isSelected, row, column); //To change body of generated methods, choose Tools | Templates.
TreeAsset treeAsset = tableModel.getElementAt(row);
JLabel jLabel = (JLabel) jPanel.getComponent(3);
jLabel.setIcon(treeAsset.getIcon());
return jPanel;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
JPanel jPanel = (JPanel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
TreeAsset treeAsset = tableModel.getElementAt(row);
JLabel jLabel = (JLabel) jPanel.getComponent(3);
jLabel.setIcon(treeAsset.getIcon());
return jPanel;
}
/**
* 重写父类方法,以实现对文件查看列表的额外设置.
* <p>
* 为什么要重写此方法,没有更好的方法吗?<br>
* 答:因父类的封装结构不佳,filePane是private私有,子类中无法直接引用,
* 要想对filePane中的文列表额外设置,目前重写本方法是个没有办法的方法.
* <p>
* sun.swing.FilePane源码可查看地址:<a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/swing/FilePane.java">Click here.</a>
*
* @param fc the fc
* @return the j panel
*/
protected JPanel createList(JFileChooser fc)
{
JPanel p = super.createList(fc);
//* 以下代码的作用就是将文件列表JList对象引用给找回来(通过从它的父面板中层层向下搜索)
//* ,因无法从父类中直接获得列表对象的直接引用,只能用此笨办法了
if(p.getComponentCount() > 0)
{
Component scollPane = p.getComponent(0);
if(scollPane != null && scollPane instanceof JScrollPane)
{
JViewport vp = ((JScrollPane)scollPane).getViewport();
if(vp != null)
{
Component fileListView = vp.getView();
//终于找到了文件列表的实例引用
if(fileListView != null && fileListView instanceof JList)
{
//把列表的行高改成-1(即自动计算列表每个单元的行高而不指定固定值)
//* 说明:在BeautyEye LNF中,为了便JList的UI更好看,在没有其它方法有前
//* 提下就在JList的BEListUI中给它设置了默写行高32,而JFildChooser中的
//* 文件列表将会因此而使得单元行高很大——从而导致文件列表很难看,此处就是恢复
//* 文件列表单元行高的自动计算,而非指定固定行高。
//*
//* 说明2:为什么不能利用list.getClientProperty("List.isFileList")从而在JList
//* 的ui中进行判断并区别对待是否是文件列表呢?
//* 答:因为"List.isFileList"是在BasicFileChooserUI中设置的,也就是说当为个属性被
//* 设置的时候JFileChooser中的文件列表已经实例化完成(包括它的ui初始化),所以此时
//* 如果在JList的ui中想区分是不可能的,因它还没有被调置,这个设置主要是供BasicListUI
//* 在被实例化完成后,来异步处理这个属性的(通过监听属性改变事件来实现的)
((JList)fileListView).setFixedCellHeight(-1);
}
}
}
}
return p;
}
/**
* 重写父类方法,以实现对文件查看列表的额外设置.
* <p>
* 为什么要重写此方法,没有更好的方法吗?<br>
* 答:因父类的封装结构不佳,filePane是private私有,子类中无法直接引用,
* 要想对filePane中的文列表额外设置,目前重写本方法是个没有办法的方法.
* <p>
* sun.swing.FilePane源码可查看地址:<a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/swing/FilePane.java">Click here.</a>
*
* @param fc the fc
* @return the j panel
*/
protected JPanel createList(JFileChooser fc)
{
JPanel p = super.createList(fc);
//* 以下代码的作用就是将文件列表JList对象引用给找回来(通过从它的父面板中层层向下搜索)
//* ,因无法从父类中直接获得列表对象的直接引用,只能用此笨办法了
if(p.getComponentCount() > 0)
{
Component scollPane = p.getComponent(0);
if(scollPane != null && scollPane instanceof JScrollPane)
{
JViewport vp = ((JScrollPane)scollPane).getViewport();
if(vp != null)
{
Component fileListView = vp.getView();
//终于找到了文件列表的实例引用
if(fileListView != null && fileListView instanceof JList)
{
//把列表的行高改成-1(即自动计算列表每个单元的行高而不指定固定值)
//* 说明:在BeautyEye LNF中,为了便JList的UI更好看,在没有其它方法有前
//* 提下就在JList的BEListUI中给它设置了默写行高32,而JFildChooser中的
//* 文件列表将会因此而使得单元行高很大——从而导致文件列表很难看,此处就是恢复
//* 文件列表单元行高的自动计算,而非指定固定行高。
//*
//* 说明2:为什么不能利用list.getClientProperty("List.isFileList")从而在JList
//* 的ui中进行判断并区别对待是否是文件列表呢?
//* 答:因为"List.isFileList"是在BasicFileChooserUI中设置的,也就是说当为个属性被
//* 设置的时候JFileChooser中的文件列表已经实例化完成(包括它的ui初始化),所以此时
//* 如果在JList的ui中想区分是不可能的,因它还没有被调置,这个设置主要是供BasicListUI
//* 在被实例化完成后,来异步处理这个属性的(通过监听属性改变事件来实现的)
((JList)fileListView).setFixedCellHeight(-1);
}
}
}
}
return p;
}
private OutputPanelPosition findOutputPanel(JTextComponent searched) {
for (int i = 0; i < getTabbedPane().getTabCount(); i++) {
Component tab = getTabbedPane().getComponentAt(i);
if (tab instanceof JPanel) {
JPanel parentPanel = (JPanel) tab;
for (int j = 0; j < parentPanel.getComponentCount(); j++) {
Component outputPanel = parentPanel.getComponent(j);
if (outputPanel.equals(searched.getParent().getParent())) {
return new OutputPanelPosition(i, j);
}
}
}
}
return null;
}
private ZapTextArea findZapTextArea(OutputPanelPosition position) {
Component currentTab = getTabbedPane().getComponentAt(position.getTabIndex());
if (currentTab instanceof JPanel) {
JPanel tabPanel = (JPanel) currentTab;
Component component = tabPanel.getComponent(position.getOutputPanelIndex());
if (component instanceof JScrollPane) {
JScrollPane scrollPane = (JScrollPane) component;
Component view = scrollPane.getViewport().getView();
if (view instanceof ZapTextArea) {
return (ZapTextArea) view;
}
}
}
return null;
}
private void addListenertoRTComp(JComponent comp) {
JPanel panelComp = (JPanel) comp;
for (int i = 0; i < panelComp.getComponentCount(); i++) {
Component child = panelComp.getComponent(i);
if (child instanceof JTextComponent) {
JTextComponent textComp = (JTextComponent) child;
textComp.getDocument().addDocumentListener(this);
}
}
}
protected static void addComponentToChooser(JFileChooser chooser, String toLabel, JComponent toAdd) {
// huge hack that only works with the Nimbus look and feel
// get the bottom panel and add the new component after the file type drop down
JPanel bottom = ((JPanel) chooser.getComponent(chooser.getComponentCount() - 1));
JPanel newPanel = new JPanel();
newPanel.setLayout(new BoxLayout(newPanel, BoxLayout.LINE_AXIS));
if (toLabel != null) {
// use the formatting of the combo box label in the component being added
// count - 1 => buttons
// count - 2 => filter filter combo box
JPanel filterPanel = (JPanel) bottom.getComponent(bottom.getComponentCount() - 2);
// count - 1 => combo box
// count - 2 => label
JLabel filterLabel = (JLabel) filterPanel.getComponent(filterPanel.getComponentCount() - 2);
JLabel label = new JLabel(toLabel);
label.setFont(filterLabel.getFont());
label.setBorder(filterLabel.getBorder());
label.setPreferredSize(filterLabel.getPreferredSize());
newPanel.add(label);
}
newPanel.add(toAdd);
bottom.add(newPanel, bottom.getComponentCount() - 1);
}
@Override
public void mouseDragged(MouseEvent e) {
Point pt = e.getPoint();
JComponent parent = (JComponent) e.getComponent();
// not yet dragging and motion > threshold
if (this.draggedOperation == null && startPt != null) {
double a = Math.pow(pt.x - startPt.x, 2);
double b = Math.pow(pt.y - startPt.y, 2);
if (Math.sqrt(a + b) > gestureMotionThreshold) {
this.draggedOperation = this.getDraggedOperation(startPt.x, startPt.y);
if (this.draggedOperation != null) {
startDragging(pt);
}
}
return;
}
// dragging, but no component was created
if (!window.isVisible() || draggedOperation == null) {
return;
}
pt = SwingUtilities.convertPoint(parent, e.getPoint(), this.target);
updateWindowLocation(pt, this.target);
Component targetLine = this.target.getComponentAt(pt);
// changed the target, remove the old preview
if (currentTargetPanel != null) {
if (targetLine == null || !targetLine.equals(currentTargetPanel)) {
this.currentTargetPanel.removeComponent(panelPreview);
this.currentTargetPanel = null;
}
}
// we have no valid target
if (targetLine == null || !(targetLine instanceof RecipeStepPanel)) {
return;
}
RecipeStepPanel targetPanel = (RecipeStepPanel) this.target.getComponentAt(pt);
this.currentTargetPanel = targetPanel;
JPanel operationsPanel = currentTargetPanel.getOperationsPanel();
pt = SwingUtilities.convertPoint(this.target, pt, operationsPanel);
if (prevRect != null && prevRect.contains(pt)) {
return;
}
boolean gotPreview = false;
for (int i = 0; i < operationsPanel.getComponentCount(); i++) {
Component comp = operationsPanel.getComponent(i);
Rectangle r = comp.getBounds();
// inside our gap, do nothing
if (Objects.equals(comp, panelPreview)) {
if (r.contains(pt)) {
return;
} else {
gotPreview = true;
continue;
}
}
int tgt;
if (!(comp instanceof Operation)) { //this is the dummy panel
int count = operationsPanel.getComponentCount();
tgt = count > 1 ? operationsPanel.getComponentCount() - 2 : 0;
} else {
tgt = getTargetIndex(r, pt, i, gotPreview);
}
if (tgt >= 0) {
addComponent(currentTargetPanel, panelPreview, tgt);
return;
}
}
}
@Test
public void testSetTitle() {
final String axisName = "Titel";
final AxisRangeControl control = new AxisRangeControl(axisName);
final JPanel rangeControlPanel = control.getPanel();
final Component component = rangeControlPanel.getComponent(0);
assertTrue(component instanceof TitledSeparator);
final TitledSeparator titledSeparator = (TitledSeparator) component;
final JLabel titleLabel = titledSeparator.getLabelComponent();
assertEquals(axisName, titleLabel.getText());
control.setTitleSuffix("radiance_3");
assertEquals(axisName + " (radiance_3)", titleLabel.getText());
control.setTitleSuffix("");
assertEquals(axisName, titleLabel.getText());
control.setTitleSuffix("radiance_5");
assertEquals(axisName + " (radiance_5)", titleLabel.getText());
control.setTitleSuffix(null);
assertEquals(axisName, titleLabel.getText());
}