hi all,i have a java project, i want to read records from a data base(access) and then put them in a container with check box within each record..but my code is only displaying the last record of the table..i want each record to be displayed on a line..please help me it's soo urgent :S ....my function's code is:

Statement Sa=null;
         ResultSet RSa=null;
         Connection connecti=null;
      
         try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            connecti=DriverManager.getConnection("jdbc:odbc:test1","","");
                 
            Sa=connecti.createStatement();
            RSa=Sa.executeQuery("Select RoomType,Costperday From clientchoices");
            
            content=getContentPane();
            content.setBackground(Color.BLUE);
            content.setForeground(Color.RED );
            content.setLayout(null);
            String out="";
            JLabel t1=new JLabel();
            t1.setBounds(20,20,200,200);
            while(RSa.next()){
            
               out=RSa.getString("RoomType")+"\t"+
                  RSa.getString("Costperday");
               t1.setText(out);
               content.add(t1);
            
            }
            setTitle("Personnel Information");
            setSize(600,600);
            setVisible(true);
            JFrame.setDefaultLookAndFeelDecorated(true);
         
         
         }
         
         
                    
            catch(Exception ee1){
                    
            
            
            
               System.out.println("Erreur " +ee1);
            }
      
      }

thnx in advance

Recommended Answers

All 11 Replies

It is displaying every row but

t1.setBounds(20,20,200,200);

when you put every label in exactly the same place you only see the last one.

I assume you are using the "so-called" null layout? If so, why? That is, usually, the worst thing you can do to your GUI. Don't use it unless you have a really good reason, and you know what you are doing.

thnx masijade..... but what should i do to solve this problem .....i don't know .......how should i change my code to fit what i need...

Well, lets look at what you're doing

t1.setBounds(20,20,200,200);

What is the first 20 there?
What is the second 20?
What is the first 200?
What is the second 200?

P.S. Look at the API docs for Component.
Once you know what those stand for think about how someone might shift the next label down far enough to be below (rather than on top of) the previous label. Then try something. If you don't get it right post what you tried and we'll help you correct it.

-the first 20 is the distance from the left border of the container to the label
-the second 20 is the distance from the top border of the container to the label
-the first 200 is the width of the label
-the second 200 is the height of the label

Okay, so if the first label is at 20 and has a height of 200 where does the next label need to be?

Edit: And, there is also the fact that you are using the same label everytime, you, obviously, need to move the creation (and, of course the bound setting) inside the while loop.

hi ...i made what u told me to do ....and now ...all the records are shown but in the same place ..all of them are gathering in the same place in the same label .. i tried to make a \n after creating each label ...but it did not works .......what to do know?

Post your new code.

i found out a way ;) to solve this problem but now i have a nother one ...i made the container with each record and with the JButtons with each record ...but when i want to calculate the total cost for the client ...the program does not run if i did not check all the checkboxes ...but what i wanted is to check one checkbox and select the cost of the checkbox checked and display the total for the client....help me please
my new code is:

