Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to add element to JList and how to add that JList to JScrollPane...?
In code I have marked the message dialog box with three ***. I want to state that when i am including this dialog box the result is displayed correctly and there is no change in the values of the listbox if I hide the dialog box .... where is the problem....Its mererly a dialog box

What I have tried:

for(File fr:fp.listFiles())
		{
			if(fr.getName().equals(fq.getName()))
			{
				//JOptionPane.showMessageDialog(null,"Same");
				String gg="";
				DefaultListModel<String> model3=new DefaultListModel<>();
				try
				{
					FileReader fread=new FileReader(fr);
					BufferedReader bread=new BufferedReader(fread);
					String line=bread.readLine();
					char []po=line.toCharArray();
					int ih=0;int pop=0;
					int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
					int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
					JList<String> kl;		
					for(ih=0;ih<line.length();ih++)
					{
						//JOptionPane.showMessageDialog(null,po[ih]);
							gg="";
						while(po[ih]!='.')
						{
							gg +=po[ih];
							ih++;
						}
						JOptionPane.showMessageDialog(null,"gg "+gg); ***
						model3.addElement(gg);
						kl=new JList<>(model3);
						kl.setVisible(true);
						gg="";	
						kl.setLayoutOrientation(JList.VERTICAL);
						this.remove(jsp3);
						JScrollPane jsp6=new JScrollPane(kl,v,h);
						jsp6.setVisible(true);
						jsp6.setBounds(550,170,180,110);
						//jsp6.updateUI();
//						this.add();
						this.add(jsp6);
						
						bread.close();fread.close();kl=null;	
					}
				
				}
				catch(Exception e)
				{
					//model=null;	
				}
				//jsp6=null;
				

			}
		}
Posted
Updated 27-Dec-22 23:40pm
v3

 
Share this answer
 
v2
Comments
Member 12712527 27-Dec-22 10:42am    
Sir please relieve me from this astonishment that what I commented. why the result is displayed correctly when i use a dialog box...?
Member 12712527 27-Dec-22 10:46am    
Sir i have gone through that website previously
Richard MacCutchan 27-Dec-22 10:47am    
See my updated suggestion.
Member 12712527 27-Dec-22 10:52am    
Done the same thing but the problem is why not correct result is comming when i hide a mere messagebox the result is displayed correctly only when i included the messagebox...why
Richard MacCutchan 27-Dec-22 11:03am    
We cannot guess what that is supposed to mean. Please use the Improve question link above, and add complete details of what is not working.
I have mocked up a cut down version that will display the items from first line of any file. There are obviously many things that need to be added in order to provide what you want the code to do. However, without a detailed explanation I am unable to guess what that is, so this is just a starting point:
Java
// java Program to create a simple JList
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import java.io.*;

class cp extends JFrame
{
    //main class
    public static void main(String[] args)
    {
        //create a new frame
        cp cpFrame = new cp();
        cpFrame.setData();
    }

    private void setData() {
        //set the size of frame
        this.setSize(400,400);
        //create a panel
        JPanel panel =new JPanel();
         
        //create list
        JList list = null;
        
        // for(File fr:fp.listFiles()) //  ***** I am not sure what all these files are supposed to be
		{
			// if(fr.getName().equals(fq.getName()))
			{
                list = getList(new File("cp.txt"));
            }
        }         
        //add list to panel
        if (list != null) {
            int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
            int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
            list.setLayoutOrientation(JList.VERTICAL);
            JScrollPane jsp6 = new JScrollPane(list, v, h);
            this.add(jsp6);
        }
          
        this.show();
    }

    private JList getList(File fr) {
        String gg="    ";
        DefaultListModel<String> model3=new DefaultListModel<>();
        try
        {
            FileReader fread=new FileReader(fr);
            BufferedReader bread=new BufferedReader(fread);
            String line=bread.readLine(); // ***** this code processes only the first line of the file
            char []po=line.toCharArray();
            int ih=0;
            int pop=0;
            int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;
            int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;
            JList<String> kl;		
            for(ih = 0; ih < line.length(); ih++)
            {
                //JOptionPane.showMessageDialog(null,po[ih]);
                gg="    ";
                while(po[ih] != '.')
                {
                    gg +=po[ih];
                    ih++;
                }
                model3.addElement(gg);
                gg="    ";
            }
            kl=new JList<>(model3);
            return kl;
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());	
        }
        return null;
    }

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900