8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

如何使用 java swing 为 Jtree、DefaultTreeCellRenderer 提供背景颜色

Karthik Srinivasan 1月前

12 0

我创建了一个以下渲染器,它使用图标和文件夹列表渲染 JTree。我想为该部分的背景添加不同的颜色。我尝试将它提供给面板,但是 Jtree 是……

我创建了一个以下渲染器,它使用图标和文件夹列表渲染 JTree。我想为该部分的背景添加不同的颜色。我尝试将它提供给面板,但 Jtree 位于左面板上方,因此无法应用。我该怎么办?请帮助我。提前谢谢您。

这是我想要灰色背景颜色的 ss[1]: https://i.sstatic.net/OlO9Y7J1.png

我需要修改的代码一览

DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() {
            private final ImageIcon mainFolderIcon = new ImageIcon(getClass().getResource(strings.getString("folder_icon")));
            private final ImageIcon folderIcon = new ImageIcon(getClass().getResource(strings.getString("folder_icon")));

            // Scale the image to the desired size
            ImageIcon originalIcon = new ImageIcon(getClass().getResource(strings.getString("right_arrow_icon")));
            int width = 20;  // Width in pixels
            int height = 20; // Height in pixels
            // Scale the image to the desired size
            Image scaledImage = originalIcon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
            // Create a new ImageIcon with the scaled image
            ImageIcon arrowIcon = new ImageIcon(scaledImage);

            private final int VERTICAL_GAP = 5; // Adjust this value as needed

            @Override
            public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
                JLabel label = (JLabel) super.getTreeCellRendererComponent(tree, value, sel, expanded, leaf, row, hasFocus);
                label.setFont(new Font("Roboto", Font.PLAIN, 14)); // Increase font size of file name

                DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
                boolean isMainFolder = (node.getParent() == null);
 // Set the custom cell renderer for the tree
        tree.setCellRenderer(renderer);

        // Add a tree selection listener to handle folder selections
        tree.addTreeSelectionListener(new TreeSelectionListener() {
            @Override
            public void valueChanged(TreeSelectionEvent e) {
                // Get the selected node
                DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();

                // Check if it is a folder node
                if (selectedNode != null && selectedNode.isLeaf()) {
                    // Perform the desired action for folder selection
                    String folderName = selectedNode.getUserObject().toString();
                    int position = selectedNode.getParent().getIndex(selectedNode);
                    System.out.println("Folder selected: " + folderName);
                    System.out.println("Position: " + position);
                    DriveFile driveFile1 = folderLeftList.get(position);
                    if (!breadcrumbTrail.contains(driveFile1)) {
                        breadcrumbTrail.add(driveFile1);
                    }

                    // Create a new renderer component with white text color
                    DefaultTreeCellRenderer renderer = (DefaultTreeCellRenderer) tree.getCellRenderer();
                    JLabel label = (JLabel) renderer.getTreeCellRendererComponent(tree, selectedNode, false, false, true, position, false);
                    label.setForeground(Color.WHITE);
                    // Repaint the tree to update the text color
                    tree.repaint();

                    new FileManagerController(drive, username, role, context, cookieStore, dashboardView, listDriveNames, 23, driveFile1, fileManagerView, null, null, null, null, null, null, null, null, null, password, null, 0, null, null, mapDrives, null, null, loginForm, null, null, null, isListView, null);
                }
            }
        });

        leftPanel.setBackground(Color.WHITE); // Set the desired background color
        leftPanel.setBorder(null); // Removes any border set on the panel
        leftPanel.add(new JScrollPane(tree), BorderLayout.CENTER);

帖子版权声明 1、本帖标题:如何使用 java swing 为 Jtree、DefaultTreeCellRenderer 提供背景颜色
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Karthik Srinivasan在本站《java》版块原创发布, 转载请注明出处!
最新回复 (0)
返回
作者最近主题: