JButtons Overlap Components When Hovered On












-1















I have set up a JPanel which is on top (Z-Axis) of a JButton. While hovering over this JPanel, if a JButton is also hovered on, the JButton automatically gets repainted on top of all components. This is not desirable for my program to work properly. Any ideas as to why this is happening and how I can fix this issue? Thanks for any help offered!



This is a quick and simple replica of my code:



setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JPanel panel = new JPanel();
panel.setBackground(new Color(0, 0, 102));
panel.setBounds(0, 0, 169, 261);
contentPane.add(panel);
panel.setVisible(false);

JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel.setVisible(!panel.isVisible());
}
});
btnNewButton.setBounds(68, 70, 130, 70);
contentPane.add(btnNewButton);


The JPanel begins on top (Z-Axis) until the JButton is hovered on (even if the JButton is covered by the JPanel). I hope this is enough information for your requirements.










share|improve this question




















  • 1





    You're overriding a paint method without calling it's super method. You're using alpha based color for the background color of a opaque component ... as a couple of guesses. For something more concrete, consider providing a Minimal, Complete, and Verifiable example

    – MadProgrammer
    Jan 2 at 3:04








  • 1





    "I have set up a JPanel which is on top of a JButton" - Why?

    – MadProgrammer
    Jan 2 at 3:10











  • It is used as a simple side menu in my program, I hope the edit will make the question more understandable.

    – Leandros Catania
    Jan 2 at 3:30






  • 1





    (1-) I hope the edit will make the question more understandable - not to me. First you say the panel is on the "top". I have no idea if you mean top as in the "north" part of the frame or "on top" like a popup menu that is displayed on the Z-axis, instead of the x/y axis. Now you say it is a "side" panel. Also all the relevant information should be included with the question so all the information is in one place. And you still haven't posted our Minimal, Complete, and Verifiable example so we still have no ideas what custom code you are using to cause the problem.

    – camickr
    Jan 2 at 4:04
















-1















I have set up a JPanel which is on top (Z-Axis) of a JButton. While hovering over this JPanel, if a JButton is also hovered on, the JButton automatically gets repainted on top of all components. This is not desirable for my program to work properly. Any ideas as to why this is happening and how I can fix this issue? Thanks for any help offered!



This is a quick and simple replica of my code:



setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JPanel panel = new JPanel();
panel.setBackground(new Color(0, 0, 102));
panel.setBounds(0, 0, 169, 261);
contentPane.add(panel);
panel.setVisible(false);

JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel.setVisible(!panel.isVisible());
}
});
btnNewButton.setBounds(68, 70, 130, 70);
contentPane.add(btnNewButton);


The JPanel begins on top (Z-Axis) until the JButton is hovered on (even if the JButton is covered by the JPanel). I hope this is enough information for your requirements.










share|improve this question




















  • 1





    You're overriding a paint method without calling it's super method. You're using alpha based color for the background color of a opaque component ... as a couple of guesses. For something more concrete, consider providing a Minimal, Complete, and Verifiable example

    – MadProgrammer
    Jan 2 at 3:04








  • 1





    "I have set up a JPanel which is on top of a JButton" - Why?

    – MadProgrammer
    Jan 2 at 3:10











  • It is used as a simple side menu in my program, I hope the edit will make the question more understandable.

    – Leandros Catania
    Jan 2 at 3:30






  • 1





    (1-) I hope the edit will make the question more understandable - not to me. First you say the panel is on the "top". I have no idea if you mean top as in the "north" part of the frame or "on top" like a popup menu that is displayed on the Z-axis, instead of the x/y axis. Now you say it is a "side" panel. Also all the relevant information should be included with the question so all the information is in one place. And you still haven't posted our Minimal, Complete, and Verifiable example so we still have no ideas what custom code you are using to cause the problem.

    – camickr
    Jan 2 at 4:04














-1












-1








-1








I have set up a JPanel which is on top (Z-Axis) of a JButton. While hovering over this JPanel, if a JButton is also hovered on, the JButton automatically gets repainted on top of all components. This is not desirable for my program to work properly. Any ideas as to why this is happening and how I can fix this issue? Thanks for any help offered!



This is a quick and simple replica of my code:



setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JPanel panel = new JPanel();
panel.setBackground(new Color(0, 0, 102));
panel.setBounds(0, 0, 169, 261);
contentPane.add(panel);
panel.setVisible(false);

JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel.setVisible(!panel.isVisible());
}
});
btnNewButton.setBounds(68, 70, 130, 70);
contentPane.add(btnNewButton);


