How to change the icon of jLabel in code in java? - java

im trying to change the icon of a jLabel named "mainDot". i have another folder in the source packages (named "Images") that contains the icons i want to put on the mainDot when I press it.
private void mainDotMousePressed(java.awt.event.MouseEvent evt) {
mainDot.setIcon(DOT_SMALL.jpg);
}
How would I be able to change the icon of mainDot?

public Test() {
JLabel t=new JLabel();
this.setLayout(new BorderLayout());
t.setIcon(new ImageIcon("Path to image goes here"));
this.add(t);
this.pack();
this.setVisible(true);
}
this one is working. and as suggested use an actionlistener to do it on button press

Related

JFrame will not load new screen

I have a class that extends JFrame and works by adding in 2 panels with BoxLayout buttons, and one JTabbedPane in the center which displays graphs.
I want one of the buttons to remove all current components in the frame and add new ones.
Here are the methods used.
private void createAndShowGraphs() {
ImageIcon createImageIcon(lsuLettersPath); //simple png file to fill one tab
final JTabbedPane jtp = new JTabbedPane();
JLabel iconLabel = new JLabel();
iconLabel.setOpaque(true);
jtp.addTab(null, icon, iconLabel);
//Here is where the errors begin
JPanel menu = new JPanel();
menu.setLayout(new BoxLayout(menu, BoxLayout.Y_AXIS));
//I want this button to remove all components currently in the JFrame and replace them with new components specified in the createAndShowIntro() method
menu.add(new JButton(new AbstractAction("Intro Pane") {
public void actionPerformed(ActionEvent e) {
//I've also tried putting removeAll in the Intro method
removeAll();
createAndShowIntro();
}
}));
add(jtp, BorderLayout.CENTER);
add(menu, BorderLayout.WEST);
pack();
setVisible(true);
}
private void createAndShowIntro() {
System.out.println("Made it to Intro");
//all I want is a blank JLabel with the String "test" to show up
JPanel test = new JPanel();
test.setLayout(new BorderLayout());
JLabel label = new JLabel();
label.setText("test");
label.setHorizontalAlignment(SwingConstants.CENTER);
label.setVerticalAlignment(SwingConstants.CENTER);
test.add(label);
add(test, BorderLayout.CENTER);
test.revalidate();
label.revalidate();
validate();
test.repaint();
label.repaint();
repaint();
pack();
setVisible(true);
}
When I call createAndShowGraphs() in main() and then hit the 'Intro' button, everything freezes and nothing is actually removed. I know it makes it the Intro method because of the "Made it to Intro" string output to the terminal.
I've tried all kinds of combinations of invalidate(), validate(), revalidate(), repaint() on the labels and on the frame itself. Really frustrated because I don't know how else I'm going to be able to display 3 different screens to switch back and forth between while only actually displaying one at a time.
Thanks for your time.

Text not displayed when mouse is rolled over JLabel

With NetBeans (Java), I am having problems in JLabel. I have assigned an image as the icon of that JLabel.
Problem - 1st:
I want to display some text (e.g - logout) below that icon (image). How to do this?
Problem - 2nd:
I want to display some text when mouse is rolled over that JLabel. What should I do?
So , please guys tell me how to these things by writing code.
I recommend reading the basic Oracle tutorials which describe in detail how to accomplish this. You can use a MouseMotionListener to determine when the mouse is rolled over the JLabel, and you can position the JLabel text underneath the Icon of the JLabel by setting its vertical text position as described in the JLabel Tutorial. This should have all been found with a simple internet search of your questions, something that your question suggests was not done (and should have been) before asking
1.
Create a JPanel that contains two JLabels. This way you can control the layout of the internal components.
I used BoxLayout with the parameter BoxLayout.Y_AXIS to get the label below the icon.
2.
Add a MouseListener using the method component.addMouseListener(new MouseAdapter() { ... });, you'll need to create a MouseAdapter and implement any methods you need (click here).
Here is a working example for you buddy... Adapt this however you need to.
Note: You'll need to change the file-path of the ImageIcon()
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel container = new JPanel();
JPanel iconLabelPanel = new JPanel();
String TEXT_FIELD_TEXT = "Hover over the logout label.";
JLabel icon = new JLabel(new ImageIcon("C:\\Users\\Gary\\Google Drive\\Pictures\\puush\\ss (2015-02-19 at 06.00.00).png"));
JLabel label = new JLabel("Logout!");
JTextField textField = new JTextField(TEXT_FIELD_TEXT);
//Add a mouse motion listener for the JLabel
label.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
//Set text of another component
textField.setText("You're over Logout!");
}
#Override
public void mouseExited(MouseEvent e) {
//Set text of another component
textField.setText(TEXT_FIELD_TEXT);
}
});
//Add components and set parameters for iconLabelPanel
iconLabelPanel.setLayout(new BoxLayout(iconLabelPanel, BoxLayout.PAGE_AXIS));
iconLabelPanel.add(icon);
iconLabelPanel.add(label);
//Add components and set parameters for container
container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
container.add(iconLabelPanel);
container.add(textField);
//Set parameters for frame
frame.add(container);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setSize(400, 400);
frame.setVisible(true);
}

