Tags down


Creating a custom JButton in Java

By : user73738
Date : July 28 2020, 06:00 PM
I wish this helpful for you When I was first learning Java we had to make Yahtzee and I thought it would be cool to create custom Swing components and containers instead of just drawing everything on one JPanel. The benefit of extending Swing components, of course, is to have the ability to add support for keyboard shortcuts and other accessibility features that you can't do just by having a paint() method print a pretty picture. It may not be done the best way however, but it may be a good starting point for you.
Edit 8/6 - If it wasn't apparent from the images, each Die is a button you can click. This will move it to the DiceContainer below. Looking at the source code you can see that each Die button is drawn dynamically, based on its value.
code :
public Dimension getPreferredSize()  
public Dimension getMinimumSize()  
public Dimension getMaximumSize()
public void paintComponent(Graphics g)

Share : facebook icon twitter icon

Creating a custom button in Java with JButton

By : user2987520
Date : March 29 2020, 07:55 AM
To fix this issue You will have to extend JButton class not AbstractButton. Try the following things and you will get idea.
The first move is to subclass JButton.

Creating custom JButton from images containing transparent pixels

By : user3312983
Date : March 29 2020, 07:55 AM
hope this fix your issue Since there were good elements in multiple answers, but none of the answers were complete on their own, I'll answer my own question so other people that have the same problem can try something similar.
I created my buttons using a new class which extends JButton, with a new constructor that takes a BufferedImage as parameter instead of an icon. The reason for that is that when I did something like myButton.getIcon(), it would return an Icon, then I'd have to make various manipulations on it to make it a BufferedImage of the right size, and it ended up not working anyway because it seems like the first cast to Icon made it lose the alpha data in the pixels, so I couldn't check to see if the user was clicking on transparent pixels or not.
code :
public class MyButton extends JButton
   private BufferedImage bufImg;

   public MyButton(BufferedImage bufImg)
      super(new ImageIcon(bufImg));
      this.bufImg = bufImg;
 public BufferedImage getBufImg()
      BufferedImage newImg = new BufferedImage(getSize().getWidth(), getSize().getHeight(), BufferedImage.TYPE_INT_ARGB); //Create a new buffered image the right size
      Graphics2D g2d = newImg.createGraphics();
      g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

      g2d.drawImage(bufImg, 0, 0, getSize().getWidth(), getSize().getHeight(), null);

      return newImg;
  private int clickAlphaValue(BufferedImage bufImg, int posX, int posY) 
    int alpha;

    alpha = (bufImg.getRGB(posX, posY) >>24) & 0x000000FF; //Gets the bit that contains alpha information

    return alpha;
