我创建了一个以下渲染器,它使用图标和文件夹列表渲染 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);