Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
Java remove JComboBox arrow button

I added a JComboBox from the palette using netbeans IDE and populated it with contents from mysql database. I also added autocomplete functionality using swingx-all-1.6.5-1

method for populating combobox

    public void doPopulateCombo(){
    Connection con = Functions.ConnectToDB();
    try {
        Statement stmt = con.createStatement();
        String sqlQuery = "select * from products";
        ResultSet rs = stmt.executeQuery(sqlQuery);

        while(rs.next()){
            String product_name = rs.getString("Product_Name");
            cboProducts.addItem(product_name);
        }
        con.close();
        cboProducts.setSelectedIndex(-1);
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, "Error populating combo box\n"+e.toString(),
                "Error",JOptionPane.ERROR_MESSAGE);
    }
}

How do I remove the combobox's arrow button so that it appears like a textbox?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The best I can come up with is putting it into a JPanel that's slightly smaller than it, but depending on the L&F that's not really the best way to do it. Alternatively you could make a class that extends JComboBox, overrides the paint(Graphics g) method and from there calls the paint method of a textbox with the same dimensions. At least, I think you could do that, if you wanted to. Do note that you'd have to update what's in the textbox each time the user selects an option.

over 4 years ago · Santiago Trujillo Relatório

0

A hacky way to fix this would be to create your own implementation of BasicComboBoxUI and override createArrowButton()

public class NoArrowJComboBoxUI extends BasicComboBoxUI {

    @Override
    protected JButton createArrowButton() {
        JButton btn = new JButton();
        btn.setPreferredSize(new Dimension(0,0));
        btn.setVisible(false);
        return btn;
    }

}

Then in your existing code:

    cboProducts.setSelectedIndex(-1);
    cboProducts.setUI(new NoArrowJComboBoxUI());

This implementation should not interfere with autocomplete behavior, however, I haven't tested it myself with autocomplete so I can't say that with certainty.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda