[英]adding JScrollBar to a JPanel with GridBagLayout
My whole purpose of this is to get something looking like this: 我的整个目的是使事情看起来像这样:
IMG http://img221.imageshack.us/img221/1294/jscrollbaruselessf.png IMG http://img221.imageshack.us/img221/1294/jscrollbaruselessf.png
I want my skills list to scroll similar to freezing the top row of your spreadsheet in excel. 我希望技能列表的滚动类似于冻结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) 这是用于解析(commandString-服务器/数据库的技能回复)的代码
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. 如您所见,它完美地添加了所有内容,但scrollbbar并未连接任何东西。 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. 我尝试通过向其添加skillPanelContent来使用JScrollPane,无论我做什么,它都不会显示。
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. 谢谢。
Why not add a JScrollPane
around your contentPanel : 为什么不在contentPanel周围添加JScrollPane
:
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)
您可能还需要通过将horizontalScrollBarPolicy设置为ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER
值来禁用它: setHorizontalScrollBarPolicy(int policy)
To get the header to always stay on top you need to pull out the headers and put them either in a separate component or in a border. 为了使标题始终位于顶部,您需要拉出标题并将其放在单独的组件中或置于边框中。 Then put the rest of the date in a JPanel, and do JScrollPane myScroller = new JScrollPane(myPanel) then set the horizontalscrollbarpolicy to NEVER. 然后将剩余的日期放在JPanel中,然后执行JScrollPane myScroller = new JScrollPane(myPanel),然后将horizontalscrollbarpolicy设置为NEVER。
It may be a little tricky to keep the headers inline with the columns, but I advise trying to make an extension of Border and reference the GridBagLayout for column positions. 使标题与列保持一致可能有点棘手,但是我建议尝试扩展Border并引用GridBagLayout来获取列位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.