Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 235
  • Last Modified:

Picture as button

Hey can anyone help me with adding a picture as a button to a java program, the picture is "pic.jpg".  When the button is pressed a value is saved in a string variable called "picValue".
Any help on how to do this would be great,
cheers.
Raftor
0
Raftor
Asked:
Raftor
  • 7
  • 6
  • 6
2 Solutions
 
CEHJCommented:
JButton b = JButton(new ImageIcon(
"pic.jpg"));

What is the 'value' you refer to about?
0
 
objectsCommented:
JButton b = JButton(new ImageIcon("pic.jpg"));
b.addActionListener(new ActionListener()
   { public void actionPerformed(ActionEvent e) { picValue = getValueFromSomewhere(); }});

0
 
RaftorAuthor Commented:
There are three buttons, the user has to choose one and when he does a string  "pic1", "pic2", or "pic3"  is stored in "value" depending on which was pressed.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
Make you main class implement ActionListener and then

public void actionPerformed(ActionEvent e) {
  Object button = e.getSource();
  if (button == b1) {
     value = "pic1";
   }
  else if (button == b2
  ...
}

if you get the idea
0
 
objectsCommented:
JButton b1 = JButton(new ImageIcon("pic.jpg"));
b1.addActionListener(new ActionListener()
   { public void actionPerformed(ActionEvent e) { picValue = "pic1"; }});
JButton b2 = JButton(new ImageIcon("pic.jpg"));
b1.addActionListener(new ActionListener()
   { public void actionPerformed(ActionEvent e) { picValue = "pic2"; }});
JButton b3 = JButton(new ImageIcon("pic.jpg"));
b1.addActionListener(new ActionListener()
   { public void actionPerformed(ActionEvent e) { picValue = "pic3"; }});
0
 
CEHJCommented:
You can use the JButton's name property to do this:

 JButton b1 = JButton(new ImageIcon("pic.jpg"));
  b1.setName("pic1");
  b1.addActionListener(this);
 
  ...
 
 
  public void actionPerformed(ActionEvent e) {
    Object o = null;
    if ((o = e.getSource()) instanceof JButton) {
      value = ((Component)o).getName();
    }
  }
0
 
RaftorAuthor Commented:
Also is it possible to return strings from threads in java?
This is my header, how do i define it to return a string?

public class control extends Thread{
    ....
    ...
    return(string);
}
0
 
CEHJCommented:
You need an instance variable and an accessor

String x;

public String getX() {
   return x;
}
0
 
RaftorAuthor Commented:
Is this kind of thing possible? If not how would you go about it?

String playerOneResponse = control.start();
0
 
RaftorAuthor Commented:
CEHJ do i do that in a simple function inside the tread?  I though i would have to have a string return type on the class header!
0
 
CEHJCommented:
Yes, instance function:

control.start();
String x = control.getX();
0
 
objectsCommented:
public class control extends Thread{
   private String S;
    ....
    ...
    public String getS() {
       return S;
    }
}

How exactly is the string being set and used?
0
 
RaftorAuthor Commented:
Whats wrong with this code?


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class evaluateChoices()
{
      public PrintWriter playerOneWrite,playerTwoWrite;
      public int returnWinner(String P1Choice, String P2Choice,Socket P1Socket, Socket P2Socket)
      {
            playerOneWrite = new PrintWriter(player1.getOutputStream(), true);
            int returnValue = 0;//1 means player1 wins 2 means player 2 wins 0 means draw
            if(P1Choice.equalsIgnoreCase(P2Choice))
            {
                  returnValue = 0;
            }
            else if((P1Choice.equalsIgnoreCase("rock")) && (P2Choice.equalsIgnoreCase("scissors")))
            {
                  returnValue = 1;
            }
                  
            return(returnValue);
      }
}
0
 
CEHJCommented:
Can we have one question at a time please? ;-)
0
 
objectsCommented:
>          playerOneWrite = new PrintWriter(player1.getOutputStream(), true);

variable player1 is never defined.
0
 
objectsCommented:
Should probably be P1Socket
0
 
CEHJCommented:
:-)
0
 
RaftorAuthor Commented:
cheers lads :-)
0
 
objectsCommented:
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now