2

I have more jlabels in a jpanel, I have to highlight the required jlabel at run time.

-> have created a search button and text field

I want to do following things

-> User typing some text in a text field -> User enters a search button -> If the typed text match with the text of any jlabels in a jpanel, the particular jlabel should highlight.

I have tried jpanel's getComponent of method but it not working

Help me to create a search bar for jpanel components search

lekshmi
  • 191
  • 1
  • 1
  • 6

1 Answers1

0

Is this what you are looking for?

String userInput = "myvalue";
JPanel panel = new JPanel();

for(Component component : panel.getComponents()){
    if(component instanceof JLabel){
        JLabel label = (JLabel) component;
        if(label.getText().contains(userInput)){
            // Mark the label
        }
    }
}
Boris Schegolev
  • 3,601
  • 5
  • 21
  • 34
  • Thanks a lot, It working. But I need to scroll the panel to the position where label positioned. I have changed the position of a J scroll pane but it not working . – lekshmi Sep 27 '16 at 12:46