0

I'm developing a software where a user has to setup the software according to the users' need before using it.

When the user clicks on a menu item the software throws a JDialog and asks for a user input and the software stores the input. This works fine. I've got a problem in the next bit. I want a toggle button (with the text entered by the user as its label) inside a panel. I tried using categoryPanel.add(C.getCategoryButton) but it didn't work. please help! Thanks in advance.

Here is what I've done... I've created a Category class that extends JToggleButton

public class Category extends JToggleButton implements ActionListener
{
    private JToggleButton categoryButton;


    public JToggleButton getCategoryButton()
    {
        buildCategoryButton();
        return categoryButton;
    }

    private void buildCategoryButton()
    {
        categoryButton = new JToggleButton();
        categoryButton.setText(MainFrame.getUserInput());
        categoryButton.setVisible(true);
    }

This is where the getCategoryButton() method is invoked

private void catCapBtnActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        userInput = catCapTextField.getText(); //works fine
        Category C = new Category();
        categoryPanel.add(C.getCategoryButton()); //doesn't work
        validate();

        catCapture.setVisible(false);//this closes the JDialog, and it works fine.
    } 
D.R.
  • 55
  • 8

1 Answers1

0

When you extend JToggleButton, the Category class you have created becomes an instance of JToggleButton. Therefore, you don't need a private instance of JToggleButton. I suggest something like the following:

public class Category extends JToggleButton implements ActionListener {

    public Category() {
        super(MainFrame.getUserInput());
    }

Also, if I'm not mistaken, buttons do not need to be set as visible.

jmhage
  • 63
  • 5
  • Thank You jmhage! It works! but not until you go full screen :( any idea why this is happening? – D.R. Feb 14 '17 at 23:46
  • @D.R. can you provide your main method? – jmhage Feb 15 '17 at 00:11
  • the main method looks like this.. `public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MainFrame().setVisible(true); } }); }` The constructor... `public MainFrame() { initComponents(); }` The initComponents() is too long – D.R. Feb 15 '17 at 22:22