下面列出了javax.swing.JComponent#getComponent ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Temporarily massage this component so it is visible, enabled, unselected,
* unfocused, etc.
*/
private void storeState(JComponent c) {
if (c instanceof AbstractButton) {
AbstractButton b = (AbstractButton) c;
b.putClientProperty(WAS_SELECTED, new Boolean(b.isSelected()));
b.putClientProperty(WAS_FOCUS_PAINTED, new Boolean(b.isSelected()));
b.setSelected(false);
b.setFocusPainted(false);
}
if (c.isEnabled() == false) {
c.putClientProperty(WAS_ENABLED, new Boolean(c.isEnabled()));
c.setEnabled(true);
}
if (c.isVisible() == false) {
c.putClientProperty(WAS_VISIBLE, new Boolean(c.isVisible()));
c.setVisible(true);
}
for (int a = 0; a < c.getComponentCount(); a++) {
if (c.getComponent(a) instanceof JComponent) {
storeState((JComponent) c.getComponent(a));
}
}
}
/**
* 重写该方法,设置内容显示区域为不完全透明
*/
@Override
protected Container createMessageArea()
{
JComponent messageArea = (JComponent) super.createMessageArea();
messageArea.setOpaque(false);
JComponent realBody = (JComponent) messageArea.getComponent(0);
realBody.setOpaque(false);
for(Component child : realBody.getComponents())
{
((JComponent)child).setOpaque(false);
}
return messageArea;
}
private static void setEnabledRecursive(final JComponent component, final boolean enabled) {
component.setEnabled(enabled);
final int count = component.getComponentCount();
for (int i = 0; i < count; i++) {
final Component child = component.getComponent(i);
if (child instanceof JComponent) {
final JComponent jchild = (JComponent) child;
if (enabled) {
final Boolean nestedEnabling = (Boolean) jchild.getClientProperty(NESTED_ENABLING);
if ((nestedEnabling == null) || nestedEnabling.booleanValue()) {
setEnabledRecursive(jchild, true);
}
} else {
setEnabledRecursive(jchild, false);
}
}
}
}
private void transparentSpinner(JSpinner spTo2) {
JComponent c = spTo2.getEditor();
for (int i = 0; i < c.getComponentCount(); i++) {
Component ct = c.getComponent(i);
if (ct instanceof JTextField) {
ct.setForeground(Color.WHITE);
ct.setBackground(ColorResource.getDarkBtnColor());
}
}
spTo2.setForeground(Color.WHITE);
spTo2.setBackground(ColorResource.getDarkBgColor());
spTo2.setBorder(null);
}
private void drawOpenSpotAtEndOfMenuBar(Graphics2D g2, JComponent mb) {
Point mblocation = SwingUtilities.convertPoint(mb, new Point(0,0), this);
if(mb.getComponentCount() > 0) {
Component lastComp = mb.getComponent(mb.getComponentCount()-1);
mblocation.x += lastComp.getX() + lastComp.getWidth();
}
g2.drawRect(mblocation.x+2, mblocation.y+2, mb.getHeight()-4, mb.getHeight()-4);
}
private void clearPopupMenu(JComponent component) {
component.setComponentPopupMenu(null);
for (int i = 0; i < component.getComponentCount(); i++) {
Component c = component.getComponent(i);
if (c instanceof JComponent)
clearPopupMenu((JComponent) c);
}
}
private void clearPopupMenu(JComponent component) {
component.setComponentPopupMenu(null);
for (int i = 0; i < component.getComponentCount(); i++) {
Component c = component.getComponent(i);
if (c instanceof JComponent)
clearPopupMenu((JComponent) c);
}
}
/**
* Removes previously bounded {@link ISettingChangeListener}s from the specified component, and also from all of its child components recursively.
*
* <p>
* Implementation simply clears the client property to which the bounded setting change listener was stored for to trigger the previously registered
* property change listener to take care of the rest.
* </p>
*
* @param container container of components whose bounded setting change listener to remove
*
* @see #addBindExecuteScl(ISettingChangeListener, ISettingsBean, Set, JComponent)
*/
public static void removeAllBoundedScl( final JComponent container ) {
// Just for the short name...
final JComponent c = container;
if ( c.getClientProperty( PROP_BOUNDED_SCL_LIST ) != null )
c.putClientProperty( PROP_BOUNDED_SCL_LIST, null );
for ( int i = c.getComponentCount() - 1; i >= 0; i-- ) {
final Component comp = c.getComponent( i );
if ( comp instanceof JComponent )
removeAllBoundedScl( (JComponent) comp );
}
}
public DataViewComponent.MasterView getMasterView() {
try {
JComponent memoryDiffPanel = (JComponent)sdw.getComponent(0);
memoryDiffPanel.setOpaque(false);
final JToolBar toolBar = (JToolBar)memoryDiffPanel.getComponent(1);
toolBar.setOpaque(false);
((JComponent)toolBar.getComponent(0)).setOpaque(false);
((JComponent)toolBar.getComponent(1)).setOpaque(false);
((JComponent)toolBar.getComponent(3)).setOpaque(false);
((JComponent)toolBar.getComponent(4)).setOpaque(false);
((JComponent)toolBar.getComponent(5)).setOpaque(false);
JPanel toolbarSpacer = new JPanel(null) {
public Dimension getPreferredSize() {
if (UISupport.isGTKLookAndFeel() || UISupport.isNimbusLookAndFeel()) {
int currentWidth = toolBar.getSize().width;
int minimumWidth = toolBar.getMinimumSize().width;
int extraWidth = currentWidth - minimumWidth;
return new Dimension(Math.max(extraWidth, 0), 0);
} else {
return super.getPreferredSize();
}
}
};
toolbarSpacer.setOpaque(false);
Component descriptionLabel = toolBar.getComponent(7);
toolBar.remove(descriptionLabel);
toolBar.remove(6);
toolBar.add(toolbarSpacer);
toolBar.add(descriptionLabel);
} catch (Exception e) {}
sdw.setPreferredSize(new Dimension(1, 1));
SnapshotDiffContainer snapshotDiff = (SnapshotDiffContainer)getDataSource();
String caption = NbBundle.getMessage(SnapshotDiffView.class, "DESCR_Snapshots_Comparison", // NOI18N
new Object[] { DataSourceDescriptorFactory.getDescriptor(snapshotDiff.getSnapshot1()).getName(),
DataSourceDescriptorFactory.getDescriptor(snapshotDiff.getSnapshot2()).getName()});
return new DataViewComponent.MasterView(caption, null, sdw); // NOI18N
}
private void registerChildren(JComponent c) {
String key = "animatedLayout.propertyListener";
for (int a = 0; a < c.getComponentCount(); a++) {
JComponent child = (JComponent) c.getComponent(a);
if (child.getClientProperty(key) == null) {
child.putClientProperty(key, destinationListener);
child.addPropertyChangeListener(PROPERTY_DESTINATION,
destinationListener);
}
}
}
private List<String> getKeywords(JComponent jc) {
List<String> returnValue = new ArrayList<>();
if (jc instanceof LazyDemoPanel) {
LazyDemoPanel ldp = (LazyDemoPanel) jc;
ShowcaseDemo d = ldp.getShowcaseDemo();
if (d.getKeywords() == null)
throw new NullPointerException(ldp.demoClassName);
for (String keyword : d.getKeywords()) {
returnValue.add(keyword.toLowerCase());
}
Class[] classes = d.getClasses();
if (classes == null)
throw new NullPointerException(
d.getClass().getSimpleName());
for (Class z : classes) {
int layer = 0;
/*
* Include at least 4 layers: CircularProgressBarUI ->
* BasicProgressBarUI -> ProgressBarUI -> ComponentUI
*/
while (z != null && !z.equals(Object.class) && layer < 4) {
returnValue.add(z.getSimpleName().toLowerCase());
returnValue.add(z.getName().toLowerCase());
z = z.getSuperclass();
layer++;
}
}
}
for (int a = 0; a < jc.getComponentCount(); a++) {
if (jc.getComponent(a) instanceof JComponent)
returnValue.addAll(
getKeywords((JComponent) jc.getComponent(a)));
}
return returnValue;
}
private static void getCheckBoxes(JComponent jc, List<JCheckBox> list) {
if (jc instanceof JCheckBox) {
list.add((JCheckBox) jc);
}
for (int a = 0; a < jc.getComponentCount(); a++) {
if (jc.getComponent(a) instanceof JComponent) {
getCheckBoxes((JComponent) jc.getComponent(a), list);
}
}
}
/** Restore this component back to its original goodness. */
private void restoreState(JComponent c) {
if (c instanceof AbstractButton) {
AbstractButton b = (AbstractButton) c;
if (b.getClientProperty(WAS_SELECTED) != null) {
b.setSelected(((Boolean) b.getClientProperty(WAS_SELECTED))
.booleanValue());
b.putClientProperty(WAS_SELECTED, null);
}
if (b.getClientProperty(WAS_FOCUS_PAINTED) != null) {
b.setFocusPainted(((Boolean) b
.getClientProperty(WAS_FOCUS_PAINTED)).booleanValue());
b.putClientProperty(WAS_FOCUS_PAINTED, null);
}
}
if (c.getClientProperty(WAS_ENABLED) != null) {
c.setEnabled(((Boolean) c.getClientProperty(WAS_ENABLED))
.booleanValue());
c.putClientProperty(WAS_ENABLED, null);
}
if (c.getClientProperty(WAS_VISIBLE) != null) {
c.setVisible(((Boolean) c.getClientProperty(WAS_VISIBLE))
.booleanValue());
c.putClientProperty(WAS_VISIBLE, null);
}
for (int a = 0; a < c.getComponentCount(); a++) {
if (c.getComponent(a) instanceof JComponent) {
restoreState((JComponent) c.getComponent(a));
}
}
}
private static void setOpaque(JComponent jc, boolean opaque) {
if (jc instanceof JTextField)
return;
jc.setOpaque(false);
if (jc instanceof JSpinner)
return;
for (int a = 0; a < jc.getComponentCount(); a++) {
JComponent child = (JComponent) jc.getComponent(a);
setOpaque(child, opaque);
}
}
@Override
protected Collection<JComponent> getEmphasizedComponents(
JComponent container) {
Collection<JComponent> returnValue = super
.getEmphasizedComponents(container);
if (returnValue.isEmpty()) {
// when the super has nothing to emphasize, try to emphasize
// the last thing the user touched. For ex: if they rolled
// the mouse over a label and then left the container, keep
// the last thing they rolled over emphasized, just for
// continuity.
List<JComponent> lastEmphasized = (List<JComponent>) container
.getClientProperty(PROPERTY_LAST_EMPHASIZED);
if (lastEmphasized != null) {
returnValue = new ArrayList<>();
List children = Arrays.asList(container.getComponents());
for (JComponent jc : lastEmphasized) {
if (children.contains(jc)) {
returnValue.add(jc);
}
}
}
// if that failed, grab the last node in the path.
if (returnValue.isEmpty() && container.getComponentCount() > 0) {
JComponent last = (JComponent) container
.getComponent(container.getComponentCount() - 1);
returnValue = Arrays.asList(last);
}
}
container.putClientProperty(PROPERTY_LAST_EMPHASIZED,
new ArrayList<>(returnValue));
return returnValue;
}
/** Instantiates a new value spinner. */
public IntegerSpinner(int min, int max, int stepSize) {
SpinnerNumberModel model = new SpinnerNumberModel(min, min, max, stepSize);
setModel(model);
JComponent comp = getEditor();
final JFormattedTextField field = (JFormattedTextField) comp.getComponent(0);
DefaultFormatter formatter = (DefaultFormatter) field.getFormatter();
formatter.setCommitsOnValidEdit(true);
addChangeListener(
new ChangeListener() {
private double oldValue = Double.MAX_VALUE;
@Override
public void stateChanged(ChangeEvent e) {
Double doubleValue = IntegerSpinner.this.getDoubleValue();
if (doubleValue != oldValue) {
double oldValueCopy = oldValue;
oldValue = doubleValue;
if (minIsZero && (doubleValue < 0.0)) {
doubleValue = 0.0;
field.setValue(doubleValue);
}
notifyListeners(oldValueCopy, doubleValue);
}
}
});
}
private static void setOpaque(JComponent jc,boolean opaque) {
if(jc instanceof JTextField)
return;
jc.setOpaque(false);
if(jc instanceof JSpinner)
return;
for(int a = 0; a<jc.getComponentCount(); a++) {
JComponent child = (JComponent)jc.getComponent(a);
setOpaque(child,opaque);
}
}
protected void setEnabledComponentsWhileLoading(boolean enabled) {
JDialog dialog = getJDialog();
JPanel dialogContentPanel = (JPanel)dialog.getContentPane();
Stack<JComponent> stack = new Stack<JComponent>();
stack.push(dialogContentPanel);
while (!stack.isEmpty()) {
JComponent component = stack.pop();
component.setEnabled(enabled);
int childrenCount = component.getComponentCount();
for (int i=0; i<childrenCount; i++) {
Component child = component.getComponent(i);
if (child instanceof JComponent) {
JComponent childComponent = (JComponent) child;
boolean found = false;
for (int k=0; k<this.componentsAllwaysEnabled.size(); k++) {
if (childComponent == this.componentsAllwaysEnabled.get(k)) {
found = true;
}
}
if (!found) {
// add the component in the stack to be enabled/disabled
stack.push(childComponent);
}
}
}
}
}
private boolean isLastChild(JComponent child, JComponent parent) {
if(parent == null) return false;
if(parent.getComponentCount() < 1) return false;
return (child == parent.getComponent(parent.getComponentCount()-1));
}