下面列出了javax.swing.JPanel#getComponentCount ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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" ) );
}
}
}
public void setContent(JPanel panel) {
removeAll();
if (panel == null) {
add(disabledPanel, BorderLayout.CENTER);
empty = true;
} else {
add(scrollPane, BorderLayout.CENTER);
scrollPane.setViewportView(panel);
empty = panel.getComponentCount() == 0;
}
}
JPanel build(final Consumer<Color> consumer, final boolean alphaHidden)
{
load();
JPanel container = new JPanel(new GridBagLayout());
GridBagConstraints cx = new GridBagConstraints();
cx.insets = new Insets(0, 1, 4, 2);
cx.gridy = 0;
cx.gridx = 0;
cx.anchor = GridBagConstraints.WEST;
for (String s : recentColors)
{
if (cx.gridx == MAX / 2)
{
cx.gridy++;
cx.gridx = 0;
}
// Make sure the last element stays in line with all of the others
if (container.getComponentCount() == recentColors.size() - 1)
{
cx.weightx = 1;
cx.gridwidth = MAX / 2 - cx.gridx;
}
container.add(createBox(ColorUtil.fromString(s), consumer, alphaHidden), cx);
cx.gridx++;
}
return container;
}
/**
* Create a panel that contains all the {@link ValueProvider ValueProviders} and their configuration, depending on
* the connectionModel they are editable
*
* @return a scrollable panel
*/
private JComponent createConfigurationPanel() {
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.fill = GridBagConstraints.BOTH;
gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.weightx = 1;
JPanel allVPPanel = new JPanel(new GridBagLayout());
ConnectionInformation information = ConnectionModelConverter.getConnection(connectionModel);
final ObservableList<ValueProviderModel> valueProviders = connectionModel.valueProvidersProperty();
for (ValueProvider valueProvider : valueProviders) {
if (allVPPanel.getComponentCount() > 0) {
final Insets insets = gbc.insets;
gbc.insets = new Insets(0, 16, 0, 0);
allVPPanel.add(new JSeparator(), gbc);
gbc.insets = insets;
}
allVPPanel.add(createVPPanel(valueProvider, information), gbc);
}
gbc.weighty = 1;
allVPPanel.add(new JPanel(), gbc);
JPanel toTheLeft = new JPanel();
toTheLeft.setLayout(new BorderLayout());
toTheLeft.add(allVPPanel, BorderLayout.CENTER);
final ExtendedJScrollPane scrollPane = new ExtendedJScrollPane(toTheLeft);
scrollPane.setBorder(null);
return scrollPane;
}
/**
* 重写父类方法,以实现对文件查看列表的额外设置.
* <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 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);
}
}
}
@Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
// cb.setEnabled(enabled);
JPanel jp = (JPanel) cards.getComponent( cb.getSelectedIndex() );
for (int i = 0; i < jp.getComponentCount(); i++) {
jp.getComponent(i).setEnabled(enabled);
}
}
/**
* Returns the renderer component after initializing it
*
* @param graph The graph (really a M in disguise)
* @param view The cell view
* @param sel If the view is selected or not
* @param focus If the view has focus or not
* @param preview If the graph is in preview mode or not
* @return The renderer component fully initialized
*/
@Override
@SuppressWarnings("unchecked")
public Component getRendererComponent(JGraph graph, CellView view, boolean sel, boolean focus, boolean preview) {
this.model = (M) graph;
this.selected = sel;
this.preview = preview;
this.hasFocus = focus;
ModelVertex<F, GM, M, N, E> v = (ModelVertex<F, GM, M, N, E>) view.getCell();
// If the constraint is hidden, return a completely empty JPanel that
// doesn't paint anything
if ((v instanceof ModelConstraint) && !((ModelConstraint<F, GM, M, N, E>) v).isVisible()) {
return new JPanel() {
private static final long serialVersionUID = -8516030326162065848L;
@Override
public void paint(Graphics g) {
}
};
}
// if entity moved, set overview as dirty FIXME(if not working properly,
// may be because of int casting and off by one)
if ((int) (view.getBounds().getX()) != v.getX() || (int) (view.getBounds().getY()) != v.getY()) {
model.setDirty();
}
// Initialize panel
removeAll();
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
// Add in entity/constraint label
add(_entity = new JPanel(new GridLayout(1, 1)));
_entity.add(_entityLabel = new JLabel(v.getName(), SwingConstants.CENTER));
N myNode = (N) v;
// Add in attributes
_attributes = new JPanel(new GridLayout(0, 1));
add(_attributes);
for (EntityAttribute<F, GM, M, N, E> att : myNode.getEntityAttributes()) {
_attributes.add(new JLabel(" @ " + att.getName()));
}
if ((_attributes.getComponentCount() == 0) || !model.getFrame().getShowAttsVal()) {
_attributes.setVisible(false);
}
// Add in unique keys
_uniqueKeys = new JPanel(new GridLayout(0, 1));
add(_uniqueKeys);
for (UniqueKey<F, GM, M, N, E> key : myNode.getUniqueKeys()) {
_uniqueKeys.add(new JLabel(" $ " + key.getKeyName()));
}
if ((_uniqueKeys.getComponentCount() == 0) || !model.getFrame().getShowAttsVal()) {
_uniqueKeys.setVisible(false);
}
@SuppressWarnings("rawtypes")
Map attributes = view.getAllAttributes();
installAttributes(v, attributes);
// Set desired size
setSize(getPreferredSize());
return this;
}
@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;
}
}
}
private void setEnabled (JPanel panel, boolean enabled) {
for (int i = 0; i < panel.getComponentCount(); ++i) {
panel.getComponent(i).setEnabled(enabled);
}
}