i have problem to change JLabel color. i am using three JLabel variables . i am putting mouse event on this JLabel variables. i run the and both are changes color when i am entring mouse on JLabels. i whis is that, at the time one JLabel change the color when I am entering the mouse on JLabel variable.
please solve this problem.
entry.addMouseListener(this);
entry.setOpaque(true);
profile.addMouseListener(this);
profile.setOpaque(true);
public void mouseClicked(MouseEvent mc)
{
}
public void mouseEntered(MouseEvent me)
{
entry.setBackground(color);
profile.setBackground(color);
}
public void mouseExited(MouseEvent me)
{
entry.setBackground(Color.white);
profile.setBackground(Color.white);
}
public void mousePressed(MouseEvent mp)
{
}
public void mouseReleased(MouseEvent mr)
{
}
Not entirely sure what you are asking... I assume that your problem is that you have two labels and when you enter the mouse into one of them you want just that label to have a red background, not both.
To do so, you can get the label that triggered the mouse event using e.getComponent() and then set the background for that label only. Also, you might want to use setBackground(null) to reset the background color, since the background of the underlying frame might not always be white. Finally, you can use the MouseAdapter class instead of the MouseListener, providing defaults (no-op) for all those other method you do not need.
MouseListener ma = new MouseAdapter() {
public void mouseEntered(MouseEvent e) {
e.getComponent().setBackground(Color.RED);
}
public void mouseExited(MouseEvent e) {
e.getComponent().setBackground(null);
}
};
Your issue is the method setBackground(), change on setForeground():
entry.addMouseListener(this);
entry.setOpaque(true);
profile.addMouseListener(this);
profile.setOpaque(true);
public void mouseClicked(MouseEvent mc)
{}
public void mouseEntered(MouseEvent me)
{
entry.setForeground(Color.red);
profile.setForeground(Color.red);
}
public void mouseExited(MouseEvent me)
{
entry.setForeground(Color.white);
profile.setForeground(Color.white);
}
public void mousePressed(MouseEvent mp)
{}
public void mouseReleased(MouseEvent mr)
{}
Related
Why at first works chartMouseClicked (JFreeChart library), and already then mouseClicked?
boolean isDoubleClicked = false;
chartPanel.addMouseListener(new MouseListener() {
#Override
public void mouseClicked(MouseEvent me) {
if (me.getClickCount() == 2 &&) {
isDoubleClicked = true;
}
}
#Override
public void mousePressed(MouseEvent me) {}
#Override
public void mouseReleased(MouseEvent me) {}
#Override
public void mouseEntered(MouseEvent me) {}
#Override
public void mouseExited(MouseEvent me) {}
});
chartPanel.addChartMouseListener(new ChartMouseListener() {
#Override
public void chartMouseClicked(ChartMouseEvent cme) {
if (isDoubleClicked)
System.out.println("Double clicked!");
}
#Override
public void chartMouseMoved(ChartMouseEvent cme) {}
});
So, System.out.println("Double clicked!"); not works. How to correct it?
You have two different listener objects here, one is a MouseListener instance (that listens to mouse events on the panel) and the other is a ChartMouseListener instance (that listens to mouse events on the chart in the panel). They are registered in separate listener lists, and the isDoubleClicked field from one object isn't visible to the other object.
The reason that ChartMouseListener is separate from MouseListener is that JFreeChart creates its own events that contain additional information about the entity in a chart that is "underneath" the mouse pointer.
To add up on #DavidGilbert, you can also use ChartMouseEvent.getTrigger().getClickCount() to detect double-click in the chart.
Hi I'm trying to program a game using java. This is my first time using java, I am used to C#. In C# I would call Mouse.getLocation() and create a rect using the mouses location. Then by using if(Mouse.Left().toString() == "Pressed") I would then check if the mouse rect intersected with any other objects and act accordingly.
I've noticed in java you aren't provided with methods like these. So I was wondering, is the best way to approach mouse input simply to add listeners on all my clickable objects? I understand listeners and have a good idea how to use them but I was just wanting to check if there are more efficient ways to handle input or ways geared more towards what I'm most conformable with.
let your frame implement the MouseListener interface
implement all abstract methods, but in your case it is probably the mouseClicked event
identify if the button clicked is a left click, using the SwingUtilities class
if it is a left click, then set the x and y, which is the location of your click relative to the frame, not the screen.
public class MouseListeningObject extends JFrame implements MouseListener {
int x, y;
public MouseListeningObject () {
addMouseListener(this);
}
#Override
public void mouseClicked(MouseEvent e) {
if(SwingUtilities.isLeftMouseButton(e)){
x = e.getX();
y = e.getY();
}
}
#Override
public void mousePressed(MouseEvent e) {
// Some codes here
}
#Override
public void mouseReleased(MouseEvent e) {
// Some codes here
}
#Override
public void mouseEntered(MouseEvent e) {
// Some codes here
}
#Override
public void mouseExited(MouseEvent e) {
// Some codes here
}
}
You want your frame to implement MouseListener then add it in the constructor.
class MyFrame extends JFrame implements MouseListener {
MyFrame() {
addMouseListener(this);
}
#Override
public void mousePressed(MouseEvent e) {}
#Override
public void mouseEntered(MouseEvent e) {}
#Override
public void mouseExited(MouseEvent e) {}
#Override
public void mouseReleased(MouseEvent e) {}
}
for(int k=0;k< dtm.getRowCount();k++) //dtm is object of default table model
{
if(String.valueOf(dtm.getValueAt(k,1)).equalsIgnoreCase("Today") && check==0 )
{
cnt++;
JLabel jp=new JLabel();
panel.add(jp);
panel.setLayout(null);
if(cnt<=12)
{
jp.setBounds(j,500,100,100);
j=j+115;
jp.addMouseListener(this);
}
else
{
j=j-115;
jp.setBounds(j,400,100,100);
}
String b="<html><body text=#FDFA0B>"+String.valueOf(dtm.getValueAt(k,0))+"'s Birthday";
jp.setText(b);
jp.setFont(new java.awt.Font("comicbd",Font.ITALIC+Font.BOLD, 14));
}
}
It will not work mouselister only apply for last placed Label...
I want to apply mouse listener for all label how can I do that ..
please help me ....
Without SSCCE I can tell you that you're adding listener on 3 conditions:
String.valueOf(dtm.getValueAt(k,1)).equalsIgnoreCase("Today")
check == 0
and if(cnt<=12)
Other JLabels (that don't pass these conditions) haven't assigned your listener.
Make sure that you're clicking correct labels.
Or move jp.addMouseListener(this); just after JLabel creation (if you want to add listener to all your JLabels).
You certainly can add the same MouseListener to multiple components - here's an example in it's simplest form:
MouseListener ml = new MouseListener() {
#Override
public void mouseReleased(MouseEvent e) {System.out.println("Released!");}
#Override
public void mousePressed(MouseEvent e) {System.out.println("Pressed!");}
#Override
public void mouseExited(MouseEvent e) {System.out.println("Exited!");}
#Override
public void mouseEntered(MouseEvent e) {System.out.println("Entered!");}
#Override
public void mouseClicked(MouseEvent e) {System.out.println("Clicked!");}
};
JLabel j1 = new JLabel("Label1");
j1.addMouseListener(ml);
JLabel j2 = new JLabel("Label2");
j2.addMouseListener(ml);
BUT according to your code, you're messing with a JTable - and JTable's act differently than you're thinking. The labels you're trying to edit are actually part of a TableCellEditor. The JTable uses the single TableCellEditor (read: single JLabel) to display every cell in the JTable. This is why you're only seeing the Listener applied to the last cell (because that's the only the last cell has a full component any more - the rest are just ghosts of where the component was applied before).
The good news is you can add a MouseListener to the JTable, and obtain information from there:
final JTable table = new JTable();
MouseListener ml = new MouseListener() {
#Override
public void mouseReleased(MouseEvent e) {
System.out.println(table.getModel().getValueAt(table.getSelectedRow(), table.getSelectedColumn()));
}
#Override
public void mousePressed(MouseEvent e) {
System.out.println(table.getModel().getValueAt(table.getSelectedRow(), table.getSelectedColumn()));
}
#Override
public void mouseExited(MouseEvent e) {
System.out.println(table.getModel().getValueAt(table.getSelectedRow(), table.getSelectedColumn()));
}
#Override
public void mouseEntered(MouseEvent e) {
System.out.println(table.getModel().getValueAt(table.getSelectedRow(), table.getSelectedColumn()));
}
#Override
public void mouseClicked(MouseEvent e) {
System.out.println(table.getModel().getValueAt(table.getSelectedRow(), table.getSelectedColumn()));
}
};
table.addMouseListener(ml);
One Option is to add another inner class:
class MListener extends MouseAdapter{
public void mouseReleased(MouseEvent e) {}
//other mouse evetns
}
then rather then:
jp.addmousListener(this);
do:
jp.addMouseListener(new MListener());
I'm developing a Swing based application in which I want to add JToolBar with images in JButton but it's not looking good. JToolBar is having some dots at the starting part.
How can I get rid of the dots?
Two things:
The "dots" you describe are probably due to the JToolbar being floatable by default. If you wish to disable this you can call setFloatable(false).
Here's a utility method I use to decorate JButtons prior to adding them to JToolBars (or JPanels, etc):
decorateButton(AbstractButton)
public static void decorateButton(final AbstractButton button) {
button.putClientProperty("hideActionText", Boolean.TRUE);
button.setBorder(BorderFactory.createEmptyBorder());
button.setBackground(null);
button.setOpaque(true);
button.setPreferredSize(BUTTON_SIZE);
button.setMaximumSize(BUTTON_SIZE);
button.setMinimumSize(BUTTON_SIZE);
button.addMouseListener(new MouseAdapter() {
#Override
public void mouseReleased(MouseEvent e) {
button.setBackground(COLOR_BUTTON_MOUSEOVER);
}
#Override
public void mousePressed(MouseEvent e) {
button.setBackground(COLOR_BUTTON_PRESSED);
}
#Override
public void mouseEntered(MouseEvent e) {
button.setBorder(button.isEnabled() ? BORDER_BUTTON_MOUSEOVER_ENABLED : BORDER_BUTTON_MOUSEOVER_DISABLED);
button.setBackground(COLOR_BUTTON_MOUSEOVER);
}
#Override
public void mouseExited(MouseEvent e) {
button.setBorder(BorderFactory.createEmptyBorder());
button.setBackground(null);
}
});
}
I'd like to create an app where some events are supposed to be handled as if they were delivered to parent containers. For example I've got a JPanel which contains JLabel. The top JPanel implements mousepress and dragging right now. What do I need to do, in order to make the events look like they arrived to JPanel instead of the label itself. (changing source object is important)
Is there some better solution than actually implementing the events and replicating them in the parent? (this would get tedious after some objects with >5 children).
At your event listener, you can dispatch the event to the parent component.
Being myEvent the event handling function argument:
Component source=(Component)myEvent.getSource();
source.getParent().dispatchEvent(myEvent);
But this solution implies creating a new EventListener for each element to add.
So, you could create a single event handler and reuse it, adding it to all the chosen children, like this:
final Container parent=this; //we are a the parent container creation code
MouseListener myCommonListener=new MouseListener() {
#Override
public void mouseClicked(MouseEvent e) {
parent.dispatchEvent(e);
}
#Override
public void mouseEntered(MouseEvent e) {
parent.dispatchEvent(e);
}
#Override
public void mouseExited(MouseEvent e) {
parent.dispatchEvent(e);
}
#Override
public void mousePressed(MouseEvent e) {
parent.dispatchEvent(e);
}
#Override
public void mouseReleased(MouseEvent e) {
parent.dispatchEvent(e);
}
};
JLabel label=new JLabel("This is the first Label");
label.addMouseListener(myCommonListener);
JLabel label2=new JLabel("This is the second Label");
label2.addMouseListener(myCommonListener);
//... and so on
You should convert event before dispatching it to the parent. Conversion includes coordinates translation to parent-relative.
public class RedispatchingMouseAdapter implements MouseListener, MouseWheelListener, MouseMotionListener{
public void mouseClicked(MouseEvent e) {
redispatchToParent(e);
}
public void mousePressed(MouseEvent e) {
redispatchToParent(e);
}
public void mouseReleased(MouseEvent e) {
redispatchToParent(e);
}
public void mouseEntered(MouseEvent e) {
redispatchToParent(e);
}
public void mouseExited(MouseEvent e) {
redispatchToParent(e);
}
public void mouseWheelMoved(MouseWheelEvent e){
redispatchToParent(e);
}
public void mouseDragged(MouseEvent e){
redispatchToParent(e);
}
public void mouseMoved(MouseEvent e) {
redispatchToParent(e);
}
private void redispatchToParent(MouseEvent e){
Component source = (Component) e.getSource();
MouseEvent parentEvent = SwingUtilities.convertMouseEvent(source, e, source.getParent());
source.getParent().dispatchEvent(parentEvent);
}
}
Mouse events are automatically targeted to the deepest component that has mouse listeners.
Because of this, to achieve your goal, you can simply remove all mouse listeners on the JLabel and it will never get picked as the target for mouse events.
The following code will disable mouse listeners on the given components and their children recursively:
public static void disableMouseForComponent(Component... components) {
for (Component c : components) {
if (c instanceof Container) {
disableMouseForComponent(((Container) c).getComponents());
}
for (MouseListener l : c.getMouseListeners()) {
c.removeMouseListener(l);
}
for (MouseMotionListener l : c.getMouseMotionListeners()) {
c.removeMouseMotionListener(l);
}
}
}