Java Swing create a Text Area for the Output - java

Hello I am really new to Java programming and I have created a Java menu with some options as well as file chooser. Therefore in my IDE I print out the file name and the path of the file that the user chooses. Is there any way that I can create a text area on my frame so the user can see the actual output ?
This is how my file chooser looks like and how I output the results on my console.
JFileChooser chooser = new JFileChooser();
File F = new File("C:/");
File namedir;
File namepath;
chooser.setCurrentDirectory(F);
chooser.showOpenDialog(null);
chooser.setDialogTitle("Choose file to play");
chooser.setApproveButtonText("Play");
namedir = chooser.getCurrentDirectory();
namepath = chooser.getSelectedFile();
System.out.print("the name of the the directory is "+namedir.getName());
System.out.print("the name of the the path is "+namepath.getAbsolutePath());
And here is the code for my menu
JFrame frame = new JFrame("Menu");
frame.setVisible(true);
frame.setSize(600,400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//add the menu bar with the item browse
JMenuBar bar = new JMenuBar();
frame.setJMenuBar(bar);
JMenu search = new JMenu("Browse");
bar.add(search);
What I need is a text area so I can output the file name and the path on my frame.

See docs, this explains the use of JTextArea. It is very simple and you can do alot of things using it. It will surely work for you.

Related

Set program's directory as the initial directory of JavaFX FileChooser

I am using JavaFX. I would like to start a FileChooser from the directory of the program, the initial repository should therefore be that of the program.
Here is my FileChooser declaration:
FileChooser chooser = new FileChooser();
chooser.getExtensionFilters().addAll(
new ExtensionFilter("Text Files", "*.txt"),
new ExtensionFilter("All Files", "*.*"));
chooser.setTitle("Choisir un fichier");
file = chooser.showOpenDialog(new Stage());
How can I do that?
The current dir is ".". Here is how you can do that:
FileChooser chooser = new FileChooser();
String currentPath = Paths.get(".").toAbsolutePath().normalize().toString();
chooser.setInitialDirectory(new File(currentPath));
chooser.showOpenDialog(new Stage());
Edit: the Stage or javafx Node you should be passing to the FileChooser is the one you want to be its parent.

Java: open image on a jlabel

I have seen tons of questions about this thing but i just cant completely understand why it doesnt work.
I want to open an image using JFileChooser and then show it on the jLabel on the other jFrame. So why it doesnt work? What is so wrong about it?
JFileChooser fileopen = new JFileChooser();
int ret = fileopen.showDialog(null, "Open file");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fileopen.getSelectedFile();
Icon icon = fileopen.getIcon(file);
origin.jLabel1.setIcon(icon);}
By the way will it work for .bmp files, not only .jpg, .png and .gif?
You need to use ImageIcon. References can be found here: ImageIcon java Docs and Swing tutorial.
Here is the updated source:
JFileChooser fileopen = new JFileChooser();
int ret = fileopen.showDialog(null, "Open file");
if (ret == JFileChooser.APPROVE_OPTION) {
File file = fileopen.getSelectedFile();
ImageIcon icon = new ImageIcon(file.getPath());
jLabel1.setIcon(icon);
}

How to add an item in a list using the java file chooser

I am using the java file chooser in order to select a file from my local disk and output its name and path. Is there any way that I could select a file and add it to a list? Here is my code for the file chooser.
JFileChooser chooser = new JFileChooser();
File F = new File("C:/");
File namedir;
File namepath;
chooser.setCurrentDirectory(F);
chooser.showOpenDialog(null);
chooser.setDialogTitle("Choose file ");
chooser.setApproveButtonText("View details");
namedir = chooser.getCurrentDirectory();
namepath = chooser.getSelectedFile();
JTextArea textarea = new JTextArea();
textarea.setEditable(false);
frame.add(textarea);
textarea.append("FILE LOCATION AND NAME:"+namepath);
System.out.print("the name of the the directory is "+namedir.getName());
System.out.print("the name of the the path is "+namepath.getAbsolutePath());
I would use a JList. Read the section from the Swing tutorial on How to Use Lists for more information and examples.
The example is almost exactly what you want. It shows you how to dynamically add a String to the ListModel of the JList.

Java getClass().getResource on a png returning Null Pointer

I am not sure if I am referring to the right location with this code, the images I am trying to access are titled Flower0.png etc.
They are located in the same directory as the rest of my code for this project.
This class is in a src folder called hangman.ui and the .png files are located in a directory folder called Resources.
Perhaps getClass().getResource is not right?
This is my first time trying to put images into a GUI.
Help is much appreciated!
public WiltingFlowerRendererRemix(HangmanLogic logic)
{
panel = new JPanel();
panel.setLayout(new BorderLayout());
imageLabel = new JLabel();
panel.add(imageLabel, BorderLayout.CENTER);
int numberOfImages = 10;
images = new ImageIcon[numberOfImages];
for (int i = 0; i < numberOfImages; i++)
{
images[i] = new ImageIcon(getClass().getResource("Flower"+Integer.toString(i) + ".png"));
}
}
You say the images are in a folder called "Resources"? You can load images like this then:
BufferedImage image = ImageIO.read(getClass().getResource("/Resources/Flower0.png"));
ImageIcon icon = new ImageIcon(image);
To use it on the GUI you can use a JLabel.
JLabel label = new JLabel();
label.setIcon(icon);
And then add the label to a panel for example.
For me Works...
According to Maven:
https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html
src/main/resources: Application/Library resources
Then I put the ICON in this Location:
PROJECTNAME\src\main\resources\usedPictures\
--Open.png
Clean and Build. And you can check here the location where the file will be get....
PROJECTNAME\target\classes\usedPictures\
--Open.png
Now the Example using the ICON:
button.setIcon(
new javax.swing.ImageIcon(
getClass().getClassLoader().getResource("usedPictures/Open.png")
)
);

image upload-showing image on a form

I am using netbeans6.7.1 and phpmyadmin for my db to develop a java application
to manage students records
i want to upload students photos through browsing by clicking a browse buton which i have
included in my interface
I mean when i click on that button a JFilechooser pops up which filter only images(i have acomplished this)
what i need is when i click on the "Attach button" of the JFilechooser, i want the image i chose to be attached to a jtextArea on the form i'm working with and the JFilechooser be diposed off.
Also how i can save this form together with the image to a database table
Is there a place where i can find a good guide/tutorial about that
JFileChooser chooser;
FileNameExtensionFilter filter;
chooser = new JFileChooser();
filter = new FileNameExtensionFilter("jpeg, gif and png files", "jpg", "gif", "png");
chooser.addChoosableFileFilter(filter);
jButton1.addActionListener(this);
if(e.getSource()==jButton1)
{
int i = chooser.showOpenDialog(jPanel1);
if(i==JFileChooser.APPROVE_OPTION)
{
jPanel2.removeAll();
jPanel2.repaint();
File image = chooser.getSelectedFile();
ImageIcon photo = new ImageIcon(image.getAbsolutePath());
//jPanel2.add(new JLabel(photo));
JLabel label=new JLabel("",photo,JLabel.CENTER);
jPanel2.add(label,BorderLayout.CENTER);
jPanel2.repaint();// sets a default image in image field.
jPanel2.revalidate();
}
}
Note:You should set borderlayout for jpanel2
and the selected image size must be the size of jpanel2

Categories

Resources