简体   繁体   中英

Java: Freezing components on a JFrame

Long time lurker, second time poster (it's got that desperate)

I have a frame that needs to display twelve components next to each other (as if each component is its own column). The first component is a list of attribute names, the second component shows the default attribute, and the next ten are all pulled in from a database. I would like for my Scroll Bar to effectively "freeze" the first two components (ie it always shows the first two components) and the scroll bar allows you to look through the rest of the entries. This is similar to Excel where we freeze columns, a la here .

I've looked into using tables, but the problem is that my components are not just text. They've got images and the like too.

Here's some of the code I'm using

    JPanel info = new JPanel(); // this is the main component (holds the other 12)
    info.setLayout(new GridBagLayout()); // GridBag Layoout
    info.add(attNames); // add in the attribute names component
    info.add(currentCase); // add in the default values

    JPanel rets = new JPanel(); // add in the ten retrieved cases
    rets.setLayout(new GridLayout(1,10));
    for (int i=0;i<maxCases;i++)
    {
        rets.add(retCase[i]);
    }

    info.add(rets); // add the return cases to the info component
    JScrollPane scrollBar2=new JScrollPane(rets,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);   // surround the return cases with a scroll bar
    info.add(scrollBar2); // add the scrollbar

    //add the info comp to the content pane along with other necessary components

    this.getContentPane().add(casesPanel, BorderLayout.NORTH);
    this.getContentPane().add(info, BorderLayout.CENTER);
    this.getContentPane().add(buttons, BorderLayout.SOUTH);

    //finally, add the overall scrollbars and set the size
    this.pack();
    JScrollPane scrollBar=new JScrollPane(info,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);  
    this.setSize(this.getWidth(), 750);
    this.setResizable(true);
    this.add(scrollBar);

The problem is that the scroll bar for the returned case doesn't think it's needed and I have to use the big scroll bar, meaning I can move away from the first two components.

Any help would be greatly appreciated!!

Here's the problem

Cheers, K

Place the frozen section into the scroll panes row header. Check how to use scroll panes for more info and examples

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM