Java Code Examples for javax.swing.JComponent#setSize()

The following examples show how to use javax.swing.JComponent#setSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppFrame.java    From HBaseClient with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 初始化主窗口信息
 */
public void init()
{
    this.setJMenuBar((JMenuBar)XJava.getObject("xmMainMenuBar"));
    this.getContentPane().setLayout(new BorderLayout());
    this.getContentPane().add((Component)XJava.getObject("xpMain") ,BorderLayout.CENTER);
    this.setEnabled(false);
    
    JComponent v_JC = ((JComponent)XJava.getObject("xlTables"));
    v_JC.setSize(50 ,v_JC.getHeight());
    
    this.showHintInfo("请稍后,正在访问数据库..." ,Color.BLUE);
    
    this.initListModel();
    this.initTableModel();
    this.initTablesInfo();
    
    this.setEnabled(true);
    this.showHintInfo("欢迎使用!");
}
 
Example 2
Source File: HideableBarRenderer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void paint(Graphics g) {
    g.setColor(getBackground());
    g.fillRect(location.x, location.y, size.width, size.height);
    
    JComponent component = mainRenderer.getComponent();
    int componentWidth = component.getPreferredSize().width;
    int componentX = size.width - componentWidth;
    
    mainRenderer.move(location.x + componentX, location.y);
    component.setSize(componentWidth, size.height);
    component.paint(g);
    
    if (numberPercentRenderer == null || numberPercentRenderer.valueRenderers()[1].getComponent().isVisible()) {
        int freeWidth = size.width - maxRendererWidth - renderersGap();
        if (freeWidth >= MIN_BAR_WIDTH) {
            barRenderer.setSize(Math.min(freeWidth, MAX_BAR_WIDTH), size.height);
            barRenderer.move(location.x, location.y);
            barRenderer.paint(g);
        }
    }
}
 
Example 3
Source File: ComponentMorpher2.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private Image getComponentImage(JComponent component) {
    // Initial component sizing & layout
    component.setSize((getClientSize().width == 0) ? component.getPreferredSize() : getClientSize()); // try to fit the component to ComponentMorpher
    component.doLayout(); // layout component

    // Correct component sizing & layout
    component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
    component.doLayout(); // layout component

    // One more iteration because of nested JTextAreas
    component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
    component.doLayout(); // layout component

    // Paint component into BufferedImage
    BufferedImage componentImage = new BufferedImage(component.getSize().width, component.getSize().height,
                                                     BufferedImage.TYPE_INT_RGB);
    component.printAll(componentImage.getGraphics());

    return componentImage;
}
 
Example 4
Source File: HideableBarRenderer.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
public void paint(Graphics g) {
    g.setColor(getBackground());
    g.fillRect(location.x, location.y, size.width, size.height);
    
    JComponent component = mainRenderer.getComponent();
    int componentWidth = component.getPreferredSize().width;
    int componentX = size.width - componentWidth;
    
    mainRenderer.move(location.x + componentX, location.y);
    component.setSize(componentWidth, size.height);
    component.paint(g);
    
    if (barRenderer.isVisible()) {
        int freeWidth = size.width - maxRendererWidth - renderersGap();
        if (freeWidth >= MIN_BAR_WIDTH) {
            barRenderer.setSize(Math.min(freeWidth, MAX_BAR_WIDTH), size.height);
            barRenderer.move(location.x, location.y);
            barRenderer.paint(g);
        }
    }
}
 
Example 5
Source File: ComponentMorpher2.java    From visualvm with GNU General Public License v2.0 6 votes vote down vote up
private Image getComponentImage(JComponent component) {
    // Initial component sizing & layout
    component.setSize((getClientSize().width == 0) ? component.getPreferredSize() : getClientSize()); // try to fit the component to ComponentMorpher
    component.doLayout(); // layout component

    // Correct component sizing & layout
    component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
    component.doLayout(); // layout component

    // One more iteration because of nested JTextAreas
    component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
    component.doLayout(); // layout component

    // Paint component into BufferedImage
    BufferedImage componentImage = new BufferedImage(component.getSize().width, component.getSize().height,
                                                     BufferedImage.TYPE_INT_RGB);
    component.printAll(componentImage.getGraphics());

    return componentImage;
}
 
