CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2007
    Posts
    129

    Unhappy Stuck on Java GUI Program

    I have a problem with a Java GUI program I am writing.
    I am trying to display a list of icons for a virtual (emulated) directory.
    That works fine, but I want to clip the display region and use a scrollbar.
    So, following some examples I found online, I create a JScrollPane and added my JPanel containing my directory JList.
    The problem is, I am also showing a button and textbox in the same JFrame, and the scrollbar scrolls this too. I only want the scrollbar to scroll the JPanel with the JList.
    I tried creating the JScrollPane and adding that to my JPanel, but the JList panel didn't display.

    Can someone please help me figure out what I'm doing wrong?
    I only want to scroll the JList panel.


    I've attached my code, which compiles using the JDK 1.6
    Attached Files Attached Files

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Stuck on Java GUI Program

    If you post the relevant code in [CODE]...[/CODE] tags, more people are likely to see it.

    The usual way to have a scrolling panel and a non-scrolling panel in the same frame is to put the scrollpane with it's scrolling content into one panel, and put the non-scrolling content into another panel, then put them both in the frame, using a suitable layout manager, e.g. BoxLayout.

    It is a bad plan that admits of no modification...
    P. Syrus
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Nov 2007
    Posts
    129

    Re: Stuck on Java GUI Program

    My code uses BorderLayout currently.
    I write a JList with icons and text to a JPanel and a button and textbox to other JPanels.
    This JPanels get added to my JFrame.
    The code I attached has this entire window with a scrollbar, but the entire window gets scrolled.

    I want to be able to only scroll the JList JPanel, but it doesn't seem to work when I try.

    Can anyone help? - I am stuck

    The following is the code for the main:
    Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class IconDisplay extends JFrame {
      public static void main(String[] args) {
        new IconDisplay();
      }
    
      public IconDisplay() {
        super("Data Location");
        WindowUtilities.setNativeLookAndFeel();
        addWindowListener(new ExitListener());
        JavaLocationCollection collection =
          new JavaLocationCollection();
        JavaLocationListModel listModel =
          new JavaLocationListModel(collection);
        JList sampleJList = new JList(listModel);
        sampleJList.setCellRenderer(new JavaLocationRenderer());
        Font displayFont = new Font("Serif", Font.BOLD, 18);
        sampleJList.setFont(displayFont);
        JButton button = new JButton("Back"); 
        JTextField location = new JTextField("Location: /root");
        location.setFont(displayFont);
        location.setBackground(Color.WHITE);
        location.setEditable(false);
        JPanel panel = new JPanel(new BorderLayout());
        JPanel scrollPanel = new JPanel(new BorderLayout());
        scrollPanel.add(sampleJList, BorderLayout.CENTER);
        panel.add(button, BorderLayout.NORTH);
        panel.add(location, BorderLayout.CENTER);
        panel.add(sampleJList, BorderLayout.SOUTH);
        JScrollPane scrolled = new JScrollPane(panel);
        getContentPane().add(scrolled, BorderLayout.CENTER);
        setResizable(false);
        pack();
        setSize(350, 300);
        setVisible(true);
      }
    }
    Attached Files Attached Files

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Stuck on Java GUI Program

    Quote Originally Posted by sjaycohn View Post
    Can anyone help? - I am stuck
    Why don't you try what I suggested?

    Put your list into the scrollpane, and add the scrollpane to a panel. Put your text field and button into another panel, and then add them both to a main panel with a BoxLayout (with Y_AXIS). Then add this main panel to your frame:
    Code:
    JPanel mainPanel = new JPanel();
    mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    // top panel for button & text
    JPanel topPanel = new JPanel(new BorderLayout());
    topPanel.add(button, BorderLayout.NORTH);
    topPanel.add(location, BorderLayout.CENTER);
    // scroll panel for scroll pane
    JPanel scrollPanel = new JPanel(new BorderLayout());
    // scroll pane for list
    JScrollPane scroller = new JScrollPane(sampleJList);
    scrollPanel.add(scroller);
    mainPanel.add(topPanel);
    mainPanel.add(scrollPanel);
    add(mainPanel);
    Rewrite and revise. Do not be afraid to seize what you have and cut it to ribbons ... Good writing means good revising...
    W Strunk Jr
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Nov 2007
    Posts
    129

    Smile Re: Stuck on Java GUI Program

    Thank you so much.
    That worked beautifully.
    I tried adding the JScrollPane to the panel before, but it never seemed to work for me previously - not sure why.

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Stuck on Java GUI Program

    The code you posted was very confused - adding the list to two panels but not to the scroll pane. When starting out you need to work out exactly what you need to do before writing the code. If you change you mind about something, or want to rework the design, go back to paper to work it out, or start with a blank method and copy across the the lines you want - don't hack around with code that isn't doing what you want unless you've worked out exactly how to fix it first. Panels and components nest inside each other very simply, but you do need to work out in advance how you want them to fit together.

    A tip - if you give your objects meaningful names that tell you what each one is for, you won't find it so confusing. That's why I called the top panel 'topPanel' (or I could have called it 'buttonPanel' or 'textPanel', etc) instead of plain 'panel'.

    The first step towards wisdom is calling things by their right names...
    Chinese proverb
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured