Skip to content
Advertisement

adding JScrollBar to a JPanel with GridBagLayout

My whole purpose of this is to get something looking like this:

IMG

I want my skills list to scroll similar to freezing the top row of your spreadsheet in excel.

Here is the code for my top row.

    //#### Skills frame start ####
    skillsFrame = new JInternalFrame("Skills", true, true, false, false);
    skillsFrame.setLayout(new BorderLayout());
    skillsFrame.setLocation(Integer.parseInt(configFile.getProperty("SKILLS_FRAME_COORDINATE_X")), 
            Integer.parseInt(configFile.getProperty("SKILLS_FRAME_COORDINATE_Y")));
    skillsFrame.setSize(Integer.parseInt(configFile.getProperty("SKILLS_FRAME_SIZE_X")), 
            Integer.parseInt(configFile.getProperty("SKILLS_FRAME_SIZE_Y")));
    skillsPanelHeader = new JPanel();
    skillsPanelContent = new JPanel();
    //skillsPanelScrollPane = new JScrollPane();
    //skillsPanelScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    //skillsPanelScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
    
    GridBagLayout gridBagLayout = new GridBagLayout();
    gridBagLayout.columnWidths = new int[]{0, 110, 200, 45, 17};
    gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0};
    gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0};
    gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
    skillsPanelHeader.setLayout(gridBagLayout);
    skillsPanelContent.setLayout(gridBagLayout);
    
    JLabel lblImage = new JLabel("Icon");
    lblImage.setFont(new Font("Tahoma", Font.BOLD, 14));
    GridBagConstraints gbc_lblImage = new GridBagConstraints();
    gbc_lblImage.insets = new Insets(0, 0, 5, 5);
    gbc_lblImage.gridx = 0;
    gbc_lblImage.gridy = 0;
    skillsPanelHeader.add(lblImage, gbc_lblImage);
    
    JLabel lblSkills = new JLabel("Skills");
    lblSkills.setFont(new Font("Tahoma", Font.BOLD, 14));
    GridBagConstraints gbc_lblSkills = new GridBagConstraints();
    gbc_lblSkills.gridx = 1;
    gbc_lblSkills.insets = new Insets(0, 0, 5, 5);
    gbc_lblSkills.gridy = 0;
    skillsPanelHeader.add(lblSkills, gbc_lblSkills);
    
    JLabel lblExp = new JLabel("Exp");
    lblExp.setFont(new Font("Tahoma", Font.BOLD, 14));
    GridBagConstraints gbc_lblExp = new GridBagConstraints();
    gbc_lblExp.insets = new Insets(0, 0, 5, 5);
    gbc_lblExp.gridx = 2;
    gbc_lblExp.gridy = 0;
    skillsPanelHeader.add(lblExp, gbc_lblExp);
    
    JLabel lblLevel = new JLabel("Level");
    lblLevel.setFont(new Font("Tahoma", Font.BOLD, 14));
    GridBagConstraints gbc_lblLevel = new GridBagConstraints();
    gbc_lblLevel.insets = new Insets(0, 0, 5, 0);
    gbc_lblLevel.gridx = 3;
    gbc_lblLevel.gridy = 0;
    skillsPanelHeader.add(lblLevel, gbc_lblLevel);
    
    JSeparator separator = new JSeparator();
    GridBagConstraints gbc_separator = new GridBagConstraints();
    gbc_separator.fill = GridBagConstraints.HORIZONTAL;
    gbc_separator.gridwidth = 4;
    gbc_separator.insets = new Insets(0, 0, 5, 0);
    gbc_separator.gridx = 0;
    gbc_separator.gridy = 1;
    skillsPanelHeader.add(separator, gbc_separator);
    
    skillsFrame.setMinimumSize(new Dimension(425,500));
    skillsFrame.setVisible(false);
    skillsFrame.getContentPane().add(skillsPanelHeader, BorderLayout.NORTH);
    panel.add(skillsFrame);
    //#### Skills frame end ####

And here is the code for parsing the (commandString – reply of skills from server/DB)

String SkillsList[] = commandString.split("\|");
//TODO:: make the painting not keep getting darker and darker
for (int i = 0; i < SkillsList.length; i = i+3){
    JLabel label = new JLabel("");
    label.setIcon(new ImageIcon(SkillsList[i]));
    GridBagConstraints gbc_label = new GridBagConstraints();
    gbc_label.insets = new Insets(0, 0, 5, 5);
    gbc_label.gridx = 0;
    gbc_label.gridy = i+2;
    skillsPanelContent.add(label, gbc_label);
                            
    JLabel lblNewLabel = new JLabel(SkillsList[i+1]);
    GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
    gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
    gbc_lblNewLabel.gridx = 1;
    gbc_lblNewLabel.gridy = i+2;
    skillsPanelContent.add(lblNewLabel, gbc_lblNewLabel);
                            
    JProgressBar progressBar_1 = new JProgressBar();
    progressBar_1.setStringPainted(true);
    progressBar_1.setString(SkillsList[i+2] + "/");
    GridBagConstraints gbc_progressBar_1 = new GridBagConstraints();
    gbc_progressBar_1.fill = GridBagConstraints.HORIZONTAL;
    gbc_progressBar_1.insets = new Insets(0, 0, 5, 5);
    gbc_progressBar_1.gridx = 2;
    gbc_progressBar_1.gridy = i+2;
    skillsPanelContent.add(progressBar_1, gbc_progressBar_1);
                            
    JLabel label_1 = new JLabel("1");
    GridBagConstraints gbc_label_1 = new GridBagConstraints();
    gbc_label_1.insets = new Insets(0, 0, 5, 0);
    gbc_label_1.gridx = 3;
    gbc_label_1.gridy = i+2;
    skillsPanelContent.add(label_1, gbc_label_1);
                            
    if (i == SkillsList.length-3){
            JScrollBar scrollbar = new JScrollBar();
            GridBagConstraints gbc_scrollbar = new GridBagConstraints();
            gbc_scrollbar.insets = new Insets(0, 0, 5, 0);
            gbc_scrollbar.gridx = 4;
            gbc_scrollbar.gridy = 2;
            gbc_scrollbar.fill = GridBagConstraints.BOTH;
            gbc_scrollbar.gridheight = i;
            skillsPanelContent.add(scrollbar, gbc_scrollbar);
     }
}
skillsFrame.getContentPane().add(skillsPanelContent, BorderLayout.CENTER);
System.out.println(commandString);

As you can see it adds it all perfectly, but the scrollbbar is not hooked up to anything. I am at my witts end with this. I tried using a JScrollPane by adding skillsPanelContent to it, and it would not show up no matter what I did.

Any advice or help would be soooo much appreciated. I hope I kept this as short(code) as necessary. Please let me if I can include anything else to be of more help. Thanks.

Advertisement

Answer

Why not add a JScrollPane around your contentPanel :

skillsFrame.getContentPane().add(new JScrollPane(skillsPanelContent), BorderLayout.CENTER);

And of course remove your scrollbar. You may also need to disable horizontalScrollBarPolicy by setting it to the ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER value: setHorizontalScrollBarPolicy(int policy)

Advertisement