jscrollpane remove function confusion
I have a panel with a text field, button, and a jscrollpane.
Button calls a function that populates the jscrollpane based on the textfield. When the textfield is focused, I want the jScrollPane1 to clear out. After the user types in text within the textfield, the button clicked will repopulate the jscrollpane.
The button repopulates the jscrollpane correctly with each new textfield entry. However, the FocusGained on the textfield does not clear out the jscrollpane. I've tried to remove the components as you can see below, but it doesn't make the componenets disappear.
jScrollPane1.removeAll() does not work because repopulating the jscrollpane ceases.
Is there a jscrollPane1.removeViewPortView or removeRowHeaderView?
private void PartSearchFocusGained(java.awt.event.FocusEvent evt) {
// TODO add your handling code here:
jScrollPane1.remove(listCheckBox);
jScrollPane1.remove(listDescription);jScrollPane1.repaint();
jPanel1.repaint();
}
[1017 byte] By [
RycherXa] at [2007-10-3 8:58:15]

Below are two functions: Find button clicked and PartSearch textfield focused. Currently, the jScrollPane does not clear out by simply removing 2 components. That's the only components I have in the jscrollpane
public JList listCheckBox = new JList();
public JList listDescription = new JList();
private void FindActionPerformed(java.awt.event.ActionEvent evt) {
jScrollPane1.requestFocus();
listCheckBox.setListData(buildCheckBoxItems(listData.size()));
listDescription.setListData(listData);
listCheckBox.setCellRenderer(new CheckBoxRenderer());
listCheckBox.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listCheckBox.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
int selectedIndex = listCheckBox.locationToIndex(me.getPoint());
if (selectedIndex < 0)
return;
CheckBoxItem item = (CheckBoxItem)listCheckBox.getModel().getElementAt(selectedIndex);
item.setChecked(!item.isChecked());
listDescription.setSelectedIndex(selectedIndex);
listCheckBox.repaint();
}
});
listDescription.setFixedCellHeight(20);
listCheckBox.setFixedCellHeight(listDescription.getFixedCellHeight());
jScrollPane1.add(listCheckBox);
jScrollPane1.add(listDescription);
jScrollPane1.setRowHeaderView(listCheckBox);
jScrollPane1.setViewportView(listDescription);
jScrollPane1.repaint();
getContentPane().add(jPanel1); //, BorderLayout.CENTER);
jPanel1.add(CreateFolder);
jPanel1.add(CloseButton);
jPanel1.add(jScrollPane1);
jPanel1.revalidate();
jPanel1.repaint();
jPanel1.setVisible(true);
}
private void PartSearchFocusGained(java.awt.event.FocusEvent evt) {
// TODO add your handling code here:
jScrollPane1.remove(listCheckBox);
jScrollPane1.remove(listDescription);
jScrollPane1.revalidate();
}
import java.awt.Component;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;
import javax.swing.JCheckBox;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import javax.swing.ListSelectionModel;
import javax.swing.UIManager;
/*
* NewJFrame.java
*
* Created on November 3, 2006, 11:17 AM
*/
/**
*
* @author a1025667
*/
public class NewJFrame extends javax.swing.JFrame {
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
this.jScrollPane1.requestFocus();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
jTextField1 = new javax.swing.JTextField();
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTextField1.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
jTextField1FocusGained(evt);
}
});
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.add(131, 131, 131)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 188, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(14, 14, 14)
.add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 87, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 306, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap(166, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(jPanel1Layout.createSequentialGroup()
.add(25, 25, 25)
.add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
.add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jButton1))
.add(63, 63, 63)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 147, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addContainerGap(117, Short.MAX_VALUE))
);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jTextField1FocusGained(java.awt.event.FocusEvent evt) {
// TODO add your handling code here:
jScrollPane1.remove(listCheckBox);
jScrollPane1.remove(listDescription);
jScrollPane1.revalidate();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
jScrollPane1.hasFocus();
Vector listData = new Vector();
listData.add("row1");
listData.add("row2");
listData.add("row3");
listCheckBox.setListData(buildCheckBoxItems(listData.size()));
listDescription.setListData(listData);
listCheckBox.setCellRenderer(new CheckBoxRenderer());
listCheckBox.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
listCheckBox.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent me) {
int selectedIndex = listCheckBox.locationToIndex(me.getPoint());
if (selectedIndex < 0)
return;
CheckBoxItem item = (CheckBoxItem)listCheckBox.getModel().getElementAt(selectedIndex);
item.setChecked(!item.isChecked());
listDescription.setSelectedIndex(selectedIndex);
listCheckBox.repaint();
}
});
listDescription.setFixedCellHeight(20);
listCheckBox.setFixedCellHeight(listDescription.getFixedCellHeight());
System.out.println(listDescription.isValid());
System.out.println(listCheckBox.isValid());
listDescription.repaint();
listCheckBox.repaint();
System.out.println(listDescription.isValid());
System.out.println(listCheckBox.isValid());
jScrollPane1.add(listCheckBox);
jScrollPane1.add(listDescription);
jScrollPane1.setRowHeaderView(listCheckBox);
jScrollPane1.setViewportView(listDescription);
jScrollPane1.repaint();
getContentPane().add(jPanel1); //, BorderLayout.CENTER);
jPanel1.add(jScrollPane1);
jPanel1.revalidate();
jPanel1.repaint();
jPanel1.setVisible(true);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
private CheckBoxItem[] buildCheckBoxItems(int totalItems) {
CheckBoxItem[] checkboxItems = new CheckBoxItem[totalItems];
for (int counter=0;counter<totalItems;counter++) {
checkboxItems[counter] = new CheckBoxItem();
}
return checkboxItems;
}
class CheckBoxItem {
private boolean isChecked;
public CheckBoxItem() {
isChecked = false;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean value) {
isChecked = value;
}
}
class CheckBoxRenderer extends JCheckBox implements ListCellRenderer {
public CheckBoxRenderer() {
setBackground(UIManager.getColor("List.textBackground"));
setForeground(UIManager.getColor("List.textForeground"));
}
public Component getListCellRendererComponent(JList listBox, Object obj, int currentindex,
boolean isChecked, boolean hasFocus) {
setSelected(((CheckBoxItem)obj).isChecked());
return this;
}
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JPanel jPanel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
// End of variables declaration
public JList listCheckBox = new JList();
public JList listDescription = new JList();
}>