Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JList problem+double click

user8744860Jan 23 2010 — edited Jan 24 2010
hello
I am trying to make a Chat server, for this i am using JList for the friend list and all users. but when i try to add more than 1 friend , my window hangs. Dont know why. also the double click is not getting enabled in dlist.
my code looks like
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
import java.net.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;

public class msgwin extends JFrame implements MouseListener,ListSelectionListener
{
  JFrame jf;
  DefaultListModel defau1=new DefaultListModel();
  DefaultListModel defau2=new DefaultListModel();
  JList slist=new JList(defau1);
  JList dlist=new JList(defau2);
  JLabel slabel,dlabel;
  public JButton sub;
  String name;
  BufferedReader reader;
  PrintWriter writer;
  Socket sock;
  public JTextArea msg;
  public JTextArea ja;
  msgwin()
  {
   jf=new JFrame("Grape Chat v1.2 beta");
   //jf.setBounds(40,30,500,400);
   jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   JFrame.setDefaultLookAndFeelDecorated(true);
   GridBagLayout layout=new GridBagLayout();
   jf.setLayout(layout);
   jf.setResizable(true);
   jf.setSize(450,500);
   slabel=new JLabel("ALL USERS");
   dlabel=new JLabel("MY GANG");
   sub=new JButton("Submit");
    msg=new JTextArea(3,8);
   msg.setEditable(true);
   msg.setLineWrap(true);
   JScrollPane textar=new JScrollPane(msg,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
   
   try
   {
        Class.forName("oracle.jdbc.driver.OracleDriver");
	Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","SCOTT","iamakhila");
	Statement st=con.createStatement();
	ResultSet rs=st.executeQuery("select firstname from data");
	
	//defau1=new DefaultListModel();
	
	while(rs.next())
	{
	 name=rs.getString("firstname");
	 
	 defau1.addElement(name);
	}
   
 
   
     

   slist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
   dlist.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  slist.setSelectedIndex(0);
   slist.addListSelectionListener(this);
   slist.addMouseListener(this);
   slist.setVisibleRowCount(10);
   slist.setFixedCellWidth(22);
   
   st.close();
   con.close();
   
  }catch(Exception e){}
   
  JScrollPane listScrollPane = new JScrollPane(slist);  
   
  
  dlist.setVisibleRowCount(10);
  JScrollPane dscroll=new JScrollPane(dlist);
  
  ja=new JTextArea(4,8);
  JScrollPane textar1=new JScrollPane(ja,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
   ja.setLineWrap(true);
  ja.setEditable(false);
  jf.add(slabel,new GBC(0,0).setAnchor(GBC.WEST).setInsets(10));
  jf.add(dlabel,new GBC(4,0,1,1).setAnchor(GBC.EAST).setInsets(10));
  jf.add(textar1,new GBC(1,0,2,2).setFill(GBC.BOTH).setWeight(100,100).setInsets(10));
  jf.add(textar,new GBC(1,4,1,1).setFill(GBC.BOTH).setWeight(100,100).setInsets(10));
  jf.add(listScrollPane,new GBC(0,1,1,1).setFill(GBC.BOTH).setWeight(100,100).setInsets(10));
  jf.add(dscroll,new GBC(4,1,1,1).setFill(GBC.BOTH).setWeight(100,100).setInsets(10));
  jf.add(sub,new GBC(1,5,1,1).setInsets(10));
  final JButton del=new JButton("Delete Friend");
  jf.add(del,new GBC(4,2,1,1).setInsets(10));
  
  ActionListener listener=new ActionListener(){
  public void actionPerformed(ActionEvent event)
  {
   //if(event.getSource()==sub)
    
     /*chatclient c1=new chatclient();
	 c1.abc();
	 c1.go();*/
   
   
   if(event.getSource()==del)
   {
      int select=JOptionPane.showConfirmDialog(jf,"Do yo want to remove the selected person from your friends??..","Confirm dialog",JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE);
     if(select==JOptionPane.OK_OPTION){
     String str=(String)dlist.getSelectedValue();
	 defau2.removeElement(str);}
	} 
  }
 };
//sub.addActionListener(listener); 
del.addActionListener(listener); 
  jf.setVisible(true);
 }

 public void valueChanged(ListSelectionEvent e)
 {
 }
  public void mouseClicked(MouseEvent e)
  {
   
   if((JList)e.getSource()==slist)
   {//=(JList)e.getSource();
   Object index=slist.getSelectedValue();
   
   if(e.getClickCount()==2)
   {
     int selection=JOptionPane.showConfirmDialog(jf,"Do yo want to add the selected person as your friend??..","Confirm dialog",JOptionPane.YES_NO_OPTION,
JOptionPane.PLAIN_MESSAGE);
     if (selection==JOptionPane.YES_OPTION)
	 {
	  boolean flag=false;
	  String str="";
	  
	  while(str!=null)
	  {
	   int i=0;
	   dlist.setSelectedIndex(i);
	   str=(String)dlist.getSelectedValue();
	   if(index==str)
	   {flag=true;
	   JOptionPane.showConfirmDialog(jf,"Selected person is already your friend!!","Exit",JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE);
         break;
		}}
     if(!flag)
     defau2.addElement((String)index);
	}
  }
  }
  
  if((JList)e.getSource()==dlist)
  {
  
  if(e.getClickCount()==2)
  {
  Object val=dlist.getSelectedValue();
   try
   {
    Class.forName("oracle.jdbc.driver.OracleDriver");
	Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:orcl","SCOTT","iamakhila");
	Statement st=con.createStatement();
	ResultSet rs=st.executeQuery("select firstname from data");
	
	//defau1=new DefaultListModel();
	
	while(rs.next())
	{
	 name=rs.getString("firstname");
	 if(name==val)
	 {
	   ma m=new ma();
     }
   
   }
  }catch(Exception xxx)
  {}
  }
  }
  }
 public void mouseEntered(MouseEvent e){}
  public void mouseExited(MouseEvent e){}
  public void mousePressed(MouseEvent e){}
  public void mouseReleased(MouseEvent e){}
  


    
      
  public static void main(String args[])
  {
     msgwin ms=new msgwin();
  }
 }
 
 
 class GBC extends GridBagConstraints
{
 public GBC(int gridx,int gridy)
 {
  this.gridx=gridx;
  this.gridy=gridy;
 }

 public GBC(int gridx,int gridy,int gridwidth,int gridheight)
 {
  this.gridx=gridx;
  this.gridy=gridy;
  this.gridwidth=gridwidth;
  this.gridheight=gridheight;
 }

 public GBC setAnchor(int anchor)
 {
  this.anchor=anchor;
  return this;
 }

 public GBC setFill(int fill)
 {
  this.fill=fill;
  return this;
 } 
 
 public GBC setWeight(double weightx,double weighty)
 {
  this.weightx=weightx;
  this.weighty=weighty;
  return this;
 } 
 
 public GBC setInsets(int top, int left, int bottom, int right) 
 {
  this.insets = new Insets(top, left, bottom, right);
  return this;
 } 

 public GBC setInsets(int distance)
 {
  this.insets=new Insets(distance,distance,distance,distance);
  return this;
 }

 public GBC setIpad(int ipadx,int ipady)
 {
   this.ipadx=ipadx;
   this.ipady=ipady;
   return this;
 } 
}  
    	
	
     
please help me. i am really stuck.i know the problem is in while loop but i am not getting what it to replace with.i have tried all the variations.

thanking in advance
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 21 2010
Added on Jan 23 2010
9 comments
272 views