You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImageButton.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.ui_swing.components;
  18. import java.awt.Dimension;
  19. import java.awt.Insets;
  20. import javax.swing.BorderFactory;
  21. import javax.swing.Icon;
  22. import javax.swing.JButton;
  23. /**
  24. * Image button, also has the ability to hold an object.
  25. *
  26. * @param <T> Type of object the button holds.
  27. */
  28. public class ImageButton<T> extends JButton {
  29. /** A version number for this class. */
  30. private static final long serialVersionUID = 2;
  31. /** Button object. */
  32. private T object;
  33. /**
  34. * Creates a new instance of ImageButton.
  35. *
  36. * @param actionCommand Action command for the button
  37. * @param icon Normal icon for the button
  38. */
  39. public ImageButton(final String actionCommand, final Icon icon) {
  40. this(actionCommand, icon, icon);
  41. }
  42. /**
  43. * Creates a new instance of ImageButton.
  44. *
  45. * @param actionCommand Action command for the button
  46. * @param icon Normal icon for the button
  47. * @param rolloverIcon Rollover icon for the button
  48. */
  49. public ImageButton(final String actionCommand, final Icon icon,
  50. final Icon rolloverIcon) {
  51. this(actionCommand, icon, rolloverIcon, rolloverIcon);
  52. }
  53. /**
  54. * Creates a new instance of ImageButton.
  55. *
  56. * @param actionCommand Action command for the button
  57. * @param icon Normal icon for the button
  58. * @param rolloverIcon Rollover icon for the button
  59. * @param pressedIcon Pressed icon for the button
  60. */
  61. public ImageButton(final String actionCommand, final Icon icon,
  62. final Icon rolloverIcon, final Icon pressedIcon) {
  63. setIcon(icon);
  64. setRolloverIcon(rolloverIcon);
  65. setPressedIcon(pressedIcon);
  66. setContentAreaFilled(false);
  67. setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
  68. setMargin(new Insets(0, 0, 0, 0));
  69. setPreferredSize(new Dimension(16, 16));
  70. setActionCommand(actionCommand);
  71. }
  72. /**
  73. * Sets all the image buttons icons.
  74. *
  75. * @param icon New icon
  76. */
  77. public void setIcons(final Icon icon) {
  78. setIcon(icon);
  79. setRolloverIcon(icon);
  80. setPressedIcon(icon);
  81. }
  82. /**
  83. * Sets the object for this button.
  84. *
  85. * @param object Object to hold
  86. */
  87. public void setObject(final T object) {
  88. this.object = object;
  89. }
  90. /**
  91. * Gets this buttons object.
  92. *
  93. * @return This button's object
  94. */
  95. public T getObject() {
  96. return object;
  97. }
  98. }