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...
Related
I am trying to create a JPanel to display when a user clicks a button within my main JFrame. In Netbeans I first used the wizard to add a new JPanel to my project, I then used the GUI creator to fill in all the content. I am not trying to display the JPanel with the following code
private void m_jbShowSelAccResultsActionPerformed(java.awt.event.ActionEvent evt)
{
Account selAcc = getSelectedAccount();
if(selAcc != null)
{
AccountView accPanel = new AccountView(Account.getDeepCopy(selAcc));
accPanel.setVisible(true);
}
else
ShowMessage("Please select an account to view");
}
But nothing happens, no error is thrown and the JPanel is not shown. So I then changed the JPanel to a JFrame (Netbeans didn't complain). When I try again with the same code I receive the error GroupLayout can only be used with one Container at a time.
How can I display my JPanel/JFrame?
To change views within a Swing GUI, use a CardLayout as this is a much more robust and reliable way to do this.
Don't try to blindly "change a JPanel to a JFrame". It looks like you're just guessing here.
GroupLayout can't be reused as the error message is telling you. Likely this error comes from the point above. If you avoid trying to make a JFrame out of a JPanel, the error message will likely go away. As an aside, GroupLayout is not easily used manually, especially if you're trying to add components to an already rendered GUI.
So for instance, if your program had a JPanel say called cardHolderPanel, that used a CardLayout, this held by a variable say called cardLayout, and you've already added a "card" JPanel to this holder for accounts, say called accPanel, and if the accPanel had a method to set its currently displayed account, say setAccount(Accoint a), you could easily swap views by calling the CardLayout show(...) method, something like:
private void m_jbShowSelAccResultsActionPerformed(java.awt.event.ActionEvent evt) {
Account selAcc = getSelectedAccount();
if(selAcc != null) {
accPanel.setAccount(Account.getDeepCopy(selAcc));
cardLayout.show(cardHolderPanel, "Account View");
}
else {
showErrorMessage("Please select an account to view");
}
}
I have Jframe with multiple textfields and textareas , I wanted to add copy paste functionalty to Jtextfields and Jtextareas . as you can see in the picture when I right click on Product Name field , it shows copy paste somewhere else on frame.
https://drive.google.com/file/d/0B2tIFybzjEheNTRUSTB1dTNPdEU/edit?usp=sharing
this is the event i have added to textfield
private void jTextField1MouseReleased(java.awt.event.MouseEvent evt) {
if(evt.isPopupTrigger())
{
jPopupMenu1.show(this,evt.getX(),evt.getY());
}
}
MouseEvents are contextual, that is, the location specified by the MouseEvent is local to the component that generated the event...
Try using...
jPopupMenu1.show(evt.getComponent(), evt.getX(),evt.getY());
instead
JComponent.setComponentPopupMenu(meu) also can be used.
I'm making minesweeper for a school project. When the player wins or loses, the mines are revealed. Their buttons are disabled, and icons of flags/mines will appear. The problem is that the icons turn grey when the buttons are disabled. Is there a way around this?
I have also tried setting the text of the JButton to something like "<html><img src=\"res\\mine.png\"/></html>" but it showed some weird image.
Update:
I tried using setDisabledIcon() but nothing's showing up. Here's some pseudo-code
The buttons I use for the minefield is a class called Field, which extends JButton
mouseReleased(mouseEvent e) {
Field fieldClicked = (Field)e.getSource();
if fieldClicked is mine {
fieldClicked.setEnabled(false);
gameTimer.stop();
setLost(true);
loop through 2D array of fields {
if field is a mine {
field.setDisabledIcon(Field.mineIcon);// public static final icon of Field. mineIcon = new ImageIcon("res\\mine.png")
field.setEnabled(false);
}
}
}
}
Figured this out in a test
For some reason
clickedButton.setDisabledIcon(mineIcon)
Alone doesn't do anything.
But:
clickedButton.setIcon(mineIcon)
clickedButton.setDisabledIcon(mineIcon)
Will show whatever icon I wanted
a JButton actually allows seven associated images: the main image (use
setIcon to specify it if not supplied in the constructor), the image
to use when the button is pressed (setPressedIcon), the image to use
when the mouse is over it (setRolloverIcon, but you need to call
setRolloverEnabled(true) first), the image to use when the button is
selected and enabled (setSelectedIcon), the image to use when the
button is disabled (setDisabledIcon), the image to use when it is
selected but disabled (setDisabledSelectedIcon), and the image to use
when the mouse is over it while it is selected
(setRolloverSelectedIcon).
- http://www.apl.jhu.edu/~hall/java/Swing-Tutorial/Swing-Tutorial-JButton.html
so use setDisabledIcon(ImageIcon)
The greyed image is the automatically generated one, in case you want a different icon, use setDisabledIcon()
Icon disabledIcon = new ImageIcon("youricon.gif");
button.setDisabledIcon(disabledIcon);
I am creating a java app.
I have one class q2a2 which is a jpanel whose design is shown as follows: -img-
Suppose if an item is selected from the combo-box and "Create Account" button is clicked. One tab is added to the jTabbedPane. every item has a common tab. so what i did is created one class and adding that everytime on button click. The class name is q2a2_add. This is a panel as well. The image for this is as follows...
After having some three items the app looks like
The code for this is:
public void addclass(int a) {
if(jTabbedPane1.getTabCount()<13) { //variable name of TabbedPane
String s=(String) mainCB.getItemAt(a); //mainCB is the variable name of main combobox
int dont=0;
for(int j=0;j<tabname.length;j++){ //just to ensure two accounts should not be same
if(s.equals(tabname[j])){
dont=1;
break;
}
}
if(dont==0){
for(int j=0;j<12;j++) {
if(index[j]==0){
q2a2_add subpanel2=new q2a2_add(this); //calling the second class
jTabbedPane1.add(s,subpanel2); //here adding panel
subpanel2.heading(s); // heading() method is defined in q2a2_add() which rename the jTextField to be same as argument s;
tabname[j]=s;
index[j]=1;
break;
}
}
}
else {
JOptionPane.showConfirmDialog(null, (String) mainCB.getItemAt(a)+" is already created","Information", JOptionPane.PLAIN_MESSAGE);
}
}
else {
JOptionPane.showConfirmDialog(null, "Account Overload. Delete wrong account and then create","Caution", JOptionPane.PLAIN_MESSAGE);
}
}
Now my question is. As seen in the function. everytime same class has been called and added. How can i access the various comboboxes and textboxes in different tabs. I want to store and opearate values entered by the user. Like for example- how to read inputs from Accounts Receivable, Accounts Payable and Office Supplies differently.
Please reply.
I would expose the functionality that you require within your q2a2_add class. For instance, if you want to change the textbox value, add a function inside the q2a2_add class called setTextBoxValue() that takes a String parameter. Inside that function, you can set the textbox value. The same goes for retrieving information from it. The only remaining problem is how to keep track of the different tabs. What I would recommend (which may simplify what you already have) is to create a HashMap which maps String types to q2a2_add types. Then, when you want to add a new tab panel, you can just check if the String exists in the HashMap instead of searching through to check the titles. If it doesn't exist, you can add it to a HashMap stored inside of your outer JPanel class. Then, when you want to access the tabpanels, you can simply access them by string inside the HashMap and get/set their properties as you please.
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.