i am trying to update the title of a JInternalFrame component in my Java Project.
The component is an instance of my ImageFrame class which extends JInternalFrame, and in my code I call a setter method in my ImageFrame class which updates the title attribute. I ran a Unit test and know that the attribute is updating properly, but I can't figure out how to refresh the component to show the new title.
Any Ideas?
FYI: I was unable to get .repaint() to do the trick.
Here's the Code:
File selectedFile = fileChooser.getSelectedFile(); // Gets File selected in JFileChooser
try {
ImageReadWrite.write(img, selectedFile); // Writes Image Data to a File
frame.setFilePath(selectedFile.getAbsolutePath()); // Changes File Location Attribute in Instance Of ImageFrame
frame.setFileName(selectedFile.getName()); // Changes Window Title Attribute
//frame.??
}
catch (Exception event) {
event.printStackTrace();
}
so what I need here is to know what I should add to make the component update with the new title
You could try by replacing:
frame.setFileName(selectedFile.getName());
with
frame.setTitle(selectedFile.getName());
I don't know your code, but setFileName is not part of JInternalFrame public interface.
Probably you added that method, probably not. Try my suggestion and see if that helps.
Related
I have been trying to follow these set of Java tutorials from this website
http://www.homeandlearn.co.uk/java/java.html
However the tutorials are in Netbeans and I am using Eclipse.
Till now there has been no difficulties until now.
http://www.homeandlearn.co.uk/java/opening_files.html
In the given tutorial using JFileChooser to open files through a JMenuItem called 'Open' is shown. However when i use the code given in the website the following error occurs
The method showOpenDialog(Component) in the type JFileChooser is not applicable for the arguments (new ActionListener(){})
This is the code for which the error is occuring.
JMenuItem mntmNewMenuItem = new JMenuItem("Open");
mntmNewMenuItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
int returnVal = db.showOpenDialog(this);
}
});
So, my question is, what should i change in the above code stub to be able to use the file chooser?
If you want to view the entire code, i will put it in on your request.
Meaning of the error: the method showOpenDialog requires a parameter of type Component but is being called with an ActionListener. More exactly, the given parameter is of an anonymous class implementing ActionListener and not a Component:
new ActionListener() { ... }
inside the methods declared where I used . . . the keyword this points to the instance of that anonymous class.
See the documentation of showOpenDialog(), it requires a parent or null:
Pops up an "Open File" file chooser dialog. Note that the text that
appears in the approve button is determined by the L&F.
Parameters:
parent - the parent component of the dialog, can be null; see showDialog for details
And the relevant documentation of showDialog():
The parent argument determines two things: the frame on which the open
dialog depends and the component whose position the look and feel should
consider when placing the dialog.
...
If the parent is null, then the dialog depends on no visible window, and
it's placed in a look-and-feel-dependent position such as the center of
the screen.
Usually the passed parameter is the JFrame or JPanel that should visually contain the dialog, but it can be null:
int returnVal = db.showOpenDialog(null);
I am making a library where an application can use it to capture a selection of a screen and convert it to an image, like Gyazo.
This library is not the application itself, but only the tool that returns the File or BufferedImage object to the application.
I want the application to be simple as this:
Bootstrap b = new Boostrap(new GifCapturer());
b.beginCapture(); // user selects an area
File file = b.getFile();
But how can I make the application wait till the library returns the object? as you see the beginCapture method should activate the JFrame where the user will select an area to capture.
Do I need to sleep the thread? or use listeners design?
The beginCapture method starts a jframe window, where the user is able to select an area of the screen. Once selected, the library will convert the selected area to an object and set it as a local variable. So when you will use getFile it ill return the captured image. But the thing is, i need to make sure that the image was selected before getFile call gets executed, and wait instead but im not sure how.
Sorry if the question is not detailed, im on phone.
Please let me know if you need more information.
Implement a listener, that is invoked as soon the selection is ready. Put your File file = b.getFile(); code into the listener.
The code of your JFrame would be necessary to give a more detailed answer.
I have decided to use a Listener with a own built listener class, and interface.
Create an interface which you will use to get the data, or that will get know when the listener gets called, like this in my case:
public static void main(String[] args) throws AWTException {
Bootstrap b = new Bootstrap(new GifCapturer());
b.beginCapture(new ScreenCaptureCallback() {
#Override
public void captureEnded(File file) {
System.out.println("done!");
}
});
}
I am using the NetBeans GUIBuilder to make a JPanel Form. I added a JLabel and used NetBeans' interface to give it an icon from an external image (.png). The path is verified and the image shows up on the GUIBuilder screen. It even shows up when I click the "Preview Design" button. It DOES NOT show up when I RUN the project. The rest of the GUI appears as it should. Do any of you know why this happening and/or how to fix it?
A lot of you have been asking for an SSCCE. Since the code is generated by the NetBeans Form Builder, I have instead included the steps I took to make the JLabel. The areas of focus are circled in red.
Drag and drop a JLabel into the Form Builder.
Open up the JLabel's properties menu. Enter the empty string ("") for the text field. Click the ellipsis next to icon.
Select External Image and click the ellipsis.
Select the image of choice. In my case it's a .png.
Notice that the image appears in the icon preview.
Close the icon menu and the properties menu, and notice that the image appears as the JLabel's icon on the Form Builder.
Thank you for accepting an unorthodox SSCCE and thank you in advance for your help.
I found out the hard way that relying on Netbeans GUI builder to do everything for you is a mistake.
Just create an icon fetching class like the one below, put the icons in it's package, and use "Custom code" instead of "Image chooser". Sure the icons will not be visible inside NB. But if they show up when the app is running, who cares about that.
package com.example.resource.icons;
import javax.swing.ImageIcon;
public class IconFetch {
private static IconFetch instance;
private IconFetch(){
}
public static IconFetch getInstance() {
if (instance == null)
instance = new IconFetch();
return instance;
}
public ImageIcon getIcon(String iconName) {
java.net.URL imgUrl = getClass().getResource(iconName);
if (imgUrl != null) {
return new ImageIcon(imgUrl);
} else {
throw new IllegalArgumentException("This icon file does not exist");
}
}
public static final String MINESWEEPER_ONE = "one.png";
}
Usage:
IconFetch.getInstance().getIcon(IconFetch.MINESWEEPER_ONE);
If the icon still doesn't show up after trying this, then something might be wrong with the way you layed out components in your form (the label is there but you can't see it).
Hope this helps even though it's a long shot.
I had the same problem, and predi's solution wasn't working either. Then I created a package instead of a folder, and added the images there, and it works now.
I do have a same problem also. But I found the solution.
I create the package in project and put the images inside there.
When I build the project, Netbeans will create 'target' folder and build .class files.
I found that the images that I copied to the package, did not transfer to the 'target' folder.
Interim solution.
4. I copy all image to target folder with the same structure. Then I can run the project directly from Netbeans.
5. Incase you clean the project. Do no.4 again.
I create a JTree and model for it out in a class separate to the GUI class. The data for the JTree is extracted from a file.
Now in the GUI class the user can add files from the file system to an AWT list. After the user clicks on a file in the list I want the JTree to update. The variable name for the JTree is schemaTree.
I have the following code for the when an item in the list is selected:
private void schemaListItemStateChanged(java.awt.event.ItemEvent evt) {
int selection = schemaList.getSelectedIndex();
File selectedFile = schemas.get(selection);
long fileSize = selectedFile.length();
fileInfoLabel.setText("Size: " + fileSize + " bytes");
schemaParser = new XSDParser(selectedFile.getAbsolutePath());
TreeModel model = schemaParser.generateTreeModel();
schemaTree.setModel(model);
}
I've updated the code to correspond to the accepted answer. The JTree now updates correctly based on which file I select in the list.
From the Component.add API docs.
Note: If a component has been added to
a container that has been displayed,
validate must be called on that
container to display the new
component. If multiple components are
being added, you can improve
efficiency by calling validate only
once, after all the components have
been added.
You have called repaint and validate on a component that is not displayed, which will not be effective. You need to call those methods on the mainPanel after the add. Also revalidate tends to be better than validate as it effectively coalesces.
I not sure that I'm understanding your question, but I'll try...
The right thing to do should be, IMHO:
get the file
create a new TreeModel from your file
give the model to the JTree
In pseudocode, it would look like that:
File newContent = getSelectedByUser(...);
TreeModel newModel = new MyFileBasedTreeModel(newContent);
//this next part must be done in the EventDispatcherThread
myTree.setModel(newModel);
then the JTree would be updated, without any call to repaint, etc.
Hope it helps
I want to display a set of images (with associated text) on my window. I want to iterate through them using a previous and a next button. So far, I have only been able to associate the image with a JLabel. =/
How do I go about doing the rest? Should I use a different container for the complete set? Should I load the images on a data structure like an ArrayList, or is it enough to keep them on a folder? How can I add the event handling so that pushing the button displays the next or previous image?
Here is a screenshot of what I have so far.
Are you still here ?
I assume that you have found how to load the path of each of your images (if they are inside the same folder). You should store the path of the directory in a global variable, and then the name of each image into a Vector if you want to iterate through them. Just store the name of the files, not the entire images.
You also have to store the index of the current image as a global variable.
If you use a JFrame as your main window, you have to specify that it implements the class ActionListener this way:
public class MyClass extends JFrame implements ActionListener
Then you have to attach the event handler to your buttons (JButton). This must be placed inside the constructor of your window (MyClass):
nextButton.addActionListener(this);
previousButton.addActionListener(this);
Having implemented ActionListener, your class has to define the method actionPerformed. Inside it, you must change the content of the image according to the button that has been pressed.
public void actionPerformed(ActionEvent e)
{
Object o = e.getSource();
if(o == nextButton)
{
currentIndex++;
if(currentIndex == vectorImages.size())
{
currentIndex = 0;
}
//Change the image in the JLabel
label.setIcon(new ImageIcon(vectorImages.get(currentIndex)));
}
else
{
//Iterate backwards
}
}
Hope this helps...