Repaint and Mouselistener Java












0















So I have this paradox where I have a mouseListener added to my mainframe. When mouse entered this has a loop to check if the time that passed is exceeding a certain limit so it is registered a hold. It has to be in a thread because otherwise, I could not check for clicks as my main Thread would be blocked (or would it?). But the problem is that I want to show info while the mouse Button is still clicked requiring me to call repaint from within the thread, which won't work as repaint only works from the main Thread, yet this one has to be free for the MouseListener...
Does anyone have an idea how to solve this Problem?










share|improve this question

























  • Possible duplicate of Calling repaint in multiple threads?

    – Robin Green
    Nov 24 '18 at 16:47











  • "repaint only works from the main Thread" is not true. paint only works from the event dispatcher thread, but repaint is fine to use everywhere.

    – Robin Green
    Nov 24 '18 at 16:48











  • Hmm... yet whenerver i try calling repaint from the thread, it doesnt have any effect...

    – MDre
    Nov 24 '18 at 16:53











  • Post your code, so we can see what the problem is.

    – Robin Green
    Nov 24 '18 at 16:56











  • Let me try camickrs solution first and see if this works for me

    – MDre
    Nov 24 '18 at 17:10
















0















So I have this paradox where I have a mouseListener added to my mainframe. When mouse entered this has a loop to check if the time that passed is exceeding a certain limit so it is registered a hold. It has to be in a thread because otherwise, I could not check for clicks as my main Thread would be blocked (or would it?). But the problem is that I want to show info while the mouse Button is still clicked requiring me to call repaint from within the thread, which won't work as repaint only works from the main Thread, yet this one has to be free for the MouseListener...
Does anyone have an idea how to solve this Problem?










share|improve this question

























  • Possible duplicate of Calling repaint in multiple threads?

    – Robin Green
    Nov 24 '18 at 16:47











  • "repaint only works from the main Thread" is not true. paint only works from the event dispatcher thread, but repaint is fine to use everywhere.

    – Robin Green
    Nov 24 '18 at 16:48











  • Hmm... yet whenerver i try calling repaint from the thread, it doesnt have any effect...

    – MDre
    Nov 24 '18 at 16:53











  • Post your code, so we can see what the problem is.

    – Robin Green
    Nov 24 '18 at 16:56











  • Let me try camickrs solution first and see if this works for me

    – MDre
    Nov 24 '18 at 17:10














0












0








0








So I have this paradox where I have a mouseListener added to my mainframe. When mouse entered this has a loop to check if the time that passed is exceeding a certain limit so it is registered a hold. It has to be in a thread because otherwise, I could not check for clicks as my main Thread would be blocked (or would it?). But the problem is that I want to show info while the mouse Button is still clicked requiring me to call repaint from within the thread, which won't work as repaint only works from the main Thread, yet this one has to be free for the MouseListener...
Does anyone have an idea how to solve this Problem?










share|improve this question
















So I have this paradox where I have a mouseListener added to my mainframe. When mouse entered this has a loop to check if the time that passed is exceeding a certain limit so it is registered a hold. It has to be in a thread because otherwise, I could not check for clicks as my main Thread would be blocked (or would it?). But the problem is that I want to show info while the mouse Button is still clicked requiring me to call repaint from within the thread, which won't work as repaint only works from the main Thread, yet this one has to be free for the MouseListener...
Does anyone have an idea how to solve this Problem?







java multithreading jframe mouselistener






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 18:17







user10699454

















asked Nov 24 '18 at 16:44









MDreMDre

42




42













  • Possible duplicate of Calling repaint in multiple threads?

    – Robin Green
    Nov 24 '18 at 16:47











  • "repaint only works from the main Thread" is not true. paint only works from the event dispatcher thread, but repaint is fine to use everywhere.

    – Robin Green
    Nov 24 '18 at 16:48











  • Hmm... yet whenerver i try calling repaint from the thread, it doesnt have any effect...

    – MDre
    Nov 24 '18 at 16:53











  • Post your code, so we can see what the problem is.

    – Robin Green
    Nov 24 '18 at 16:56











  • Let me try camickrs solution first and see if this works for me

    – MDre
    Nov 24 '18 at 17:10



















  • Possible duplicate of Calling repaint in multiple threads?

    – Robin Green
    Nov 24 '18 at 16:47











  • "repaint only works from the main Thread" is not true. paint only works from the event dispatcher thread, but repaint is fine to use everywhere.

    – Robin Green
    Nov 24 '18 at 16:48











  • Hmm... yet whenerver i try calling repaint from the thread, it doesnt have any effect...

    – MDre
    Nov 24 '18 at 16:53











  • Post your code, so we can see what the problem is.

    – Robin Green
    Nov 24 '18 at 16:56











  • Let me try camickrs solution first and see if this works for me

    – MDre
    Nov 24 '18 at 17:10

