The JPanel begins on top (Z-Axis) until the JButton is hovered on (even if the JButton is covered by the JPanel). I hope this is enough information for your requirements.










share|improve this question
















I have set up a JPanel which is on top (Z-Axis) of a JButton. While hovering over this JPanel, if a JButton is also hovered on, the JButton automatically gets repainted on top of all components. This is not desirable for my program to work properly. Any ideas as to why this is happening and how I can fix this issue? Thanks for any help offered!



This is a quick and simple replica of my code:



setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);

JPanel panel = new JPanel();
panel.setBackground(new Color(0, 0, 102));
panel.setBounds(0, 0, 169, 261);
contentPane.add(panel);
panel.setVisible(false);

JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
panel.setVisible(!panel.isVisible());
}
});
btnNewButton.setBounds(68, 70, 130, 70);
contentPane.add(btnNewButton);


The JPanel begins on top (Z-Axis) until the JButton is hovered on (even if the JButton is covered by the JPanel). I hope this is enough information for your requirements.







java swing jbutton






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 12:48







Leandros Catania

















asked Jan 2 at 3:01









Leandros CataniaLeandros Catania

134




134








  • 1





    You're overriding a paint method without calling it's super method. You're using alpha based color for the background color of a opaque component ... as a couple of guesses. For something more concrete, consider providing a Minimal, Complete, and Verifiable example

    – MadProgrammer
    Jan 2 at 3:04








  • 1





    "I have set up a JPanel which is on top of a JButton" - Why?

    – MadProgrammer
    Jan 2 at 3:10











  • It is used as a simple side menu in my program, I hope the edit will make the question more understandable.

    – Leandros Catania
    Jan 2 at 3:30






  • 1





    (1-) I hope the edit will make the question more understandable - not to me. First you say the panel is on the "top". I have no idea if you mean top as in the "north" part of the frame or "on top" like a popup menu that is displayed on the Z-axis, instead of the x/y axis. Now you say it is a "side" panel. Also all the relevant information should be included with the question so all the information is in one place. And you still haven't posted our Minimal, Complete, and Verifiable example so we still have no ideas what custom code you are using to cause the problem.

    – camickr
    Jan 2 at 4:04














  • 1





    You're overriding a paint method without calling it's super method. You're using alpha based color for the background color of a opaque component ... as a couple of guesses. For something more concrete, consider providing a Minimal, Complete, and Verifiable example

    – MadProgrammer
    Jan 2 at 3:04








  • 1





    "I have set up a JPanel which is on top of a JButton" - Why?

    – MadProgrammer
    Jan 2 at 3:10











  • It is used as a simple side menu in my program, I hope the edit will make the question more understandable.

    – Leandros Catania
    Jan 2 at 3:30






  • 1





    (1-) I hope the edit will make the question more understandable - not to me. First you say the panel is on the "top". I have no idea if you mean top as in the "north" part of the frame or "on top" like a popup menu that is displayed on the Z-axis, instead of the x/y axis. Now you say it is a "side" panel. Also all the relevant information should be included with the question so all the information is in one place. And you still haven't posted our Minimal, Complete, and Verifiable example so we still have no ideas what custom code you are using to cause the problem.

    – camickr
    Jan 2 at 4:04








1




1





You're overriding a paint method without calling it's super method. You're using alpha based color for the background color of a opaque component ... as a couple of guesses. For something more concrete, consider providing a Minimal, Complete, and Verifiable example

– MadProgrammer
Jan 2 at 3:04







You're overriding a paint method without calling it's super method. You're using alpha based color for the background color of a opaque component ... as a couple of guesses. For something more concrete, consider providing a Minimal, Complete, and Verifiable example

– MadProgrammer
Jan 2 at 3:04






1




1





"I have set up a JPanel which is on top of a JButton" - Why?

– MadProgrammer
Jan 2 at 3:10





"I have set up a JPanel which is on top of a JButton" - Why?

– MadProgrammer
Jan 2 at 3:10













It is used as a simple side menu in my program, I hope the edit will make the question more understandable.

– Leandros Catania
Jan 2 at 3:30





It is used as a simple side menu in my program, I hope the edit will make the question more understandable.

– Leandros Catania
Jan 2 at 3:30




1




1





(1-) I hope the edit will make the question more understandable - not to me. First you say the panel is on the "top". I have no idea if you mean top as in the "north" part of the frame or "on top" like a popup menu that is displayed on the Z-axis, instead of the x/y axis. Now you say it is a "side" panel. Also all the relevant information should be included with the question so all the information is in one place. And you still haven't posted our Minimal, Complete, and Verifiable example so we still have no ideas what custom code you are using to cause the problem.

