Hello forum:
I have to set the position of 4 labels, though in this example I'm only starting with one. for some reason even though I do setLocation(40,50) it is not working. Any ideas?

The code of the small part ; If you guys want the entire program Ill post it. Thanks

static JLabel label,label2; static JTextField search;	 static JButton button;
      	
       public Driver(){
           setLayout(new FlowLayout());
           
         label  = new JLabel("Search");
         add(label);
         
        
      	search= new JTextField("Name of the snake");
        add(search);
             	
      	button = new JButton("Find");
        button.setHorizontalAlignment(SwingConstants.CENTER);
      	add(button);
      	 
         
          label2  = new JLabel("Name");
          label2.setLocation(10,50);
          label2.setHorizontalAlignment(SwingConstants.CENTER);
          add(label2);
           
      }

Try calling repaint() after setting location

Are you fighting with the layout manager? Who decides where a component is placed?
I don't know if you can split the job of doing layouts.

Oh yes. He is using a flow layout. Didnt notice that. Yes, me thinks so too. The Layout manager will decide the location if you use a LayoutManager. If you have limited amount of components why not handcode their locations? Or you can seperate the area in which the snakes appear into a new panel which has no LayoutManager and you can play around as u like in that panel.

But unless you can tell what ure doing in your program we cant give concrete suggestions...

Are you fighting with the layout manager? Who decides where a component is placed?
I don't know if you can split the job of doing layouts.

So your telling me that I shouldn't do it?

Oh yes. He is using a flow layout. Didnt notice that. Yes, me thinks so too. The Layout manager will decide the location if you use a LayoutManager. If you have limited amount of components why not handcode their locations? Or you can seperate the area in which the snakes appear into a new panel which has no LayoutManager and you can play around as u like in that panel.

But unless you can tell what ure doing in your program we cant give concrete suggestions...

Well I'm reading from a snakes class which has the instances name, venomous, age, and weight.( Read from a text file).
I'm storing the variables into a class array I believe is called. Everything until that point is fine.

But I want a layout that looks like this.
search:Type name of snake here, then click search // After the user types a name show results in this order and well positioned)
Name:blahblah
venomous:true/false
age:3
weight:45

The actual values of the class will be placed there.

You should turn off the layout manager if you want to set the components' locations.

You should turn off the layout manager if you want to set the components' locations.

Even when I do this doesn't change

label2  = new JLabel("Name");
          label2.setLocation(10,50);
          label2.setHorizontalAlignment(SwingConstants.CENTER);
          add(label2);

Make a small simple program that compiles and executes that shows the problem.
Your 5 lines of code are not enough to see what the problem is.

Make a small simple program that compiles and executes that shows the problem.
Your 5 lines of code are not enough to see what the problem is.

Here I customize a couple of things so the button might not work, but I'm just trying to move the label "Name:" .

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author SOAD
 */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 import java.util.*;
   import java.io.*;
   import javax.swing.*;


    public class Driver2 extends JPanel {
    
      static  snakes[] snakesArray = new snakes[4];
      static JLabel label,label2; static JTextField search;	 static JButton button;
      	
       public Driver2(){
                    
         label  = new JLabel("Search");
         add(label);
         
        
      	search= new JTextField("Name of the snake");
        add(search);
             	
      	button = new JButton("Find");
        button.setHorizontalAlignment(SwingConstants.CENTER);
        button.addActionListener(new Listener());
      	add(button);
      	 
         
          label2  = new JLabel("Name");
          label2.setLocation(10,50);
          label2.setHorizontalAlignment(SwingConstants.CENTER);
          add(label2);
           
      }    
        
     
    
       public static void main(String[] args){
             		
         JFrame frame = new JFrame("Zoo: Snakes");
         frame.setSize(360,240);
         frame.setLocation(200,100);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setContentPane(new Driver());
         frame.setVisible(true);
      		
      		
      }
           private class Listener implements ActionListener
      {
         public void actionPerformed(ActionEvent e){
                    label2.setText("Name:");
             
             }
         }
    
       }

This code does NOT compile. It needs to compile and execute to be tested.

This code does NOT compile. It needs to compile and execute to be tested.

Really but it is working on my PC, when I click build and run

You must have the classes that are missing when I try to compile: snakes and Driver

You must have the classes that are missing when I try to compile: snakes and Driver

Oh I thought you wanted me to simplify my program but ok Driver2 was modified to be shorter , but here it is.