Possible duplicate of Calling repaint in multiple threads?

– Robin Green
Nov 24 '18 at 16:47





Possible duplicate of Calling repaint in multiple threads?

– Robin Green
Nov 24 '18 at 16:47













"repaint only works from the main Thread" is not true. paint only works from the event dispatcher thread, but repaint is fine to use everywhere.

– Robin Green
Nov 24 '18 at 16:48





"repaint only works from the main Thread" is not true. paint only works from the event dispatcher thread, but repaint is fine to use everywhere.

– Robin Green
Nov 24 '18 at 16:48













Hmm... yet whenerver i try calling repaint from the thread, it doesnt have any effect...

– MDre
Nov 24 '18 at 16:53





Hmm... yet whenerver i try calling repaint from the thread, it doesnt have any effect...

– MDre
Nov 24 '18 at 16:53













Post your code, so we can see what the problem is.

– Robin Green
Nov 24 '18 at 16:56





Post your code, so we can see what the problem is.

– Robin Green
Nov 24 '18 at 16:56













Let me try camickrs solution first and see if this works for me

– MDre
Nov 24 '18 at 17:10





Let me try camickrs solution first and see if this works for me

– MDre
Nov 24 '18 at 17:10












1 Answer
1






active

oldest

votes


















2















When mouse entered this has a loop to check if the time that passed is exeeding a certain limit




Don't use a loop. If this is executed in the listener then you will be blocking the Event Dispatch Thread (EDT).



Instead use a Swing Timer. When you enter the component you start the Timer. The Timer will then generate an event after your specified time interval.



However, you can also stop the Timer if some other event is generated and you want to reset the timer.



This will not block the Event Dispatch Thread (EDT) and events will still be generated normally.




i want to show info while the mouse Button is still clicked




Not sure what "still clicked" means. If the button is still pressed and you are executing code from an ActionListener you will block the EDT and the GUI won't be able to repaint itself until the long running task is completed.



Read the section from the Swing tutorial on Concurrency for more information about the EDT.






share|improve this answer


























  • I will try to implement this, thanks for helping me! As far as the Mouse pressed is concerned, my current setup for this event is that I set a bool in the MouseListener when i press to true. When the timelimit is exceeded, the hold function will be executed, unless i have already released the mouse which would set the bool to false.

    – MDre
    Nov 24 '18 at 17:07













  • So you have a Timer that you start with a mousePressed event and then you stop it with the mouseReleased event.

    – camickr
    Nov 24 '18 at 17:22











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460282%2frepaint-and-mouselistener-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2















When mouse entered this has a loop to check if the time that passed is exeeding a certain limit




Don't use a loop. If this is executed in the listener then you will be blocking the Event Dispatch Thread (EDT).



Instead use a Swing Timer. When you enter the component you start the Timer. The Timer will then generate an event after your specified time interval.



However, you can also stop the Timer if some other event is generated and you want to reset the timer.



This will not block the Event Dispatch Thread (EDT) and events will still be generated normally.




i want to show info while the mouse Button is still clicked




Not sure what "still clicked" means. If the button is still pressed and you are executing code from an ActionListener you will block the EDT and the GUI won't be able to repaint itself until the long running task is completed.



Read the section from the Swing tutorial on Concurrency for more information about the EDT.






share|improve this answer


























  • I will try to implement this, thanks for helping me! As far as the Mouse pressed is concerned, my current setup for this event is that I set a bool in the MouseListener when i press to true. When the timelimit is exceeded, the hold function will be executed, unless i have already released the mouse which would set the bool to false.

    – MDre
    Nov 24 '18 at 17:07













  • So you have a Timer that you start with a mousePressed event and then you stop it with the mouseReleased event.

    – camickr
    Nov 24 '18 at 17:22
















2















When mouse entered this has a loop to check if the time that passed is exeeding a certain limit




Don't use a loop. If this is executed in the listener then you will be blocking the Event Dispatch Thread (EDT).



Instead use a Swing Timer. When you enter the component you start the Timer. The Timer will then generate an event after your specified time interval.