Example 6
Source File: SystemAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Create an icon.
* @param comp a component, which must be unattached to a container
*             and should not be used for other purposes
*/
public ComponentIcon(JComponent comp) {
    if (comp.getParent() != null) {
        throw new IllegalArgumentException();
    }

    this.comp = comp;

    Dimension size = comp.getPreferredSize();

    // Careful! If you have e.g. a JLabel with empty text, width = 0 => exceptions.
    // Must make sure it is at least a reasonable size.
    comp.setSize(Math.max(size.width, 16), Math.max(size.height, 16));
}
 
Example 7
Source File: ShadowPopupFactory.java    From javamelody with Apache License 2.0 5 votes vote down vote up
/**
 * Reinitializes this ShadowPopup using the given parameters.
 *
 * @param newOwner component mouse coordinates are relative to, may be null
 * @param newContents the contents of the popup
 * @param newX the desired x location of the popup
 * @param newY the desired y location of the popup
 * @param newPopup the popup to wrap
 */
private void reset(Component newOwner, Component newContents, int newX, int newY,
		Popup newPopup) {
	this.owner = newOwner;
	this.contents = newContents;
	this.popup = newPopup;
	this.x = newX;
	this.y = newY;
	if (newOwner instanceof JComboBox) {
		return;
	}
	// Do not install the shadow border when the contents
	// has a preferred size less than or equal to 0.
	// We can't use the size, because it is(0, 0) for new popups.
	final Dimension contentsPrefSize = newContents.getPreferredSize();
	if (contentsPrefSize.width <= 0 || contentsPrefSize.height <= 0) {
		return;
	}
	for (Container p = newContents.getParent(); p != null; p = p.getParent()) {
		if (p instanceof JWindow || p instanceof Panel) {
			// Workaround for the gray rect problem.
			p.setBackground(newContents.getBackground());
			heavyWeightContainer = p;
			break;
		}
	}
	final JComponent parent = (JComponent) newContents.getParent();
	oldOpaque = parent.isOpaque();
	oldBorder = parent.getBorder();
	parent.setOpaque(false);
	parent.setBorder(SHADOW_BORDER);
	// Pack it because we have changed the border.
	if (heavyWeightContainer != null) {
		heavyWeightContainer.setSize(heavyWeightContainer.getPreferredSize());
	} else {
		parent.setSize(parent.getPreferredSize());
	}
}
 
Example 8
Source File: TextHighlightingDemo.java    From filthy-rich-clients with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void installInLayeredPane(JComponent component) {
    JLayeredPane layeredPane = getRootPane().getLayeredPane();
    layeredPane.add(component, JLayeredPane.PALETTE_LAYER, 20);
    Dimension size = component.getPreferredSize();
    component.setSize(size);
    component.setLocation((getWidth() - size.width) / 2,
            (getHeight() - size.height) / 2);
    component.revalidate();
    component.setVisible(true);
}
 
Example 9
Source File: PlotterAdapter.java    From rapidminer-studio with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void render(Graphics graphics, int width, int height) {
	JComponent renderComponent = getRenderComponent();
	renderComponent.setSize(width, height);
	renderComponent.setDoubleBuffered(false);
	renderComponent.paint(graphics);
	renderComponent.setDoubleBuffered(true);
}
 
Example 10
Source File: MockComponent.java    From pumpernickel with MIT License 5 votes vote down vote up
/**
 * Creates a MockComponent that resembles the argument component.
 * <P>
 * Note this method will traverse c and its subcomponents and may
 * temporarily change properties of inner components: such as the focused
 * state, the visibility, etc.
 * <P>
 * The goal is of this component is not to mirror the exact state of a
 * component, but rather to provide a sample image of this component in its
 * plain, unmodified, unused state.
 * 
 * @param c
 */