Driver:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author SOAD
 */
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 import java.util.*;
   import java.io.*;
   import javax.swing.*;


    public class Driver extends JPanel {
    
      static  snakes[] snakesArray = new snakes[4];
      static JLabel label,label2; static JTextField search;	 static JButton button;
      	
       public Driver(){
                    
         label  = new JLabel("Search");
         add(label);
         
        
      	search= new JTextField("Name of the snake");
        add(search);
             	
      	button = new JButton("Find");
        button.setHorizontalAlignment(SwingConstants.CENTER);
        button.addActionListener(new Listener());
      	add(button);
      	 
         
          label2  = new JLabel("Name");
          label2.setLocation(10,50);
          label2.setHorizontalAlignment(SwingConstants.CENTER);
          add(label2);
           
      }    
        
     
    
       public static void main(String[] args){
           
           
           
       // snakes[] snakesArray = new snakes[4];
         try{
            int i = 0;
           /* Jgraphs File file = new File("snake.txt");*/File file = new File("C:\\Users\\SOAD\\Documents\\ComputerScience\\Assignment4\\src\\snake.txt");
            Scanner scanner = new Scanner(file);
            scanner.useDelimiter("%");
            while(scanner.hasNext()){
               snakesArray[i] = new snakes(scanner.next(),scanner.nextBoolean(),scanner.nextInt(),scanner.nextDouble());
               i++;
            }
         }
             catch(Exception e){
               e.printStackTrace();
            }
            // below is JFrame
      	     	
      		
         JFrame frame = new JFrame("Zoo: Snakes");
         frame.setSize(360,240);
         frame.setLocation(200,100);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setContentPane(new Driver());
         frame.setVisible(true);
      		
      		
      }
           private class Listener implements ActionListener
      {
         public void actionPerformed(ActionEvent e){
                    label2.setText("Name:"+snakesArray[0].snake1());
             
             }
         }
    
   }

Snakes:

/**
 *
 * @author SOAD
 */
    public class snakes {
      private String snake; private boolean venomous; private int age; private double weight;
    
       public snakes(){
         snake = "empty";
         venomous = false;
         age = 2;
         weight = 5.23;
      }
    
       public snakes(String n, boolean v, int a, double w){
         snake = n;
         venomous = v;
         age = a;
         weight =w;
      }
       public String snake1(){
         return snake;
      }
      
   }

snaketext:

pokey%true%4%42.34%
sakie%false%2%21.54%
rackie%true%1%5.12%
vairon%true%5%10.32%

I thought you wanted me to simplify my program

Yes I did. I asked for a small simple program that compiles and shows the problem. I don't want all your code.
I've fixed your earlier post so it compiles and executes.

Two problems: many component classes have a default layout manager that you must get rid of if you want to do your own layout. You have not done that.
When you set the layout manager to null, then you are responsible for positioning and sizing your components. You have given the component a location but not a size.
So its size is 0 and you don't see it.

Yes I did. I asked for a small simple program that compiles and shows the problem. I don't want all your code.
I've fixed your earlier post so it compiles and executes.

Two problems: many component classes have a default layout manager that you must get rid of if you want to do your own layout. You have not done that.
When you set the layout manager to null, then you are responsible for positioning and sizing your components. You have given the component a location but not a size.
So its size is 0 and you don't see it.

Well that will become a pain but I can work with it.

To align your components in columns, define a variable with the value for the column
that you can use for each component as you set its location. The same for the rows.
Then when you want to move a row or a column, one change in one place will do it.

For example:

JLabel inLbl = new JLabel("Input:"); 
      inLbl.setBounds(Col1, Row1, 80, Height);
      aPanel.add(inLbl);

      aPanel.add(filepath);
      filepath.setBounds(Col2, Row1, Col3-Col2-Spacer, Height); 
      aPanel.add(chooseBtn);
      chooseBtn.addActionListener(this);
      chooseBtn.setBounds(Col3, Row1, BtnWidth, Height);
      // Second row
      JLabel keyLbl = new JLabel("Key:");
      keyLbl.setBounds(Col1, Row2, BtnWidth, Height);
      aPanel.add(keyLbl);
      aPanel.add(password);
      password.setBounds(Col2, Row2, Col3-Col2-Spacer, Height);

To align your components in columns, define a variable with the value for the column
that you can use for each component as you set its location. The same for the rows.
Then when you want to move a row or a column, one change in one place will do it.

So is the same as doing this.

