Hello
I have a small problem with ListSelectionListener.
I can not copy dataList.getSelectedValue().toString(); to global String variable text.
How can I do it ?
This is inside of if statement and that is the problem.
Anyone knows how to solve it ?

ListSelectionListener lListener = new ListSelectionListener() {
			
			public void valueChanged(ListSelectionEvent e) {
				dataList = (JList) e.getSource();
				if(!(dataList.isSelectionEmpty())){
					
					text = dataList.getSelectedValue().toString();
					
					
				} else {
					
				}
					
				
				
			}
			
		};
		dataList.addListSelectionListener(lListener);

The if test can't cause that problem. Exactly what error message do you get?

Here is all code of the file_tree class.
I am coding program to display image.
But I have external class to drawImage.
I add small label to display text. But it didn't display anything.

public class file_tree extends JPanel implements ListSelectionListener{
	
	 JList dataList;
	 JLabel label1 = new JLabel();
	 ListSelectionModel listSelectionModel;
	 File my_dir;
	 String[] filenames_in_dir;
	 String text;
	 
	 int value;
	public file_tree(){
		my_dir = new File("backgrounds");
		assert(my_dir.exists()); // the directory exists
		assert(my_dir.isDirectory()); // and is actually a directory
				
		filenames_in_dir = my_dir.list();
		
		dataList = new JList(filenames_in_dir);
		text = new String();
		
		setBackground(Color.LIGHT_GRAY);
		setBorder(BorderFactory.createTitledBorder("Lista"));
		
		//dataList.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);		 
	
	
		ListSelectionListener lListener = new ListSelectionListener() {
			
			public void valueChanged(ListSelectionEvent e) {
				dataList = (JList) e.getSource();
				if((dataList.isSelectionEmpty())){
					
					
					
					
				} else {
					text = dataList.getSelectedValue().toString();
				}
					
				
				
			}
			
		};
		dataList.addListSelectionListener(lListener);		
			
		
		label1.setText(text);
		
		
		
		JScrollPane scrollPane = new JScrollPane(dataList);
		scrollPane.setPreferredSize(new Dimension(250,80));
		scrollPane.setViewportView(dataList);
		
		add(scrollPane);
		add(label1);
		 
		 
	}
	
	
	
	@Override
	public void valueChanged(ListSelectionEvent e) {
		
		
	}
	
	
	
	
	
}

I need to get value from dataList.getSelectedValue().toString(); and put to variable from next drawImage

How can i do this ?

import javax.swing.*;

import java.awt.image.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class drawImage extends JPanel implements ListSelectionListener{
	String FileName = new String();
	file_tree first = new file_tree();
	JLabel picLabel = new JLabel();
	ImageLabel label;
	
	public drawImage() {
		ListSelectionListener lListener = new ListSelectionListener() {
			
			public void valueChanged(ListSelectionEvent e) {
				first.dataList = (JList) e.getSource();
				
				if((first.dataList.isSelectionEmpty())){
					
					
					
					
				} else {
					
					
					
					//;
					label = new ImageLabel(new ImageIcon(first.dataList.getSelectedValue().toString()));
					
					
					
				}  
									
				
			}
			
		};
		first.dataList.addListSelectionListener(lListener);	
		
		add(label);
	}
	
	
	class ImageLabel extends JLabel {

		  public ImageLabel(String img) {
		    this(new ImageIcon(img));
		  }

		  public ImageLabel(ImageIcon icon) {
		    setIcon(icon);
		    // setMargin(new Insets(0,0,0,0));
		    setIconTextGap(0);
		    // setBorderPainted(false);
		    setBorder(null);
		    setText(null);
		    setSize(icon.getImage().getWidth(null), icon.getImage().getHeight(null));
		  }

		}

	@Override
	public void valueChanged(ListSelectionEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	
	
	
}

How can we possibly answer that? You haven't told us what next is or what drawimage is or where they are.

sorry for that
I edited my last post and copied my code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.