D.Jones D.Jones - 6 months ago 10
Java Question

JButton appears after selection of value in JList

I have a JList with some elements and I want to have a JButton appears only when the user select a value in the list. This is what I have tried :

public void itemStateChanged(ItemEvent event) {
int choice = avDevBox.getSelectedIndex();
if (choice = 0) {
list = new JList(listModel1);
list.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent ev1) {
if (!ev1.getValueIsAdjusting()) {
String outputext = list.getSelectedValue().toString();
itemDetails.setText(outputext);
}
}

});
add(list);
add(itemDetails);
add(but1);
}


Is there any way to do this ?

Thanks in advance.

Answer

You can have a JButton already on your GUI already but make it invisible via myButton.setVisible(false). Then within your JList's ListSelectionListener call myButton.setVisible(true). You'll then want to call revalidate() and repaint() on the container (often a JPanel) that holds the now-visible JButton.

Note that for future questions you will want to create and post more pertinent code, preferably a Minimal, Complete, and Verifiable Example Program, as this would allow us to see why your current code is not working and would help us to answer well and with confidence.

For an example of an MCVE:

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

@SuppressWarnings("serial")
public class McveExample extends JPanel {
    private static final String[] DATA = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    private static final int PREF_W = 400;
    private static final int PREF_H = 250;
    private JList<String> list = new JList<>(DATA);
    private JButton myButton = new JButton();

    public McveExample() {

        myButton.setVisible(false);  // **** make button invisible

        list.setVisibleRowCount(4);
        list.addListSelectionListener(new ListListener()); // listener that does the dirty work

        JScrollPane scrollPane = new JScrollPane(list);

        // add a JButton that resets myButton back to being invisible
        add(new JButton(new ResetAction("Reset", KeyEvent.VK_R)));
        add(scrollPane);
        add(myButton);
    }

    // make sure things are big enough
    @Override
    public Dimension getPreferredSize() {
        Dimension superSz = super.getPreferredSize();
        if (isPreferredSizeSet()) {
            return superSz;
        }
        int prefW = Math.max(superSz.width, PREF_W);
        int prefH = Math.max(superSz.height, PREF_H);
        return new Dimension(prefW, prefH);
    }

    private class ResetAction extends AbstractAction {
        public ResetAction(String name, int mnemonic) {
            super(name);
            putValue(MNEMONIC_KEY, mnemonic);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            list.clearSelection();
            myButton.setVisible(false);
            McveExample.this.revalidate();
            McveExample.this.repaint();
        }
    }

    private class ListListener implements ListSelectionListener {
        @Override
        public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
                JList<String> lst = (JList<String>) e.getSource();
                String selection = lst.getSelectedValue();
                if (selection != null) {
                    myButton.setText(selection);
                    myButton.setVisible(true);
                    McveExample.this.revalidate();
                    McveExample.this.repaint();
                }
            }
        }
    }

    private static void createAndShowGui() {
        McveExample mainPanel = new McveExample();

        JFrame frame = new JFrame("McveExample");
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> createAndShowGui());
    }
}