-1

There is a JPanel with buttons and textField. TextField filters unnecessary buttons by name. But after delete the size of rest buttons is changing. The height of buttons must not change after filtering in the textField. How to achieve this?

public class TestViewer {
public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {


public void run() {

JDialog dialog = new TestDialog();  
dialog.setLocationRelativeTo(null);
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setTitle("Type text and press Enter");
dialog.setSize(300, 700);
dialog.setVisible(true);
dialog.setLocationRelativeTo(null);
dialog.setModal(true); 
}
});
}
}

class TestDialog  extends JDialog {

public TestDialog() {
    getContentPane().add(new JPan

el(), BorderLayout.CENTER);
    getContentPane().add(createBtnPanel(), BorderLayout.CENTER);
}



 private JPanel createBtnPanel() {
        int n = 100; 
    Final String ArrButtonNames[] = new String[n];  
for (int h = 0; h < ArrButtonNames.length; h++) {
    if ((h%2)==0){
        ArrButtonNames[h]="Filter"+h;
    }
    else{
        ArrButtonNames[h]="Button"+h;
    }
}

final JButton ArrButton[] = new JButton[n];
final JPanel btnPanel = new JPanel(new GridLayout(0, ArrButtonNames.length, 1,1));
btnPanel.setLayout(new GridLayout(0, 1));
final JTextField textField = new JTextField();  
textField.setColumns(23);
btnPanel.add(textField);
for (int i = 0; i < ArrButtonNames.length; i++) {  
    String btnString = ArrButtonNames[i];
    JButton button = new JButton(btnString);

    Dimension d = button.getPreferredSize();
    d.setSize(d.getWidth(), d.getHeight()*1);  
    button.setPreferredSize(d);
    button.setSize(d);
    button.setMinimumSize(d);
    button.setMaximumSize(d);   

   btnPanel.add(button);
   ArrButton[i] = button;
}
textField.addActionListener(new ActionListener() {  
public void actionPerformed(ActionEvent e) {
    for(int  k = 0; k < ArrButtonNames.length; k++) { 
        if(ArrButtonNames[k].indexOf(textField.getText())==-1) {   
            btnPanel.remove(ArrButton[k]);
            btnPanel.revalidate();
            btnPanel.repaint();
        }
    }
}   
});

JPanel MainPanel = new JPanel();
MainPanel.setLayout(new BorderLayout());
MainPanel.add(btnPanel, BorderLayout.NORTH);
final JScrollPane scrollPane = new JScrollPane(btnPanel);
MainPanel.add(scrollPane, BorderLayout.CENTER);
scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
    return MainPanel;
}
}
CRABOLO
  • 8,605
  • 39
  • 41
  • 68

1 Answers1

1

You have that effect, because you use GridLayout, which resize components to fill whole cell.

For your purposes I recommend you to use GridBagLayout, that can do what you want.

Try to use next code for filling your btnPanel instead of yours:

    final JButton ArrButton[] = new JButton[n];
    final JPanel btnPanel = new JPanel();
    GridBagConstraints c = new GridBagConstraints();
    c.anchor = GridBagConstraints.WEST;
    c.fill = GridBagConstraints.HORIZONTAL;
    c.weightx = 1;
    c.gridx=0;
    c.gridy=0;
    btnPanel.setLayout(new GridBagLayout());
    final JTextField textField = new JTextField();
    btnPanel.add(textField,c);
    for (int i = 0; i < ArrButtonNames.length; i++) {
        String btnString = ArrButtonNames[i];
        JButton button = new JButton(btnString);
        c.gridy ++;
        btnPanel.add(button,c);
        ArrButton[i] = button;
    }
    c.fill = GridBagConstraints.BOTH;
    c.weighty = 1;
    c.gridy ++;
    btnPanel.add(new JLabel(""),c);

And it looks like:

enter image description here

ALso use pack() method instead of setSize(...), and don't use setPreferedSize(...) and others size methods read about that here.

Community
  • 1
  • 1
alex2410
  • 10,904
  • 3
  • 25
  • 41