0

Hi i created a swing application which contain combobox on contentpane..My need is when i click any combobox textbox must be removed and replace a combo box.!

  txtSearch = new JTextField();
            txtSearch.setBounds(10, 11, 107, 22);
            getContentPane().add(txtSearch);
            txtSearch.setColumns(10);

 category = new JRadioButton("Search By Category");
    category.setActionCommand("category");
    buttonGroup.add(category);
    category.setBounds(10, 118, 140, 23);
    getContentPane().add(category);

mouse clieck event for category radio button

@Override
            public void mouseClicked(MouseEvent arg0) {

                getContentPane().remove(txtSearch);


                getContentPane().add(comboCategory);


            }

mouse click event for title radio button

@Override
            public void mouseClicked(MouseEvent e) {
            getContentPane().add(txtSearch);

                getContentPane().remove(comboCategory);

                //getContentPane().add(txtSearch);


            }

i already done click event for category radio button.it working perfectly But when i click title radio button it does not change back to text box..!!

user3318622
  • 167
  • 2
  • 3
  • 11
  • 3
    *"textbox must be removed and replace a textbox.!"* What? Why replace one 'textbox' with another? It's better just to change the text in the existing text box. Other tips: 1) For better help sooner, post a [MCTaRE](http://stackoverflow.com/help/mcve) (Minimal Complete Tested and Readable Example). .. – Andrew Thompson Feb 24 '14 at 10:36
  • 2
    .. 2) Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). .. – Andrew Thompson Feb 24 '14 at 10:37
  • 3
    .. 3) For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Feb 24 '14 at 10:37
  • 1
    I'd add 4) For JRadioButton, JButton or JCheckBox don't use MouseListener but ActionListener to listen when the button is pressed. See [How to Use Buttons, Check Boxes, and Radio Buttons](http://docs.oracle.com/javase/tutorial/uiswing/components/button.html) – dic19 Feb 24 '14 at 13:42
  • @dic19 Yes, that is a worthy point (4). – Andrew Thompson Feb 24 '14 at 23:28

0 Answers0