im designing a program for my school that displays the teachers schedules at the click of a button. I have everything figured out but how to open an image by clicking on the specific teachers name. here is some of my code any help is appreciated. Thanks
`JButton gabster = new JButton("Mrs. Gabster");
gabster.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPanel gabster = new JPanel();
}
});`
i need to have the button open a new image from my workspace
public void actionPerformed(ActionEvent ae)
{
ImageIcon pic = new ImageIcon("picture.jpg");
JLabel picture = new JLabel(pic);
panel.add(picture);
}
Create a method for when the button is clicked.
Related
I am making an image gallery. I am using javafx 8 along with scene builder for GUI. I want the user to have choice of selecting images from a gallery: I want to have clickable and selectable images in the gallery. I also want to note the sequence of those selected images, i.e. which one was selected first and which one was selected last. I have made the gallery but I was wondering what I would do to make the images clickable and get the images selected, noting their sequence? I don't want to use checkboxes with the images.
You can add a setOnMouseClicked handler on each ImageView.
Here is a code example of what I mean:
public class ClickableImage extends Application {
private ArrayList<String> clickedImages = new ArrayList<>();
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
String imgUrl = getClass().getResource("image.png").toExternalForm();
assert imgUrl != null;
Image img = new Image(imgUrl);
assert img != null;
BorderPane root = new BorderPane();
ImageView imgView = new ImageView(img);
imgView.setUserData(imgUrl);
root.setCenter(imgView);
Scene scene = new Scene(root, 600, 400);
primaryStage.setScene(scene);
primaryStage.setTitle("ClickableImage");
primaryStage.show();
//-------------
imgView.setOnMouseClicked(e -> {
String clickedImgUrl = (String)((ImageView)e.getSource()).getUserData();
System.out.println("Image was clicked: " + clickedImgUrl);
clickedImages.add(clickedImgUrl);
});
}
}
A piece of code I created to open an image requires that I click twice on the words to open the image. I don't understand why it doesn't respond to just one click.
In the layout here is the TextView
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="The New Yorker"
android:id="#+id/the_new_yorker"
android:textStyle="italic"
android:textColor="#color/blue"
android:onClick="new_yorker_cartoon"/>
and here is the corresponding function in the java file
public void new_yorker_cartoon(View view) {
final TextView TextGenerique = (TextView) findViewById(R.id.the_new_yorker);
View.OnClickListener monEcouteur = new View.OnClickListener() {
#Override
public void onClick(View v) {
String imageUrl = "http://www.lapasserelle.com/english/l09/imgs/new_yorker.gif";
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(imageUrl), "image/gif");
startActivity(intent);
}
};
TextGenerique.setOnClickListener(monEcouteur);
}
What I don't understand is why I need to click twice on the words New Yorker to display the image new_yorker.gif on my phone.
It might be that the first click is to activate the screen and the second to get the response.
My app has a camera button. Upon click this button launches the native camera app and the user can take a picture. If the user accepts the picture (taps check mark) I want to start a new activity which will display that image on a new screen. I am trying to save the image like so: File file = new File(this.getFilesDir(), filename); and then get the file path and pass the path on as a string to my new activity. In my new activity I am attempting to retrieve the filepath from the previous intent, and use that to load the image into an ImageView. Before I write the bit to load the image into the ImageView I wanted to check that the filepath string is being passed by printing it in a text area on the new screen. Unfortunately, it does not appear to be passing properly. Any help would be greatly appreciated. Below is a snippet of my code. Many thanks in advance for the assistance!
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_use_camera);
// create Intent to take a picture and return control to the calling application
Intent cameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//start the image capture Intent
startActivityForResult(cameraintent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
String filename = "myfile";
//make sure the user clicked to accept the picture in the native camera app
if (resultCode == RESULT_OK) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
{
//save image internally
File file = new File(this.getFilesDir(), filename);
//get the filepath
String filepath = file.getAbsolutePath();
//create the swatchintent
Intent Swatchintent = new Intent(this, DisplaySwatchActivity.class);
//pass filepath to the intent
Swatchintent.putExtra(EXTRA_MESSAGE, filepath);
//start the intent
startActivity(Swatchintent);
}
}
}
then, my next activity:
public class DisplaySwatchActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_swatch);
String filepath = this.getIntent().getStringExtra(UseCameraActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(20);
textView.setText(filepath);
}
I would like to start editing after adding new row into TableView.
I copied example from Oracle website: Using JavaFX UI Controls - 13 Table View. Then I put additional button for adding new row and define action for the button.
final Button addButton = new Button("Add");
addButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
Person p = new Person("", "", "");
table.getItems().add(p);
table.getSelectionModel().select(p);
table.edit(table.getSelectionModel().getSelectedIndex(), table.getColumns().get(2));
}
});
In result I can see selected new row but the table doesn't start edditing in the third column.
I have similar method for editing existing rows and it works properly.
final Button editButton = new Button("Edit");
editButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
table.edit(table.getSelectionModel().getSelectedIndex(), table.getColumns().get(2));
}
});
Could you help me what I do wrong?
I have a wp7 with some buttons in the application bar.
When each button is pressed I change the menuItems of the application bar's menu.
After this, I want to automatically open the menu when an application bar button is pressed.
But it seems that the SDK doesn't allow me to do that.
Do you know any work around?
I was thinking, if the above is not possible, to simulate a user finger click at the bottom right corner of the screen to open the menu. Any ideas on that?
Thanx in advance
It's possible to change the Application Bar Menu Items in response to an Icon Button click as demonstrated in the code below.
There isn't a way to forcibly open (or close) the application bar through code through.
It also isn't possible to simulate a finger click on the application bar as this isn't part of the actual page. Note that even if possible any click would need to be in the top right or bottom left if the device was in a landscape orientation.
Here's some code which demonstrates changing the menu items:
public partial class MainPage : PhoneApplicationPage
{
private ApplicationBar appbar;
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
appbar = new ApplicationBar();
var ib1 = new ApplicationBarIconButton(new Uri("/images/one.png", UriKind.Relative)) { Text = "Option one" };
ib1.Click += new EventHandler(ShowMenuOption1);
var ib2 = new ApplicationBarIconButton(new Uri("/images/two.png", UriKind.Relative)) { Text = "Option two" };
ib2.Click += new EventHandler(ShowMenuOption2);
appbar.Buttons.Add(ib1);
appbar.Buttons.Add(ib2);
// Show menu option 1 as default
DisplayMenuOption1();
this.ApplicationBar = appbar;
}
private void DisplayMenuOption1()
{
appbar.MenuItems.Clear();
var itemA = new ApplicationBarMenuItem("AAAA");
var itemB = new ApplicationBarMenuItem("BBB");
appbar.MenuItems.Add(itemA);
appbar.MenuItems.Add(itemB);
}
private void DisplayMenuOption2()
{
appbar.MenuItems.Clear();
var itemC = new ApplicationBarMenuItem("CCCC");
var itemD = new ApplicationBarMenuItem("DDDD");
appbar.MenuItems.Add(itemC);
appbar.MenuItems.Add(itemD);
}
private void ShowMenuOption2(object sender, EventArgs e)
{
DisplayMenuOption2();
}
private void ShowMenuOption1(object sender, EventArgs e)
{
DisplayMenuOption1();
}
}
As far as I'm aware this capability has not been exposed as yet. It wasn't possible during beta and I've not noticed anything that's changed since that would allow it. You could always comment on the their suggestions forum or raise it on connect (vs/wpdt).