public MockComponent(JComponent c) {
	Dimension preferredSize = c.getPreferredSize();
	Dimension currentSize = c.getSize();

	Dimension d = new Dimension(Math.max(preferredSize.width,
			currentSize.width), Math.max(preferredSize.height,
			currentSize.height));

	if (currentSize.width == 0 || currentSize.height == 0) {
		// if the component isn't visible yet
		c.setSize(d);
		c.doLayout();
	}

	storeState(c);

	image = new BufferedImage(d.width, d.height,
			BufferedImage.TYPE_INT_ARGB);

	Graphics2D g = image.createGraphics();
	g.setComposite(AlphaComposite.Clear);
	g.fillRect(0, 0, d.width, d.height);
	g.setComposite(AlphaComposite.SrcOver);

	c.paint(g);
	g.dispose();
	setPreferredSize(d);
	setMinimumSize(d);
	setMaximumSize(d);
	setOpaque(c.isOpaque());
	setName(c.getName());
	setToolTipText(c.getToolTipText());

	restoreState(c);
}
 
Example 11
Source File: PaddingInfo.java    From pumpernickel with MIT License 5 votes vote down vote up
/**
 * Should be called shortly after <code>prep()</code>.
 */
private static void restore(Component c) {
	if (c instanceof JComponent) {
		JComponent jc = (JComponent) c;
		Boolean b = (Boolean) jc.getClientProperty(USED_TO_BE_OPAQUE);
		if (b != null && b.booleanValue()) {
			jc.setOpaque(true);
		}
		jc.putClientProperty(USED_TO_BE_OPAQUE, null);

		Dimension d = (Dimension) jc.getClientProperty(SIZE);
		if (d != null) {
			jc.setSize(d);
			jc.putClientProperty(SIZE, null);
		}
	}
	if (c instanceof JSlider) {
		JSlider s = (JSlider) c;
		ChangeListener[] listeners = (ChangeListener[]) s
				.getClientProperty(CHANGE_LISTENERS);
		Integer i = (Integer) s.getClientProperty(SLIDER_VALUE);
		if (i != null)
			s.setValue(i.intValue());
		if (listeners != null) {
			for (int a = 0; a < listeners.length; a++) {
				s.addChangeListener(listeners[a]);
			}
		}
		s.putClientProperty(SLIDER_VALUE, null);
		s.putClientProperty(CHANGE_LISTENERS, null);
	}
	if (c instanceof Container) {
		Container c2 = (Container) c;
		for (int a = 0; a < c2.getComponentCount(); a++) {
			restore(c2.getComponent(a));
		}
	}
}
 
Example 12
Source File: PaddingInfo.java    From pumpernickel with MIT License 5 votes vote down vote up
/**
 * This makes some visual changes to the component and its children. Shortly
 * after calling this method, the <code>restore()</code> method needs to be
 * called.
 */
private static void prep(Component c) {
	if (c instanceof JComponent) {
		JComponent jc = (JComponent) c;
		if (jc.isOpaque()) {
			jc.setOpaque(false);
			jc.putClientProperty(USED_TO_BE_OPAQUE, Boolean.TRUE);
		}

		Dimension preferredSize = c.getPreferredSize();
		if (greaterThanOrEqualTo(jc.getSize(), preferredSize) == false) {
			jc.putClientProperty(SIZE, c.getSize());
			jc.setSize(preferredSize);
		}
	}
	if (c instanceof JSlider) {
		JSlider s = (JSlider) c;
		ChangeListener[] listeners = s.getChangeListeners();
		int mid = (s.getMinimum() + s.getMaximum()) / 2;
		if (mid != s.getValue()) {
			s.putClientProperty(CHANGE_LISTENERS, listeners);
			for (int a = 0; a < listeners.length; a++) {
				s.removeChangeListener(listeners[a]);
			}
			s.putClientProperty(SLIDER_VALUE, new Integer(s.getValue()));
			s.setValue(mid);
		}
	}
	if (c instanceof Container) {
		Container c2 = (Container) c;
		for (int a = 0; a < c2.getComponentCount(); a++) {
			prep(c2.getComponent(a));
		}
	}
	if (c.isValid() == false)
		c.validate();
}
 