myButton.addMouseListener(new MouseListener()

    public void mouseClicked(MouseEvent e)

    public void mousePressed(MouseEvent e)

    public void mouseReleased(MouseEvent e)
      if(clickAlphaValue(((myButton)e.getSource()).getBufImg(), e.getX(), e.getY()) != 0) //If alpha is not set to 0
        System.exit(0); //Or other things you want your button to do

    public void mouseEntered(MouseEvent e)

    public void mouseExited(MouseEvent e)

Java GUI, creating piano keys using JButton

Date : March 29 2020, 07:55 AM
will help you I'm not sure how you're laying out your buttons in the first place. I suggest you use JLayeredPane for the layout. With it, you specify z-index of each component - and it takes care of the rest. Have a look here: http://docs.oracle.com/javase/tutorial/uiswing/components/layeredpane.html#depth

Creating a custom JButton which surppress CustomTooltip

By : AngMic
Date : March 29 2020, 07:55 AM
I hope this helps you . To customize the behaviour of a component you should be overriding meethods, not creating a new instance of the class you are extending.
Something like:
code :
public class CButton extends JButton
    public CButton(String text, Icon icon)
        super(text, icon);

    public JToolTip createToolTip()
        JToolTip toolTip = super.createToolTip();
        toolTip.setFont(new Font("Arial", Font.PLAIN, 12));
        return toolTip;

Java MouseListener creating a Button without JButton

By : user2967464
Date : March 29 2020, 07:55 AM
Hope this helps I want to create a pause screen for my mini-game. For that, you press the esc button, and a picture will appear with the buttons: Resume, Options and Exit. In my MouseInput-class, is the mousePressed method. I just want, that if I press the mouse at the location of the button, that something happens. I've already made the half, but I don't know the rest. The method would now only use a corner of the box.(Sorry if my english is bad) , the location should be a range not a point.
Example of range:
code :
if (mx >= 154 || mx<=200 && my >= 160 && my<= 200) {
Related Posts Related Posts :
  • Java EE 7 batch API (JSR-352): it's possible to stop a single step and not all the job?
  • Java multithread example - is object shared or not
  • Why String's replaceAll() method come at high performance cost at the first time and faster at the next time?
  • Lua script coverts empty array to object
  • Doesn't Access to external storage in API-29
  • How to add a button to the Android system navigation bar?
  • If inside for-each-loop to Stream
  • Shift array to the right by one slot
  • How to setup a Java development environment for Z3
  • how to make notify() works properly with wait()
  • IllegalFormatConversionException d != java.lang.Integer when trying to printf date?
  • Duplicate logic in backend and frontend with Domain Driven Design
  • How would I randomize an int[][]?
  • StringDate to Date coming in different Time in SimpleDateFormat in java
  • Get content uri from file path in android for video
  • function param issue in Scala 11, works in Scala 12 using resilience4j
  • Android unit testing : calling a method from fragment which uses context
  • How to remove E notation from Double value
  • Check if input is any of the values in an array
  • DateTimeParseException - could not be parsed at index 0
  • How to make a Java HTTP async request?
  • Make Javafx/Scenebuilder recognize tagged fields and methods when splitting the MainController into multiple files?
  • Why does this time fail to be parsed by DateTimeFormatter.ofPattern()?
  • Java - Merge objects of list given a condition
  • How to know if arraylist contains value in property of object?
  • Java modelling class if objects have same properties but they are fundamentally different
  • False Positive and False Negative
  • Java UnknownFormatConversionException = ','
  • Apache POI recording only 1 row in the spreadsheet
  • Getting Constructor from Java Class that would be called for argument types, not requiring exact argument & paramete
  • @JsonIgnore with Conditions
  • Does synchronizing on the static field that you are modifying make your code thread safe?
  • Compiler errors while trying to make a pop up
  • Trying to take two timestamps, subtract them, convert the difference to seconds and store in an int variable
  • How can I use Optionals instead of nested if statement?
  • How do I compare values in parallel arrays to eliminate duplicates?
  • make a method which returns the number of one digit numbers in an array
  • Does calling multiple times save() method of hibernate with same object insert new record in DB?
  • Single Number solving by Haspmap, return always be a "@"
  • What do you do about the JLabel classes? It says, "JLabel not a statement" for the error
  • Functional Interface call for a new Instance
  • When I tried to add ArrayList into ArrayList second ArrayList is repeating
  • How can I print the longest word from a user defined list?
  • enabling authentication in ignite
  • Providing OSGi Service Without Implementing Interface
  • Read manifest from JAR with gradle
  • Create one element Set the same way (if applicable) as create single element list
  • pass by reference an integer in java
  • how to generate the POI excel file in a specific location?
  • Issue trying to create a Bubble Sort using ArrayList<Integer>
  • How can I avoid typechecking in this situation
  • How to create a map<k,v> between first and last nested object using Java streams?
  • First time writing junit: Need suggestions
  • what is the best solution to split in chunk monthly given two dates startTime and EndTime in Java?
  • View Binding causes "The specified child already has a parent"
  • parse float in java with trailing junk
  • How to convert color from ColorPicker to String value in javaFX
  • Spring test integration not run how I deal with this error?
  • Java 8 Streams : Count the occurrence of elements(List<String> list1) from list of text data(List<String> li
  • What is the difference between static and protected in java?
  • shadow
    Privacy Policy - Terms - Contact Us © voile276.org