Trying to build a GUI that would enable users to add more TextFields to it

I need to let users add more text fields to my JFrame so once the size of the containing frame being JPanel has exceeded its original value a scroll pane would step in.
In order to be able to do this, I came up with an idea to put one JButton and upon hitting it a new TextField would show up (this was my original idea which doesn't necessarily mean I am right). The problem is, once I call the ActionListener class to add more TextFields and eventually stretch its containing panel, the program asks me to make the JPanel final which in turns doesn't allow for stretching of the panel. In other words, it appears to me that I'm just beating around the bush, please help me out put this together, below is a piece of my code:
public class Button {
public static void main(String[] args) {
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JPanel p = new JPanel(new GridLayout(0, 5));
JScrollPane jsp = new JScrollPane(p);
jsp.setPreferredSize(new Dimension(300,300));
jsp.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
jsp.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
JButton but = new JButton("Add");
f.add(but);
but.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int height=0;
JTextField jtx = new JTextField();
jtx.setSize(new Dimension(70,20));
jtx.setPreferredSize(new Dimension(70,20));
p.add(jtf);
height+=20;
p.setSize(new Dimension(300, height));
p.setPreferredSize(new Dimension(300, height));
}
});
f.add(jsp, BorderLayout.CENTER);
f.setLocation(300, 300);
f.setVisible(true);
f.pack();
}
}
I will give you an idea : you can add button and inside the button instruction you would add an instruction set that create text field . if you want more than once then you must handle position pointer that tell you the last place the user add text field then by updating the pointer the user can see the text in different place , if you want the user to control the position of the text then he must enter the location .

Image button created from JLabel is not working

I'm trying to make a buttom from an image on the JFrame using the ImageIcon and the addMouseListener that will replace the current image with another image by clicking it.
static JPanel jp = new JPanel();
final JLabel jl = new JLabel();
final JFrame jf = new JFrame();
ImageIcon image = new ImageIcon("image1.jpg");
jl.setIcon(image);
jp.add(jl);
jf.add(jp);
jf.validate();
JLabel button = new JLabel(image);
button.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jl.setIcon( null );
ImageIcon image = new ImageIcon("image2.jpg");
jl.setIcon(image);
}
});
The GUI is displayed with image1.jpg, but the button does not work at all and I can't even test whether the replacement from image1 to image2 works. GUI will not do anything even if I attempt to click the image1.jpg displayed on the window.
Edit: Adjusted JLabel varaible to be final for now. Other similar questions intimate that this method should be working but I can't figure out what is wrong with the code.
Not really sure ActionListener works with JLabel either.
No you can't add an ActionListener to a JLabel. An easier approach is to make a JButton look like a JLabel, then you can add the ActionListener to the button:
JButton button = new JButton(...);
button.setBorderPainted(false);
button.setContentAreaFilled(false);
button.addActionListener(...);
but the button does not work at all
A mouse click is generated when a mousePressed and mouseReleased is received for the same mouse point. So if you move the mouse slightly the event will not get generated. Instead listen for the mousePressed() event.

adding components dynamically in a JPanel

how can i add components dynamically in a jpanel?
I am having add button when i click the button the components should be added to the JPanel.
my question is that adding a textfield and button to jpanel when i click on the add button the user can click on the add button any number of times according to that i have to add them to the jpanel. i have added to scrollerpane to my jpanel,and jpanel layout manager is set to null.
Just as you always do, except that you have to call:
panel.revalidate();
when you are done, since the container is already realized.
Use an ActionListener, you can use an anonymous class like this:
JPanel myJPanel = new JPanel();
...
b = new Button("Add Component");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JLabel someLabel = new JLabel("Some new Label");
myJPanel.add(someLabel);
myJPanel.revalidate();
}
});

Categories