search= new JTextField("Type the name of a snake, here");
        search.setSize(240,20);search.setLocation(50, 15);
        add(search);

So is the same as doing this.

No, you are not using variables to define the locations. I used int variables to define them. Something like:

final int Row1 = 50;  // y value for items on row1
final int Col1 = 40;  // x value for items in col1

The problem with coding literal numbers such as 240 and 20 is that you can not easily change the rows or columns.

No, you are not using variables to define the locations. I used int variables to define them. Something like:

final int Row1 = 50;  // y value for items on row1
final int Col1 = 40;  // x value for items in col1

The problem with coding literal numbers such as 240 and 20 is that you can not easily change the rows or columns.

Really? but my program is running just fine.

The problems will be when you try to change it.
Say you want to swap the components on the second row with those on the third.
With my scheme, you would change two variables:
final int Row2 = 40;
final int row3 = 60;

And the code would compile and work as you expected.

The problems will be when you try to change it.
Say you want to swap the components on the second row with those on the third.
With my scheme, you would change two variables:
final int Row2 = 40;
final int row3 = 60;

And the code would compile and work as you expected.

Allright got it, thanks for all of the help. Now I'm just trying to do a case insensitive search.

The problems will be when you try to change it.
Say you want to swap the components on the second row with those on the third.
With my scheme, you would change two variables:
final int Row2 = 40;
final int row3 = 60;

And the code would compile and work as you expected.

I'm really sorry to bother you , but I just have question.
If now need to search for a name in an array which I got to work, but only the first name is working. I don't know how to explain it...

Names: pokey, sakie, rackie, vairon

when the user searches for any name other than pokey, I receive an incorrect message I set up for misspelling but I'm searching everything right.

I also tried this without nested loops same result

private class Listener implements ActionListener
      {
         public void actionPerformed(ActionEvent e){
            search.getText();
           if(search.getText().equalsIgnoreCase(snakesArray[0].snake1())==true){
           nameField.setText(snakesArray[0].snake1());  
           
           }
           else if(search.getText().equalsIgnoreCase(snakesArray[0].snake1())==false){
               if(search.getText().equalsIgnoreCase(snakesArray[1].snake1())){
                   nameField.setText(snakesArray[1].snake1());
               }
               else{search.setText("Incorrect");}
           }
          
       }
    
    }

You need a whole test program for this problem. Write a small program that compiles and executes and shows the problem.

This code doesn't show what is being searched for or what is being searched.

You need a whole test program for this problem. Write a small program that compiles and executes and shows the problem.

This code doesn't show what is being searched for or what is being searched.

Driver

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author SOAD
 */
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 import java.util.*;
   import java.io.*;
   import javax.swing.*;


    public class Driver extends JPanel {
      int i=0;
      static  snakes[] snakesArray = new snakes[4];
      static JLabel label,label2; 
      static JTextField search,nameField,nameField2;
      static JButton button;
      	
       public Driver (){
           setLayout(null);
                
      	search= new JTextField("Type the name of a snake, here");
        search.setSize(240,20);search.setLocation(75, 15);
        add(search);
             	
      	button = new JButton("Find");
        
        button.addActionListener(new Listener());
        button.setSize(60,30); button.setLocation(5,10);
        add(button);
      	 
         
          label  = new JLabel("Name");
          label.setLocation(10,50); label.setSize(80,40);
          label.setHorizontalAlignment(SwingConstants.LEFT);
          add(label);
          
          nameField = new JTextField(10);
          nameField.setLocation(50,60); nameField.setSize(100,20);
           add(nameField); 
           
            label2  = new JLabel("Venomous");
          label2.setLocation(10,80); label2.setSize(80,40);
          label2.setHorizontalAlignment(SwingConstants.LEFT);
          add(label2);
          
          nameField2 = new JTextField(10);
          nameField2.setLocation(75,90); nameField2.setSize(100,20);
           add(nameField2); 
          
       }    
        
     
    
       public static void main(String[] args){
           
           
           
       // snakes[] snakesArray = new snakes[4];
         try{
            int i = 0;
           /* Jgraphs File file = new File("snake.txt");*/File file = new File("C:\\Users\\SOAD\\Documents\\ComputerScience\\Assignment4\\src\\snake.txt");
            Scanner scanner = new Scanner(file);
            scanner.useDelimiter("%");
            while(scanner.hasNext()){
               snakesArray[i] = new snakes(scanner.next(),scanner.nextBoolean(),scanner.nextInt(),scanner.nextDouble());
               i++;
            }
         }
             catch(Exception e){
               e.printStackTrace();
            }
            // below is JFrame
      	     	
      		
         JFrame frame = new JFrame("Zoo: Snakes");
         frame.setSize(360,240);
         frame.setLocation(200,100);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setContentPane(new Driver());
         frame.setVisible(true);
      		
      		
      }
           private class Listener implements ActionListener
      {
         public void actionPerformed(ActionEvent e){
            search.getText();
           if(search.getText().equalsIgnoreCase(snakesArray[0].snake1())==true){
           nameField.setText(snakesArray[0].snake1());  
           
           }
           else if(search.getText().equalsIgnoreCase(snakesArray[0].snake1())==false){
               if(search.getText().equalsIgnoreCase(snakesArray[1].snake1())){
                   nameField.setText(snakesArray[1].snake1());
               }
               else{search.setText("Incorrect");}
           }
          
       }
    
    }
   }