– camickr
Jan 2 at 4:04





(1-) I hope the edit will make the question more understandable - not to me. First you say the panel is on the "top". I have no idea if you mean top as in the "north" part of the frame or "on top" like a popup menu that is displayed on the Z-axis, instead of the x/y axis. Now you say it is a "side" panel. Also all the relevant information should be included with the question so all the information is in one place. And you still haven't posted our Minimal, Complete, and Verifiable example so we still have no ideas what custom code you are using to cause the problem.

– camickr
Jan 2 at 4:04












1 Answer
1






active

oldest

votes


















2














In Swing UIs, almost always use layout managers. See this to learn how to use layout managers:
https://docs.oracle.com/javase/tutorial/uiswing/layout/layoutlist.html



So, in your code remove these lines:



contentPane.setLayout(null);
panel.setBounds(0, 0, 169, 261);
btnNewButton.setBounds(68, 70, 130, 70);


And do something like:



contentPane.setLayout(new BorderLayout());
contentPane.add(panel, BorderLayout.CENTER);
contentPane.add(btnNewButton, BorderLayout.SOUTH);





share|improve this answer
























  • Thanks for the help! My issue has been resolved by this fix, wish you all the best.

    – Leandros Catania
    Jan 2 at 12:52











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%2f54000763%2fjbuttons-overlap-components-when-hovered-on%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














In Swing UIs, almost always use layout managers. See this to learn how to use layout managers:
https://docs.oracle.com/javase/tutorial/uiswing/layout/layoutlist.html



So, in your code remove these lines:



contentPane.setLayout(null);
panel.setBounds(0, 0, 169, 261);
btnNewButton.setBounds(68, 70, 130, 70);


And do something like:



contentPane.setLayout(new BorderLayout());
contentPane.add(panel, BorderLayout.CENTER);
contentPane.add(btnNewButton, BorderLayout.SOUTH);





share|improve this answer
























  • Thanks for the help! My issue has been resolved by this fix, wish you all the best.

    – Leandros Catania
    Jan 2 at 12:52
















2














In Swing UIs, almost always use layout managers. See this to learn how to use layout managers:
https://docs.oracle.com/javase/tutorial/uiswing/layout/layoutlist.html



So, in your code remove these lines:



contentPane.setLayout(null);
panel.setBounds(0, 0, 169, 261);
btnNewButton.setBounds(68, 70, 130, 70);


And do something like:



contentPane.setLayout(new BorderLayout());
contentPane.add(panel, BorderLayout.CENTER);
contentPane.add(btnNewButton, BorderLayout.SOUTH);





share|improve this answer
























  • Thanks for the help! My issue has been resolved by this fix, wish you all the best.

    – Leandros Catania
    Jan 2 at 12:52














2












2








2







In Swing UIs, almost always use layout managers. See this to learn how to use layout managers:
https://docs.oracle.com/javase/tutorial/uiswing/layout/layoutlist.html



So, in your code remove these lines:



contentPane.setLayout(null);
panel.setBounds(0, 0, 169, 261);
btnNewButton.setBounds(68, 70, 130, 70);


And do something like:



contentPane.setLayout(new BorderLayout());
contentPane.add(panel, BorderLayout.CENTER);
contentPane.add(btnNewButton, BorderLayout.SOUTH);





share|improve this answer













In Swing UIs, almost always use layout managers. See this to learn how to use layout managers:
https://docs.oracle.com/javase/tutorial/uiswing/layout/layoutlist.html



So, in your code remove these lines:



contentPane.setLayout(null);
panel.setBounds(0, 0, 169, 261);
btnNewButton.setBounds(68, 70, 130, 70);


And do something like:



contentPane.setLayout(new BorderLayout());
contentPane.add(panel, BorderLayout.CENTER);
contentPane.add(btnNewButton, BorderLayout.SOUTH);






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 4:35









Prasad KarunagodaPrasad Karunagoda

1,7031814




1,7031814













  • Thanks for the help! My issue has been resolved by this fix, wish you all the best.

    – Leandros Catania
    Jan 2 at 12:52



















  • Thanks for the help! My issue has been resolved by this fix, wish you all the best.

    – Leandros Catania
    Jan 2 at 12:52

















Thanks for the help! My issue has been resolved by this fix, wish you all the best.

– Leandros Catania
Jan 2 at 12:52





Thanks for the help! My issue has been resolved by this fix, wish you all the best.

– Leandros Catania
Jan 2 at 12:52




















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%2f54000763%2fjbuttons-overlap-components-when-hovered-on%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

Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

A Topological Invariant for $pi_3(U(n))$