Java Swing O Reilly want to return a (Web server setup)

Java Swing O Reilly want to return a subclass of JComponent. If you want multiple components to do your rendering (or your editing, for that matter), extending Container is a good place to start. This interface defines one method: public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) This method takes as arguments all of the information relevant to rendering a tree node. You are free to ignore any argument that doesn’t interest you, or you can go directly to the tree node, value. You can then create and return a component that will draw your node correctly. A simple way to build your first renderer is to extend the JLabel class. You’ll probably want to keep some other state information around, as well. Here’s the code that built the expression renderer above. In the constructor, we create a center-aligned label with a medium-sized monospaced font. When the program renders a cell or displays a cell’s tooltip (by calling getTreeCellRendererComponent()) we set the current color for the foreground and background according to the selected status of our object. We could also query our object for other bits of information if needed. To get the (slightly) more interesting tooltips working, we override the getToolTipText() method to return a different string, depending on whether the mouse is over is a node or a leaf. We use the opaque property to determine whether we see the blue background. // ExpressionTreeCellRenderer.java // A renderer for our expression cells. // import java.awt.*; import javax.swing.*; import javax.swing.tree.*; public class ExpressionTreeCellRenderer extends JLabel implements TreeCellRenderer { Color backColor; boolean isLeaf; public ExpressionTreeCellRenderer() { // Pick a nice, big, fixed width font for our labels setFont(new Font(”Monospaced”, Font.PLAIN, 16)); setHorizontalAlignment(SwingConstants.CENTER); } public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) { - 522 -
Check Tomcat Web Hosting services for best quality webspace to host your web application.

Leave a Reply