javaswinguser-interfacenetbeans

Custom Bean Class for GUI Component in Netbeans


Alright so my professor posted up an assignment but the reference code that we're supposed to use is confusing me. He made a JFrame and inside that Jframe he put in a button, but the beanclass for that button is the subclass JHoverButton.Java which extends JButton. When I try to do that in my code, I can't get the Bean Class to be my own sub class. I've tried custom creation code, I've tried binding and I've looked around on stack overflow but I couldn't really find an answer. Any help would be greatly appreciated!

This is what my teacher posted This is what my teacher posted

This is where I'm stuck.enter image description here

Any help would really be appreciated, thank you!

Source Code for BeanProjectTest.Java:

package beanproject;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.plaf.metal.*;


 public class BeanProjectTest extends javax.swing.JFrame {

/**
 * Creates new form BeanProjectTest
 */
public BeanProjectTest() {
    initComponents();
    try{
        UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
        //UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");
        MetalLookAndFeel.setCurrentTheme(new DefaultMetalTheme());
        SwingUtilities.updateComponentTreeUI(this);

    }catch(Exception e){
        JOptionPane.showMessageDialog(this, e.toString());
    }
}

/**
 * This method is called from within the constructor to initialize the form.
 * WARNING: Do NOT modify this code. The content of this method is always
 * regenerated by the Form Editor.
 */
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    jColorChooser1 = new javax.swing.JColorChooser();
    jHoverButton1 = new beanproject.JHoverButton();
    jIntegerField1 = new beanproject.JIntegerField();
    jButton1 = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setTitle("Bean Project Test");

    jHoverButton1.setText("jHoverButton1");

    jIntegerField1.setText("jIntegerField1");

    jButton1.setText("jButton1");

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(38, 38, 38)
            .addComponent(jHoverButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 110, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addGap(53, 53, 53))
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jIntegerField1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap())
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(layout.createSequentialGroup()
            .addGap(43, 43, 43)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jHoverButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jButton1))
            .addGap(72, 72, 72)
            .addComponent(jIntegerField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(241, Short.MAX_VALUE))
    );

    pack();
}// </editor-fold>                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
    /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
     * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
     */

    //</editor-fold>

    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            new BeanProjectTest().setVisible(true);
        }
    });
}

// Variables declaration - do not modify                     
private javax.swing.JButton jButton1;
private javax.swing.JColorChooser jColorChooser1;
private beanproject.JHoverButton jHoverButton1;
private beanproject.JIntegerField jIntegerField1;
// End of variables declaration                   

}

Source Code For JHoverButton.Java:

public class JHoverButton extends JButton implements MouseListener{

public JHoverButton(){
    super();
    initialize();
}

private void initialize(){
    setBorderPainted(false);
    addMouseListener(this);
}

public JHoverButton(String text){
    super(text);
    initialize();
}

public JHoverButton(String text, Icon icon){
    super(text, icon);
    initialize();
}

public void setEnabled(boolean enabled){
    super.setEnabled(enabled);
    if(enabled){
        if(isBorderPainted()){
            setBorderPainted(false);
            repaint();
        }
    }
}

@Override
public void mouseClicked(MouseEvent me) {

}

@Override
public void mousePressed(MouseEvent me) {

}

@Override
public void mouseReleased(MouseEvent me) {

}

@Override
public void mouseEntered(MouseEvent me) {
   if(!isBorderPainted() && isEnabled()){
       setBorderPainted(true);
       repaint();
   }
}

@Override
public void mouseExited(MouseEvent me) {
    if(isBorderPainted()){
        setBorderPainted(false);
        repaint();
    }
}

}


Solution

  • As a starter please note that adding a third-party component would be really easier if you code your GUI by hand instead of using a GUI builder:

    JPanel panel = new JPanel();
    panel.add(new JHoverButton("Hello!"));
    ...
    frame.add(panel);
    

    Now having said that, instead of adding a JButton from the palette you need to add a new Bean and specify the full path to your component: packagename.ComponentName.

    1. Select Choose Bean option

    Beans palette

    2. Choose the appropriate bean by inserting the Class' full path

    Choose bean

    3. Place the JHoverButton component

    Place JHoverButton

    4. Inspect Bean's properties

    JHoverButton properties