• 0

Extending JButton issues


Question

Hey all. I want to create some custom buttons throughout my program so I thought I could create a new class that extends JButton, then override the methods I want, however it's not working like I wanted.

So I wanted to start with the following. I don't want the border painted and I dont want the area filled, so I tried overriding the methods. The docs say it has a boolean b, so I tried setting that to false. Now since I overrode it, I'm missing the rest of the method correct? If so, how do I find the rest of it? Is this the best way to achieve what I want. The scenario is I have several buttons and I want to change them, like a css sheet. (I tried using JavaFX but that seems insane)


import javax.swing.JButton;

@SuppressWarnings("serial")
public class SCJButton extends JButton {
/**
* @param args
*/


public SCJButton(String label){
super(label);
}

@Override
public void setBorderPainted(boolean b){
b = false;
}

@Override
public void setContentAreaFilled(boolean b){
b=false;
}

}
[/CODE]

Link to comment
https://www.neowin.net/forum/topic/1129072-extending-jbutton-issues/
Share on other sites

3 answers to this question

Recommended Posts

  • 0

I'm not sure I'm understanding your question correctly, but can't you use the base-class methods to achieve what you want?


import javax.swing.JButton;

@SuppressWarnings("serial")
public class SCJButton extends JButton {
/**
* @param args
*/


public SCJButton(String label){
super(label);
}

@Override
public void setBorderPainted(boolean b){
super.setBorderPainted(false);
}

@Override
public void setContentAreaFilled(boolean b){
super.setContentAreaFilled(false);
}

}
[/CODE]

This would force the border and content area not to be filled I think (my knowledge of Java and the JButton class is pretty limited), but it's bad because it diverts from user expectations (i.e. [i]"I did setBorderPainted(true), but it still shows false!"[/i]). I think you'd be better off just throwing an exception in those methods instead. Like this:

[code]
public SCJButton(String label){
super(label);
super.setBorderPainted(false);
super.setContentAreaFilled(false);
}

@Override
public void setBorderPainted(boolean b){
throw new UnsupportedOperationException("SCJButton class does not support borders");
}

@Override
public void setContentAreaFilled(boolean b){
throw new UnsupportedOperationException("SCJButton class does not support filling the content area");
}

}
[/CODE]

That still sucks IMO, but there's not much more you can do without inheriting from java.awt.Container, and it'd take you a long time to re-implement all the component methods.

This topic is now closed to further replies.
  • Posts

    • Those persons has complete control over the internet right now. They do see everything what we do regardless.
    • Everyone and every country who doesn't support Israel's aggression, terrorism and hypocrisy is their immediate enemy. You can definitely see how many innocent people they are killing almost everyday. In fact they're the actual Neo-Nazi who holds Hitler's ideology.
    • Just pull a 4Chan and ignore the UK gov, or better troll them. It's not like they can enforce the fine across border.
    • It has NEVER been shown that all these overreaching creepy methods of surveillance have ever saved a child or prevented a terrorist attack. Not a single one. It's the kind of people like you who just wave it away as "paranoid conspiracy" that makes big tech and governments this creepy mass data hoarding entities. Not only that, 3/4 of these surveillance ideas undermine the very foundations of safe online communication because they always want to have a backdoor in everything "just in case" they might need it to... checks the notes "save the children". If you put a backdoor into encryption chain there is no encryption chain anymore. You know what encryption keeps safe? Your medical records, your online shopping and credit card during payment, your photos in the cloud, your emails, your passwords, everything. There is ZERO guarantee only the good guys will use it. And if you think police suddenly can't apprehend child abusers because of encryption, Epstein was running his entire sex trafficking ring using GMail which is not even encrypted end to end. Or to make matters even worse, USA has a **** and a good buddy of Epstein as a president. Absolutely NOTHING has been done to address it. Maxwell just got a better "hotel" room as a reward. This clearly shows how they absolutely don't really care about the children but they care about the absolute control over all of us. And you're defending them here. Good grief. On top of constant attempts to insert backdoors into encryption chain, the entire age verification nonsense is again entirely over reaching, creepy, invades everyone's privacy with premise of yet again "protecting the children" instead of demanding device makers to provide simple and powerful tools for PARENTS to control how their children use devices and what they do on them. THIS would be the way, not the stupid age verification for everyone. Imagine if government would be dictating companies how their phones work and not the company's IT department. The parents should be the IT department to their children. And for everyone excusing "they are not knowledgeable enough" buuuuuulsheat. We live in a digital age, if you have children now, you absolutely are well versed in digital everything at least to basic extent. If you're not, how do you even function in these times then? Reality is that parents are just lazy and don't want to deal with this. They want government to raise their kids because they are too busy scrolling stupid Instagram and Tiktok or some bs.
    • You could make the argument that K should not be included, but FC, the fried chicken, is not the framework, it's the product. It's the Paint in Paint.NET. A closer analogy is if KFC included the name of the deep fryer they used. HennyPennyFC.
  • Recent Achievements

    • Very Popular
      Captain_Eric earned a badge
      Very Popular
    • One Month Later
      amusc earned a badge
      One Month Later
    • One Month Later
      DJC50PLUS earned a badge
      One Month Later
    • Week One Done
      DJC50PLUS earned a badge
      Week One Done
    • Proficient
      Eric Biran went up a rank
      Proficient
  • Popular Contributors

    1. 1
      +primortal
      507
    2. 2
      PsYcHoKiLLa
      221
    3. 3
      ATLien_0
      92
    4. 4
      +Edouard
      88
    5. 5
      Steven P.
      83
  • Tell a friend

    Love Neowin? Tell a friend!