jframe/jpanel refresh problem

Discussion in 'Java' started by iwrk4saten, Jul 25, 2005.

  1. iwrk4saten

    iwrk4saten Guest

    Here is my problem. I have an application that keeps a running
    checklist of currently running windows. Everything seems to work fine,
    except I have a problem refreshing the JPanel/JFrame/JScrollPane (not
    sure which?) when I add and remove items from the list.

    I have a JFrame, with 2 JPanels (the first contains other buttons for
    the form). Then I have a JScroll Pane which displays the contents of
    another panel: an array of checkboxes, one checkbox for each item in my
    window list.

    I first set up the m_Display panel with all the buttons and other text
    fields, etc. Then I set up the scroll pane m_Scroll, with another
    panel m_Panel, then add the scroll pane to the first panel. Then I set
    the panel that contains everything to be the content pane of the frame.


    Everytime I want to refresh, I send a message to the form, remove all
    the items currently in the list, and then add all the new incoming
    ones. The frame properly refreshes when I add items (if there are 3
    items, and I send a message with 4 or 5, the new list automatically
    pops up). But if I have 3 items in the list, and I update it with 2,
    the frame will not redraw until I resize or move it off the screen,
    etc. I have tried an endless combination of validate, invalidate,
    pack, etc. on all the components. I don't know if something is wrong
    with my setup of components, or something is out of order. If anyone
    could help me out I'd really appreciate it. An excerpt of my code is
    included. Thanks a lot.


    public void Display()
    {
    try
    {
    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    }
    catch(Exception e)
    {
    JFrame.setDefaultLookAndFeelDecorated(true);
    }

    //set up and position the JDialog frame
    setPreferredSize(new Dimension(310, 500));
    Dimension d = getToolkit().getScreenSize();
    Point p = new Point((d.width-450)/2, (d.height-100)/2);
    setLocation(p);

    m_Display = new JPanel();
    JLabel text = new JLabel("Please enter the IP Address of the server
    in the box below:");
    m_Input = new JTextField();
    m_Panel = new JPanel();
    m_Popup = new JPopupMenu();

    //set up the buttons: ok, cancel, start, refresh
    ok = new JButton("Connect");
    cancel = new JButton("Cancel");
    start = new JButton("Start");
    refresh = new JButton("Refresh");

    //set up the text field for input
    m_Input.setPreferredSize(new Dimension(270, 20));
    //m_Input.setText("localhost");

    m_CheckBox = new JCheckBox("Display Window List", true);

    m_Display.add(text);
    m_Display.add(m_Input);
    m_Display.add(m_CheckBox);
    m_Display.add(ok);
    m_Display.add(cancel);

    m_Panel.setBackground(Color.WHITE);

    m_Scroll = new JScrollPane(m_Panel);
    m_Scroll.setPreferredSize(new Dimension(280,350));
    m_Scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
    m_Scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    m_Scroll.setBackground(Color.WHITE);


    m_Display.add(m_Scroll);
    m_Display.add(start);
    m_Display.add(refresh);

    //add action listener to buttons and text field
    ActionListener buttonaction = new buttonAction();
    cancel.addActionListener(buttonaction);
    ok.addActionListener(buttonaction);
    refresh.addActionListener(buttonaction);
    start.addActionListener(buttonaction);
    m_CheckBox.addItemListener(new CheckboxListener());
    m_Input.requestFocus();
    m_Input.addActionListener(new WRAKeyboardListener());

    //display the dialog
    setContentPane(m_Display);
    //setGlassPane(m_GlassPanel);

    m_Display.setOpaque(true);
    pack();
    setVisible(true);
    }

    //refresh function

    public void Refresh(WindowListDataMessage msg)
    {
    System.out.println("refresh");
    m_numWindows = msg.getNumWindows();
    m_TitleList = msg.getWindowTitles();
    m_GuidList = msg.getGuids();
    m_CheckedFlags = msg.getFlags();
    //first clear the existing list
    for(int i = 0; i < m_numCheckboxes; i++)
    {
    m_Panel.remove(m_checkBoxes);
    }
    m_numCheckboxes = 0;
    //14 is the number of items we can correctly display without a
    vertical scrollbar
    if(m_numWindows < 14)
    m_Panel.setLayout(new GridLayout(14, 0));
    else
    //if there are more than 14 windows, set up a vertical scrollbar
    m_Panel.setLayout(new GridLayout(m_numWindows, 0));

    //populate the check box list
    m_checkBoxes = new JCheckBox[m_numWindows];
    for(int i = 0; i < msg.getNumWindows(); i++)
    {
    m_checkBoxes = new JCheckBox(m_TitleList);
    m_checkBoxes.setBackground(Color.WHITE);
    m_Panel.add(m_checkBoxes);
    m_checkBoxes.addItemListener(new CheckboxListener());
    if(m_CheckedFlags == 1)
    m_checkBoxes.setSelected(true);
    m_numCheckboxes++;
    }
    m_Display.invalidate();
    //m_Display.pack();
    //m_Scroll.pack();
    m_Scroll.invalidate();
    validate();
    pack();
    }
    iwrk4saten, Jul 25, 2005
    #1
    1. Advertising

  2. On 25 Jul 2005 07:39:48 -0700, iwrk4saten wrote:

    > Here is my problem. ...


    Multi-posting?
    <http://www.physci.org/codes/javafaq.jsp#xpost>
    The way to solve that is, not (multi-posting)..

    --
    Andrew Thompson
    physci.org 1point1c.org javasaver.com lensescapes.com athompson.info
    It's Like "Hee Haw" With Lasers
    Andrew Thompson, Jul 25, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green
    Replies:
    0
    Views:
    3,041
    Roedy Green
    Jul 30, 2003
  2. Frédéric Colombero
    Replies:
    0
    Views:
    1,081
    Frédéric Colombero
    Nov 5, 2003
  3. Andreas Beresko

    Add a JPanel to a JPanel...

    Andreas Beresko, Jul 14, 2004, in forum: Java
    Replies:
    6
    Views:
    26,971
  4. Raphael Jolivet

    [JPanel] Drop Down JPanel

    Raphael Jolivet, Jul 18, 2008, in forum: Java
    Replies:
    1
    Views:
    1,512
    John B. Matthews
    Jul 18, 2008
  5. t1m1976
    Replies:
    1
    Views:
    2,397
    t1m1976
    Nov 7, 2010
Loading...

Share This Page