Example 13
Source File: ComponentMorpher.java    From visualvm with GNU General Public License v2.0 5 votes vote down vote up
private JComponent layoutComponent(JComponent component) {
    // Initial component sizing & layout
    if (getClientSize().width > 0) {
        component.setSize(getClientSize()); // try to fit the component to ComponentMorpher
        component.validate(); // layout component

        // Correct component sizing & layout
        component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
        component.validate(); // layout component
    }

    return component;
}
 
Example 14
Source File: bug8132119.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static JComponent createComponent(String str) {
    JComponent comp = new JLabel(str);
    comp.setSize(WIDTH, HEIGHT);
    comp.putClientProperty(TextAttribute.NUMERIC_SHAPING, NUMERIC_SHAPER);
    comp.setFont(getFont());
    return comp;
}
 
Example 15
Source File: PalettePanel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void layoutContainer( Container parent ) {
    int width = getWidth ();

    int height = 0;
    for( int i=0; i<descriptors.length; i++ ) {
        CategoryDescriptor paletteCategoryDescriptor = descriptors[i];
        paletteCategoryDescriptor.setPositionY( height );
        JComponent comp = paletteCategoryDescriptor.getComponent();
        comp.setSize( width, comp.getPreferredSize().height );
        height += paletteCategoryDescriptor.getComponent().getHeight();
    }
}
 
Example 16
Source File: ComponentMorpher.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private JComponent layoutComponent(JComponent component) {
    // Initial component sizing & layout
    if (getClientSize().width > 0) {
        component.setSize(getClientSize()); // try to fit the component to ComponentMorpher
        component.validate(); // layout component

        // Correct component sizing & layout
        component.setSize(new Dimension(getClientSize().width, component.getPreferredSize().height)); // Width of component is fixed, update height
        component.validate(); // layout component
    }

    return component;
}
 
Example 17
Source File: MenuBar.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private Component createNoComponent() {
    JComponent noComponent = new JComponent() {};
    noComponent.setSize(0, 0);
    noComponent.setVisible(false);
    return noComponent;
}
 
Example 18
Source File: PToolTipUI.java    From PolyGlot with MIT License 4 votes vote down vote up
/**
 * To override font, override createToolTip() on component creating tooltip.
 * On creation of ToolTip, change to desired font.
 * @param g
 * @param c 
 */
@Override
public void paint(Graphics g, JComponent c) {
    String tipText = ((JToolTip)c).getTipText();
    tipText = tipText == null ? "" : tipText;
    String[] tipLines = tipText.split("\n");
    Font font = c.getFont();
    
    g.setFont(font);
    
    FontMetrics metrics = g.getFontMetrics();
    int fontHeight = metrics.getHeight();
    int height = (fontHeight * tipLines.length) + 2;
    int width = this.getWidestStringText(tipLines, metrics) + 10;
    
    int fontSize = font.getSize();
    fontSize = fontSize == 0 ? PGTUtil.DEFAULT_FONT_SIZE.intValue() : fontSize;
    c.setFont(font.deriveFont(fontSize));
    ((JToolTip)c).setTipText(tipText);
    
    
    Dimension size = new Dimension(width, height);
    
    c.setSize(size);
    c.getParent().setSize(size);
    
    Insets insets = c.getInsets();
    Rectangle paintTextR = new Rectangle(
        insets.left,
        insets.top,
        size.width - (insets.left + insets.right),
        size.height - (insets.top + insets.bottom));
    
    ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
    g.setColor(Color.black);
    g.fillRect(insets.left,
        insets.top,
        size.width - (insets.left + insets.right),
        size.height - (insets.top + insets.bottom));
    
    g.setColor(Color.white);
    
    for (int i = 0 ; i < tipLines.length; i++) {
        g.drawString(tipLines[i], paintTextR.x + 5,
                paintTextR.y + metrics.getAscent() + (i * (fontHeight)));
    }
}