jframe/jpanel refresh problem

I

iwrk4saten

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();
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,739
Latest member
Clint8040

Latest Threads

Top