public void create(){
              
         Statement Sa=null;
         ResultSet RSa=null;
         Connection connecti=null;
      
         try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            connecti=DriverManager.getConnection("jdbc:odbc:test1","","");
                  //System.out.println("Connexion avec la base " + DB + " établie");
               // création d'un objet Statement
            Sa=connecti.createStatement();
               // exécution d'une requête select
            RSa=Sa.executeQuery("Select * From clientchoices");
         
            content=getContentPane();
           // content.setBackground(Color.BLUE);
            content.setForeground(Color.RED );
            content.setLayout(null);
            String out="";
         
         
         
         //t2.setText("<html><u>Roomtypes</u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<u>CostPerday</u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<u>NbValidRoom</u><br></html>!");
         
            int a=30;
            int b=40;
            int c=350;
            int d=50;
            JLabel t2=new JLabel();
            t2.setBounds(30,20,400,50);
            t2.setText("<html><u>Roomtypes</u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<u>CostPerday</u>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<u>NbValidRoom</u><br>");
            content.add(t2);
         
            JLabel fumeur=new JLabel();
            fumeur.setText("<html><font color=black><u><i>Fumeur</u></i></font></html>");
            fumeur.setBounds(350,80,50,20);
            content.add(fumeur);
         
            yes=new JCheckBox();
            yes.setBounds(330,110,50,15);
            yes.setText("Yes");
            content.add(yes);
         
            no=new JCheckBox();
            no.setBounds(390,110,40,15);
            no.setText("No");
            content.add(no);
         
            JLabel view=new JLabel();
            view.setText("<html><u><i>Room View</u></i></html>");
            view.setBounds(340,150,80,20);
            content.add(view);
         
            JCheckBox poll=new JCheckBox();
            poll.setBounds(300,180,50,15);
            poll.setText("Poll");
            content.add(poll);
         
            JCheckBox ocean=new JCheckBox();
            ocean.setBounds(350,180,70,15);
            ocean.setText("Ocean");
            content.add(ocean);
         
            JCheckBox mountain=new JCheckBox();
            mountain.setBounds(420,180,80,15);
            mountain.setText("Mountain");
            content.add(mountain);
         
         
         
            while(RSa.next()){
               room=new JCheckBox();
               room.setBounds(3,b+17,100,15);
               room.setText( RSa.getString("RoomType"));
               content.add(room);
            
               JLabel t1=new JLabel();
               t1.setBounds(a,b,c,d);
               out="                                     "+RSa.getString("Costperday") + "                       "+ RSa.getString("Nbvalid") ;
               t1.setText(out + "\n");
               content.add(t1);
            
            //	a+=30;
               b+=30;
            
            
            }
            JButton choice=new JButton();
            choice.setText("Choice");
            choice.setBounds(150,350,90,40);
            content.add(choice);
            choice.addActionListener(
                                 
                                 
                                            
                                    new ActionListener (){                                   
                                            
                                    
                                    
                                    
                                    
                                               
                                       public void actionPerformed(ActionEvent event){ // event:de type Actionevent
                                               
                                       
                                       
                                       
                                       
                                          try{
                                          
                                             Calculate(event);
                                          
                                          }
                                          
                                          
                                                     
                                             catch(Exception e){
                                                     
                                             
                                             
                                                System.out.print("Erreur3");
                                             }
                                       }
                                    }
                                 );
         
            JTextField total=new JTextField();
            total.setBounds(270,350,90,40);
            total.setEditable(false);
            content.add(total);
         
            setTitle("Client Choices");
            setSize(600,600);
            setVisible(true);
            JFrame.setDefaultLookAndFeelDecorated(true);
         
         
         }
         
         
                    
            catch(Exception ee1){
                    
            
            
            
               System.out.println("Errur annuler insc" +ee1);
            }
      
      }
              
      public void Calculate(ActionEvent event){
              
      
         if((!room.isSelected())){
            JOptionPane.showMessageDialog(null,"Make Your Choice");
         }
         
         else {
            int total=0;
            if(room.isSelected()){
            
               Statement stm=null;
               ResultSet RS1=null;
               Connection connect1=null;
            
               try{
                  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                  connect1=DriverManager.getConnection("jdbc:odbc:test1","","");
                  //System.out.println("Connexion avec la base " + DB + " établie");
               // création d'un objet Statement
                  stm=connect1.createStatement();
               // exécution d'une requête select
                  RS1=stm.executeQuery("Select Costperday From clientchoices where RoomType="+ room.getText());
               }
                          
                  catch(Exception e){
                          
                     System.out.println("EEEEEEEERRRRRRRRRRROr"+ e);
                  }
               System.out.println("T"+ RS1);
            }
         }
      
      
      
   
      }

that's the code with the calculate function ........please help as soon as possible ......thnx

where are u ? i need urgent help please

@weblover : The thing is not that only when all the checkboxes are selected the code would work. Only when the last checkbox that you add is selected, that the code would work.
Reason : Something similar to what you were doing with your placement of labels earlier. You use the same room (JCheckBox) object to store all the instances of the generated checkboxes, so that 'room' contains the reference to the last such object added. Now when you check room.isSelected() it checks whether the last room is selected and would obviously won't work if it isn't. Now when you select all rooms/check boxes naturally the last one is selected and your code works. But when you select anything other than it, your code won't work.

You need to keep a list of all the generated checkbox instances and check which all are selected, and do the calculation of the total depending on it.

Edit : Just a word of wisdom, using words such as urgent and phrases such as "as soon as possible" would only make your helpers ignore you. Nobody here is obligated to help anyone, it is a matter of choice and nobody likes to be pushed to do charity.

ok .....thank you very much verruckt24 ...but i'am new user here in this forum and i don't know this ........i say sorry to all of you .....really sorry .....

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.