However, you can also stop the Timer if some other event is generated and you want to reset the timer.



This will not block the Event Dispatch Thread (EDT) and events will still be generated normally.




i want to show info while the mouse Button is still clicked




Not sure what "still clicked" means. If the button is still pressed and you are executing code from an ActionListener you will block the EDT and the GUI won't be able to repaint itself until the long running task is completed.



Read the section from the Swing tutorial on Concurrency for more information about the EDT.






share|improve this answer


























  • I will try to implement this, thanks for helping me! As far as the Mouse pressed is concerned, my current setup for this event is that I set a bool in the MouseListener when i press to true. When the timelimit is exceeded, the hold function will be executed, unless i have already released the mouse which would set the bool to false.

    – MDre
    Nov 24 '18 at 17:07













  • So you have a Timer that you start with a mousePressed event and then you stop it with the mouseReleased event.

    – camickr
    Nov 24 '18 at 17:22














2












2








2








When mouse entered this has a loop to check if the time that passed is exeeding a certain limit




Don't use a loop. If this is executed in the listener then you will be blocking the Event Dispatch Thread (EDT).



Instead use a Swing Timer. When you enter the component you start the Timer. The Timer will then generate an event after your specified time interval.



However, you can also stop the Timer if some other event is generated and you want to reset the timer.



This will not block the Event Dispatch Thread (EDT) and events will still be generated normally.




i want to show info while the mouse Button is still clicked




Not sure what "still clicked" means. If the button is still pressed and you are executing code from an ActionListener you will block the EDT and the GUI won't be able to repaint itself until the long running task is completed.



Read the section from the Swing tutorial on Concurrency for more information about the EDT.






share|improve this answer
















When mouse entered this has a loop to check if the time that passed is exeeding a certain limit




Don't use a loop. If this is executed in the listener then you will be blocking the Event Dispatch Thread (EDT).



Instead use a Swing Timer. When you enter the component you start the Timer. The Timer will then generate an event after your specified time interval.



However, you can also stop the Timer if some other event is generated and you want to reset the timer.



This will not block the Event Dispatch Thread (EDT) and events will still be generated normally.




i want to show info while the mouse Button is still clicked




Not sure what "still clicked" means. If the button is still pressed and you are executing code from an ActionListener you will block the EDT and the GUI won't be able to repaint itself until the long running task is completed.



Read the section from the Swing tutorial on Concurrency for more information about the EDT.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 17:02

























answered Nov 24 '18 at 16:50









camickrcamickr

275k15127239




275k15127239













  • I will try to implement this, thanks for helping me! As far as the Mouse pressed is concerned, my current setup for this event is that I set a bool in the MouseListener when i press to true. When the timelimit is exceeded, the hold function will be executed, unless i have already released the mouse which would set the bool to false.

    – MDre
    Nov 24 '18 at 17:07













  • So you have a Timer that you start with a mousePressed event and then you stop it with the mouseReleased event.

    – camickr
    Nov 24 '18 at 17:22



















  • I will try to implement this, thanks for helping me! As far as the Mouse pressed is concerned, my current setup for this event is that I set a bool in the MouseListener when i press to true. When the timelimit is exceeded, the hold function will be executed, unless i have already released the mouse which would set the bool to false.

    – MDre
    Nov 24 '18 at 17:07













  • So you have a Timer that you start with a mousePressed event and then you stop it with the mouseReleased event.

    – camickr
    Nov 24 '18 at 17:22

















I will try to implement this, thanks for helping me! As far as the Mouse pressed is concerned, my current setup for this event is that I set a bool in the MouseListener when i press to true. When the timelimit is exceeded, the hold function will be executed, unless i have already released the mouse which would set the bool to false.

– MDre
Nov 24 '18 at 17:07







I will try to implement this, thanks for helping me! As far as the Mouse pressed is concerned, my current setup for this event is that I set a bool in the MouseListener when i press to true. When the timelimit is exceeded, the hold function will be executed, unless i have already released the mouse which would set the bool to false.

– MDre
Nov 24 '18 at 17:07















So you have a Timer that you start with a mousePressed event and then you stop it with the mouseReleased event.

– camickr
Nov 24 '18 at 17:22





So you have a Timer that you start with a mousePressed event and then you stop it with the mouseReleased event.

– camickr
Nov 24 '18 at 17:22




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53460282%2frepaint-and-mouselistener-java%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

Refactoring coordinates for Minecraft Pi buildings written in Python