简体   繁体   中英

add image when a jlist item is selected

I have in doubt. I don't know how my program don't work.

import br.com.operacao.Paga;

public class Tela extends JPanel{
    JLabel image;
  public Tela() {

    this.setLayout(new GridBagLayout());


    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH; 
    c.insets = new Insets(5, 5, 5, 5); 


    String labels[] = { "Coca-Cola", "Fanta Laranja", "Fanta-Uva", 
     "Sprite"};
    final JList<String> list = new JList<String>(labels);
    list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    list.setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
    list.setSelectedIndex(0);


    JScrollPane pane = new JScrollPane();
    pane.getViewport().add(list);
    JPanel firstpanel = new JPanel();
    firstpanel.add(pane);
    firstpanel.setBackground(Color.BLACK);
    c.gridx = 0;
    c.gridy = 0;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.weightx = c.weighty = 0.0;
    this.add(firstpanel,c);

    image = new JLabel();
    image.setFont(image.getFont().deriveFont(Font.ITALIC));
    image.setHorizontalAlignment(JLabel.CENTER);
    updateLabel(labels[list.getSelectedIndex()]); //there is a error here but why?
    image.setBorder(BorderFactory.createEmptyBorder(10,0,0,0));

    c.gridx = 1;
    c.gridy = 0;
    c.gridwidth = 1;
    c.gridheight = 1;
    c.weightx = c.weighty = 0.0;
    this.add(image, c);

    JScrollPane js = new JScrollPane();
    js.setPreferredSize(new Dimension(110,110));
    c.gridx = 0;
    c.gridy = 2;
    c.gridwidth = 3;
    c.gridheight = 1;
    this.add(js, c);

    final JButton comprar = new JButton("Comprar");
    comprar.setEnabled(false);
    list.addListSelectionListener(new ListSelectionListener() {

        public void valueChanged(ListSelectionEvent e) {
            int selections[] = list.getSelectedIndices();
            //String selectedValue = list.getSelectedValue();
            Object selectionValues[] = list.getSelectedValues();
            for (int i = 0, n = selections.length; i < n; i++) {
                if (i == 0) {
           System.out.println("Value" + selectionValues[i] );
                }}
           comprar.setEnabled(true);

        }
    });
    c.gridx = 0;
    c.gridy = 4;
    c.gridwidth = 1;
    c.gridheight = 1;
    this.add(comprar, c);
    comprar.addActionListener(new Paga());

    final JButton confirma = new JButton("Confirmar");
    confirma.setEnabled(false);
    c.gridx = 1;
    c.gridy = 4;
    c.gridwidth = 1;
    c.gridheight = 1;
    this.add(confirma,c);


  }

  public void actionPerformed(ActionEvent e) {

        JList<String> jl = (JList<String>)e.getSource();
        int refriName[] = jl.getSelectedIndices();
        updateLabel(refriName); //what is this error? 
    }
    protected void updateLabel(String name) {
        ImageIcon icon = createImageIcon("images/" + name + ".jpg");
        image.setIcon(icon);
        image.setToolTipText("A drawing of a " + name.toLowerCase());
        if (icon != null) {
            image.setText(null);
        } else {
            image.setText("Image not found");
        }
    }

    /** Returns an ImageIcon, or null if the path was invalid.*/ 
    protected static ImageIcon createImageIcon(String path) {
        java.net.URL imgURL = Tela.class.getResource(path);
        if (imgURL != null) {
            return new ImageIcon(imgURL);
        } else {
            System.err.println("Couldn't find file: " + path);
            return null;
        }
    }

}

I need to add in my gui a image relative a selected item in my jlist... but i don't know whats the problem in my implemments... in my project java there is a folder images and the following images.

这是我的计划

When you call...

updateLabel(labels[list.getSelectedIndex()]);

From within the constructor, it is likely that there is nothing yet selected, this means that list.getSelectedIndex() will return -1 is not a valid array index...

A better solution might be to pass the selected index to the updateLabel method and allow it to perform some sanity checking ( >= 0 && < lables.length ) for example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM