Jump to content

Java setting location

- - - - -

  • Please log in to reply
6 replies to this topic

#1
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts
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);

           

      }


#2
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
Here's a short tutorial that might help you:
Doing Without a Layout Manager (Absolute Positioning) (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

They use absolute position to place components onto a panel.

#3
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

lethalwire said:

Here's a short tutorial that might help you:
Doing Without a Layout Manager (Absolute Positioning) (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

They use absolute position to place components onto a panel.

Thanks lethalwire, I figure it out yesterday but the post helped me to understand how it actually works.
By the way do you know how can I make a case insensitive search?

#4
Sinipull

Sinipull

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 386 posts
"case insensitive search?"

String has equalsIgnoreCase method.
.

#5
rakz

rakz

    Newbie

  • Members
  • PipPip
  • 17 posts
Try setBounds()

#6
vaironl

vaironl

    Programmer

  • Members
  • PipPipPipPip
  • 117 posts

rakz said:

Try setBounds()

Sinipull said:

"case insensitive search?"

String has equalsIgnoreCase method.

I used the equalsIgnoreCase.
Though I been trying to solve this problem for the past 2 days which has taken all of my homework time.
I an if and if else statement which compares the string to know what values to place in the boxes. Point is only the first if statement works is not executing the else if any ideas? i really can't explain it too well I'm frustrated

Code:
  private class Listener implements ActionListener

      {

         public void actionPerformed(ActionEvent e){

            search.getText(); System.out.println(search.getText());

            if(search.getText().equalsIgnoreCase(snakesArray[0].snake1())==true){

                    nameField.setText(snakesArray[0].snake1()); nameField2.setText(Boolean.toString(snakesArray[0].snake2()));

                    System.out.println(nameField.getText()); search.setText("Success");

            }            

            else if(search.getText().equalsIgnoreCase(snakesArray[1].snake1())==true ){

                    nameField.setText(snakesArray[1].snake1());

                        }

            

            

        } 

  }


#7
lethalwire

lethalwire

    while(false){ ... }

  • Members
  • PipPipPipPipPipPipPip
  • 748 posts
  • Programming Language:Java, PHP
  • Learning:Java, PHP
I can't quite understand your question but if the first if statement is true, the else if is ignored.

Also when you have functions that return boolean values you don't have to include == true or == false.
For instance

if( "abc".equals("abc") == true )

//is the same as

if( "abc".equals("abc") )


// AND


if( "abc".equals("abc") == false )

//is the same as

if( !("abc".equals("abc")) )




rakz said:

Try setBounds()
This won't work unless the layout is set to null.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users