snakes:

/**
 *
 * @author SOAD
 */
    public class snakes {
      private String snake; private boolean venomous; private int age; private double weight;
    
       public snakes(){
         snake = "empty";
         venomous = false;
         age = 2;
         weight = 5.23;
      }
    
       public snakes(String n, boolean v, int a, double w){
         snake = n;
         venomous = v;
         age = a;
         weight =w;
      }
       public String snake1(){
         return snake;
      }
      
   }

snakes txt:

pokey%true%4%42.34%
sakie%false%2%21.54%
rackie%true%1%5.12%
vairon%true%5%10.32%

Where is the test program for this one problem of doing a search? Three files is two too many.

Where is the test program for this one problem of doing a search? Three files is two too many.

I would guess you want me to customize it ?

Yes, a small, simple piece of code that only does the search. Define an array, add some Strings and search it. No need of any GUI.

Yes, a small, simple piece of code that only does the search. Define an array, add some Strings and search it. No need of any GUI.

Well this somehow works I don't understand why

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author SOAD
 */
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 import java.util.*;
   import java.io.*;
   import javax.swing.*;


    public class Driver2 extends JPanel {
      int i=0;
      static  snakes[] snakesArray = new snakes[4];
      static JLabel label,label2; 
      static JTextField search,nameField,nameField2;
      static JButton button;
      	
       public Driver2 (){
           setLayout(null);
                
      	search= new JTextField("Type the name of a snake, here");
        search.setSize(240,20);search.setLocation(75, 15);
        add(search);
             	
      	button = new JButton("Find");
        
        button.addActionListener(new Listener());
        button.setSize(60,30); button.setLocation(5,10);
        add(button);
      	 
         
          label  = new JLabel("Name");
          label.setLocation(10,50); label.setSize(80,40);
          label.setHorizontalAlignment(SwingConstants.LEFT);
          add(label);
          
          nameField = new JTextField(10);
          nameField.setLocation(50,60); nameField.setSize(100,20);
           add(nameField); 
           
            label2  = new JLabel("Venomous");
          label2.setLocation(10,80); label2.setSize(80,40);
          label2.setHorizontalAlignment(SwingConstants.LEFT);
          add(label2);
          
          nameField2 = new JTextField(10);
          nameField2.setLocation(75,90); nameField2.setSize(100,20);
           add(nameField2); 
          
       }    
        
     
    
       public static void main(String[] args){
           
           
           
       // snakes[] snakesArray = new snakes[4];
        snakesArray[0] = new snakes("pokey",true,5,5.5);
        snakesArray[1] = new snakes("sakie",false,2,23.2);
      	     	
      		
         JFrame frame = new JFrame("Zoo: Snakes");
         frame.setSize(360,240);
         frame.setLocation(200,100);
         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         frame.setContentPane(new Driver2());
         frame.setVisible(true);
      		
      		
      }
           private class Listener implements ActionListener
      {
         public void actionPerformed(ActionEvent e){
            search.getText();
           if(search.getText().equalsIgnoreCase(snakesArray[0].snake1())==true){
           nameField.setText(snakesArray[0].snake1());  
           
           }
           else if(search.getText().equalsIgnoreCase(snakesArray[0].snake1())==false){
               if(search.getText().equalsIgnoreCase(snakesArray[1].snake1())){
                   nameField.setText(snakesArray[1].snake1());
               }
               else{search.setText("Incorrect");}
           }
          
       }
    
    }
   }

If this works and the other doesn't, what is the difference?

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.