Could you help me with this little problem?
I'm trying to make a menu system which shows the options in a JEditorPane, it's something like this:
Welcome
Select an option.
1.) New register.
2.) New input.
3.) Exit.
the options are chosen by the user through a JTextField, when "1" is entered it shows another menu:
New register
1.) Option X.
2.) Option Y.
3.) Back.
and so on, the problem is that I don't know how I can capture the user's input, advance to the next menu, and re-capture the user's input all in a JTextField.
textField.addActionListener(new ActionListener () {
public void actionPerformed(ActionEvent e) {
String cap = "";
cap = textField.getText();
switch(cap) {
case "1":
paintEditorPane("Welcome");
// here is my problem, I don't know how to re-capture JTextField input
switch(cap){
case "1":
paintEditorPane("NewRegister");
break;
}
break;
}
}
});
Here's Basic. Now you have to make many cases to judge states.
public static class MainPanel extends JPanel{
private JTextArea textArea;
public MainPanel() {
this.setLayout(new BorderLayout());
this.textArea = new JTextArea();// you can use constructor to set Text but I like use method "setText".
this.textArea.addKeyListener(new keyHandler());
this.textArea.setText("Welcome\r\nSelect an option. 1.) New register. 2.) New input. 3.) Exit.\r\n");
this.textArea.setCaretPosition(this.textArea.getText().length());// move caret to last
this.add(this.textArea, BorderLayout.CENTER);
}
public void addText(String text) {textArea.setText(textArea.getText() + "\r\n" + text +"\r\n");}
public class keyHandler extends KeyAdapter{
#Override
public void keyReleased(KeyEvent e) {
switch(e.getKeyCode()){
case KeyEvent.VK_1 : addText("New register"); break;
case KeyEvent.VK_2 : addText("New input"); break;
case KeyEvent.VK_3 : addText("Exit"); break;
}
}
}
}
Related
In my java code below it produces a frame with a jtextrea. This allows for simple text processing. All I want to do is add " Sam". Which is 5 spaces with sam at the end. Every time the user hits enter. You can see also the gif I added below which is exactly what I am looking for.
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class text11 extends JFrame implements ActionListener {
// JFrame
static JFrame f;
// text area
static JTextArea jt;
// main class
public static void main(String[] args)
{
// create a new frame to store text field and button
f = new JFrame("textfield");
// create a label to display text
// create a object of the text class
text11 te = new text11();
// create a text area, specifying the rows and columns
jt = new JTextArea(" ", 20, 20);
JPanel p = new JPanel();
// add the text area and button to panel
p.add(jt);
f.add(p);
// set the size of frame
f.setSize(300, 300);
f.show();
}
#Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
}
}
String actionKey = "ADD_SAM";
InputMap inputMap = jt.getInputMap(JComponent.WHEN_FOCUSED);
KeyStroke enterPressed = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0);
inputMap.put(enterPressed, actionKey);
jt.getActionMap().put(actionKey, new TextAction(actionKey) {
#Override
public void actionPerformed(ActionEvent e) {
jt.append(" Sam\n");
}
});
To get input so you know when the use hits enter, you have to create your own KeyListener class. If you don't know how to use it, here is a handy link from the documentation you can use: https://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html.
But simply put, an KeyListener is an interface where you have to specify a few methods, but in your case I think the only one you need is keyPressed(KeyEvent e)(which is called pressed). If you're interested in the others, keyReleased(KeyEvent e) is when a key gets released, and keyType(KeyEvent e) is when it's pressed and released quickly. Then, use JFrames addKeyListener(KeyListener k) to add your custom action listener.
After you did that, you can use JTextArea's setText() and getText() method to append " sam" to the end (the 5 spaces get cut of by stack overflow, I know you want 5 spaces).
#Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
jt.setText(jt.getText() + " sam");
}
}
If you added the KeyListener correctly, you should be fine!
How to check from a class ModalDialog extends JDialog implements ActionListener if actionPerformed(ActionEvent e) method ocurred in another class (Connect extends JFrame implements ActionListener)? And one step further, how to check which of two buttons that I have in ModalDialog fired ActionPerformed method? (I know about event.getSource, but I need to check it from another class).
public ModalDialog()
{
btn8 = new Button("human");
btn8.setMaximumSize(new Dimension(60,40));
btn8.addActionListener(this);
btn9 = new Button("robot");
btn9.setMaximumSize(new Dimension(60,40));
btn9.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
}
class Connect extends JFrame implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
ModalDialog md = new ModalDialog();
if(md.ActionPerformed(e)....)...something like that...
}
}
How to check from a class ModalDialog extends JDialog implements ActionListener if actionPerformed(ActionEvent e)
This is a basic problem of how to return information from one class to another. The simple answer is to provide a getter method, which returns the selected value.
Start by defining the value to be returned, here I used a enum, as it clearly defines what could be returned
public enum Option {
HUMAN, ROBOT;
}
Update your ModalDialog to provide a getter to return the selected value
public class ModalDialog extends JDialog implements ActionListener {
private Option selection;
public ModalDialog() {
setModal(true);
Button btn8 = new Button("human");
btn8.addActionListener(this);
Button btn9 = new Button("robot");
btn9.addActionListener(this);
setLayout(new GridBagLayout());
add(btn8);
add(btn9);
pack();
}
public Option getSelection() {
return selection;
}
public void actionPerformed(ActionEvent e) {
//...
}
}
When the dialog is closed, the caller can now call getSelection to get the selected value (or null if the user closed the dialog via the [X] button
And one step further, how to check which of two buttons that I have in ModalDialog fired ActionPerformed method?
This is not an uncommon problem, and there a number of ways you might implement it. Since you've already implemented ActionListener at the class level, you could just make use of the actionCommand support available in buttons, which defaults to the text of the button
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
switch (cmd) {
case "human":
selection = Option.HUMAN;
break;
case "robot":
selection = Option.ROBOT;
break;
}
setVisible(false);
}
So now, when the dialog is closed, you can just request the selected value...
ModalDialog dialog = new ModalDialog();
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
Option selection = dialog.getSelection();
System.out.println("You choose " + selection);
I see this has been asked multiple times and apologize in advance if I'm just missing something simple...
I've created a custom JDialog with the examples provided in the Java docs here and from a similar question asked here.
My main application is a JFrame that contains a JPanel with an array of JButtons that display various employee names. I've added a custom ActionListener to each JButton that calls the mentioned JDialog:
//inner class for handling user button pushes
private class UserButtonHandler implements ActionListener
{
//handle button event
#Override
public void actionPerformed(ActionEvent event)
{
statusDialog = new ChangeDialog(statusWindow);
statusDialog.setVisible(true);
statusDialog.setLocationRelativeTo(null);
//set title for dialog box
String dialogTitle = "Status change for " + event.getActionCommand();
statusDialog.setTitle(dialogTitle);
statNum = ((ChangeDialog) statusDialog).getInputStatus();
System.out.println("Current num is: " + statNum);
//statNum = statusDialog.getInputStatus();
}
}
Here is the class for the custom JDialog (ChangeDialog):
class ChangeDialog extends JDialog implements ActionListener, PropertyChangeListener
{
//create panel where users can modify their status
private final ChangePanel empStatusChangePanel;
//text of buttons in dialog
private String btnString1 = "OK";
private String btnString2 = "Cancel";
private String btnString3 = "Clear Time-Off";
private JOptionPane statusPane;
//determines message to return for user input
private int inputStatus;
public ChangeDialog(JFrame statusFrame)
{
empStatusChangePanel = new ChangePanel();
//create an array specifying the number
//of dialog buttons and their text
Object[] options = {btnString1, btnString2, btnString3};
//create the JOptionPane
statusPane = new JOptionPane(empStatusChangePanel,
JOptionPane.PLAIN_MESSAGE,
JOptionPane.YES_NO_CANCEL_OPTION,
null,
options,
options[0]);
//set contents of dialog
setContentPane(statusPane);
//handle window closing
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
//register event handler for changes in status pane state
statusPane.addPropertyChangeListener(this);
pack();
}
#Override
public void actionPerformed(ActionEvent e)
{
statusPane.setValue(btnString1);
}
#Override
public void propertyChange(PropertyChangeEvent e)
{
String prop = e.getPropertyName();
if (isVisible()
&& (e.getSource() == statusPane)
&& (JOptionPane.VALUE_PROPERTY.equals(prop)))
{
Object value = statusPane.getValue();
if (value == JOptionPane.UNINITIALIZED_VALUE)
{
//ignore reset
return;
}
//Reset the JOptionPane's value. If this is not done,
//then if the user presses the same button next time,
//no property change event will be fired
statusPane.setValue(JOptionPane.UNINITIALIZED_VALUE);
if(value.equals(btnString1)) //user clicked "OK"
{
//validation of user input
inputStatus = empStatusChangePanel.validateUserInput();
//handle validation results
switch (inputStatus)
{
case 0: //user input is good
JOptionPane.showMessageDialog(this, "Good input given");
dispose();
break;
case 1: //one (or both) of the date pickers are empty
JOptionPane.showMessageDialog(this, "PTO pickers can't be empty.",
"ERROR", JOptionPane.ERROR_MESSAGE);
break;
case 2:
case 3: //bad date range (start before end or visa-versa)
JOptionPane.showMessageDialog(this, "Bad date range.",
"ERROR", JOptionPane.ERROR_MESSAGE);
break;
case 99: //dates are equal
JOptionPane.showMessageDialog(this, "Single-day PTO");
dispose();
break;
}
}
else if(value.equals(btnString3)) //user clicked "Clear Input"
{
JOptionPane.showMessageDialog(this, "User clicked 'clear input");
//more processing should be done here
empStatusChangePanel.recycle();
//dispose();
}
else //user clicked "Cancel" or closed dialog
{
JOptionPane.showMessageDialog(this, "User closed status window");
dispose();
}
}
}
//returns value from user validation
public int getInputStatus()
{
return inputStatus;
}
}
I need to access the method getInputStatus from the custom dialog but each attempt I've tried comes back stating that:
getInputStatus is undefined for the type JDialog
I have looked at several other similar posts but feel that I'm missing something fundamental in trying to solve this problem (or I've been looking at the code too long).
Another thing that has me stumped (and why I left it in the first snippet) is that if I cast the method to the type ChangeDialog
statNum = ((ChangeDialog) statusDialog).getInputStatus();
It suddenly has access (this was a suggestion from Eclipse and doesn't make sense to me). Thanks again for any and all help.
That is how inheritance works, you have defined statusDialog as a JDialog reference and JDialog doesn't have a getInputStatus method.
To access the members of ChangeDialog, you have to define statusDialog as variable of ChangeDialog.
I'm very new to coding(2 months) and i attempted to make Tic-Tac-Toe in java. I'm in a little over my head but i managed to create it using swing. My main problem is in the button1 class. I was going to use the getText() method but ended up not needing it or so i thought. I tried deleting it but as it turns out my tictactoe buttons don't switch letters without it. The compiler told me it overrides AbstractButton's getText() method but i don't see why that should matter since i never actually used it i thought. I'm thinking it's maybe a scope issue handled by it being overwritten somehow but i'm not sure. I was trying to use the text variable to update the button with setText() and that doesn't seem to work like i thought it should. I also don't understand why the 3 by 3 gridlayout seems to work properly most of the time but sometimes the number of buttons added is wrong.
So in summation the program works(mostly) but i'm not fully understanding how the button1 class is working.
TicTacToe.java
import java.awt.*;
import java.util.*;
import javax.swing.*;
public class TicTacToe extends JFrame {
public static void main(String[] args) {
JFrame window = new JFrame("Tic-Tac-Toe");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
window.setSize(600, 600);
window.setLayout(new GridLayout(3, 3));
ArrayList<button1> buttonArrayList = new ArrayList<>(9);
for (int i = 0; i < 9; i++) {
button1 newbutton = new button1();
buttonArrayList.add(newbutton);
window.add(buttonArrayList.get(i));
}
}
}
button1.java
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
public class button1 extends JButton {
int value = 0;
String text = "";
public button1() {
class ButtonAction extends AbstractAction {
public ButtonAction() {}
#Override
public void actionPerformed(ActionEvent Switcher) {
System.out.println(text + " " + value);
value++;//value is a relic from earlier attempts that i just felt like keeping.
if (text.equals("O")) {
text = "X";
} else if (text.equals("X")) {
text = "";
} else if (text.equals("")) {
text = "O";
}
}
}
this.setAction(new ButtonAction());
this.setText(text);
this.setFont(new Font("Arial",Font.PLAIN,120));
}
public String getText()// <----culprit
{
return text;
}
}
A JButton class has a methods defined for it, including setText() (which will set the displayed text on the button) and getText() (which will return the current text that is displayed on the button).
You created a class button1 (note: classes should start with Capital Letters).
You added an Action to the button1 class, which means that when the action is activated, something happens. Note that in that actionPerformed method, you should call setText(text) to update the displayed value.
You have also defined a getText() method that overrides the getText() method defined in JButton. This approach is fine if it is a conscious design decision. As it is, I think you should remove the getText() method from the button1 class, and allow the standard JButton class to handle the update. Right now, you are attempting to keep an instance variable text with the value, but it is possible for that instance variable to not be in alignment with the actual displayed value of the button (consider another class calling .setText() on the button).
EDIT: It is true that this referring to the JButton in the ButtonAction is not available. However, the Action itself contains the button that was pressed.
#Override
public void actionPerformed(ActionEvent e)
{
JButton btn = (JButton)e.getSource();
// if desired, String cur = btn.getText() may be called to find the
// current setting; get and process if needed
btn.setText(WHAT_EVER_TEXT);
}
Unless it is a specific requirement to process the current text, however (allowing selecting an O to an X to a blank), I would implement something to keep track of the current turn. This code is something I was experimenting with, and has good and bad points to it (as it is illustrative):
static class TurnController
{
// whose turn it is; start with X
private Player whoseTurn = Player.X;
// the instance variable
private static final TurnController instance = new TurnController();
private TurnController()
{
}
public static Player currentTurn()
{
return instance.whoseTurn;
}
public static Player nextTurn()
{
switch (instance.whoseTurn) {
case X:
instance.whoseTurn = Player.O;
break;
case O:
instance.whoseTurn = Player.X;
break;
}
return instance.whoseTurn;
}
public static String getMarkerAndAdvance()
{
String marker = currentTurn().toString();
nextTurn();
return marker;
}
enum Player
{
X,
O,
;
}
}
Using this TurnController, the actionPerformed becomes:
#Override
public void actionPerformed(ActionEvent e)
{
JButton btn = (JButton)e.getSource();
btn.setText(TurnController.getMarkerAndAdvance());
}
and the Button1 class may have the String text instance variable removed.
What you have tried is Try to make a Custom Button Class and its EventHandler just by extending AbstractAction namee button1 as we See in Your Question.
You have Override the method actionPerformed(ActionEvent Switcher) which actually belongs to Class AbstractAction by your own definition (What should Performed on Action Event of Every Button).
class ButtonAction extends AbstractAction {
public ButtonAction() {}
#Override
public void actionPerformed(ActionEvent Switcher) { // Your Definition For actionPerformed..
System.out.println(text + " " + value);
value++;//value is a relic from earlier attempts that i just felt like keeping.
if (text.equals("O")) {
text = "X";
} else if (text.equals("X")) {
text = "";
} else if (text.equals("")) {
text = "O";
}
}
}
this.setAction(new ButtonAction()); // add ActionListener to each Button.
this.setText(text); // Setting Text to each Button
this.setFont(new Font("Arial",Font.PLAIN,120)); //add Font to each Button.
}
Now In this Code.
ArrayList buttonArrayList = new ArrayList<>();
for (int i = 0; i < 9; i++) {
button1 newbutton = new button1(); // Creating 9 new buttons.
buttonArrayList.add(newbutton); // add each button into the ArrayList.
window.add(buttonArrayList.get(i)); // each Button to the the AWT Window.
}
Above Code will generate 9 Button and add it to Your AWT Window. each button have actionPerformed() method which contains the overrided Definition.
Now Each button will performed action as per the definition you give to actionPerformed() Method.
Thank You.
As you can see from my code I am totally missing a whole gaping concept here. My goal is to have the user input:
Jbutton text field:
text orientation and mnemonic
I pass that info to my Button class that checks for errors and gives back a button already defined.
Then populate the JFrame with said button as to the user specs.
BASIC and obviously a course question, but I'm at my wit's end here. This is the first time I've asked for help so please take it easy on me.
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
/**
* This class will create a button using the Button class and with user input to define the instance
* of the Button Class.
*
*/
public class CreateButton extends JPanel
{
// instance variables
private static String userIn; // user input
private static Button userButton; // button to be manipulated
public static JButton createdButton; // finished button
/**
* Contructor for the CreateButton class.
*/
public static void main (String [] args)
{
System.out.println("\nThis program will create a button with some input for you.");
System.out.println("What would you like to call this button?");
userIn = StringIn.get();
userButton = new Button();
userButton.buttonText(userIn);
System.out.println("\nYou can orient your text on the button in the vertical and");
System.out.println("horizontal axis. We will start with the vertical. Where would you like");
System.out.println("the text, on the top, center, or bottom?");
userIn = StringIn.get();
String vertical = userIn;
System.out.println("\nNext let's select the horizontal alignment. Would you like the text");
System.out.println("aligned to the left, center, or right?");
userIn = StringIn.get();
String horizontal = userIn;
userButton.textPosition(vertical,horizontal);
System.out.println("\nFinally let's add a mnemonic or hotkey to the button. Pick a letter");
System.out.println("from a-z on the keyboard and you can activate the button by pushing");
System.out.println("ALT + your letter of choice. Please enter your letter:");
userIn = StringIn.get();
userButton.buttonMnemomic(userIn);
System.out.println("\nGreat let's create and see this button.");
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
//Create and set up the window.
JFrame frame = new JFrame("Create a Button");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
CreateButton newContentPane = new CreateButton();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
}
My button error checker code is as follows:
import javax.swing.AbstractButton;
import javax.swing.JButton;
/**
* This class will demonstrate the use of a button.
*
*/
public class Button
{
// instance variables
protected JButton myButton; // button to be created and manipulated
private String myButtonText; // text on the button
private String myButtonVerticalTextPosition;
private String myButtonHorizontalTextPosition;
private String myButtonMnemonic; // hotkey for the button
/**
* Constructor for objects of class Button
*/
public Button()
{
}
/**
*Set button text. String input.
*/
public void buttonText(String textIn)
{
myButtonText = textIn;
}
/**
*Set button text position. String input for vertical (top, center, bottom) and horizontal
*(left, center, right).
*/
public void textPosition(String verticalIn, String horizontalIn)
{
myButtonVerticalTextPosition = verticalIn;
boolean validInput = false;
do
{
if ((myButtonVerticalTextPosition.compareToIgnoreCase("top") == 0)||
(myButtonVerticalTextPosition.compareToIgnoreCase("centre") == 0) ||
(myButtonVerticalTextPosition.compareToIgnoreCase("center") == 0) ||
(myButtonVerticalTextPosition.compareToIgnoreCase("bottom") == 0))
{
validInput = true;
} else
{
System.out.println("\nPlease enter top, center, or bottom for vertical position:");
myButtonVerticalTextPosition = StringIn.get();
}
} while (validInput == false);
myButtonHorizontalTextPosition = horizontalIn;
validInput = false;
do
{
if ((myButtonHorizontalTextPosition.compareToIgnoreCase("left") == 0) ||
(myButtonHorizontalTextPosition.compareToIgnoreCase("centre") == 0) ||
(myButtonHorizontalTextPosition.compareToIgnoreCase("center") == 0) ||
(myButtonHorizontalTextPosition.compareToIgnoreCase("right") == 0))
{
validInput = true;
} else
{
System.out.println("\nPlease enter left, center, or right for horizontal position:");
myButtonHorizontalTextPosition = StringIn.get();
}
} while (validInput == false);
}
/**
*Set button mnemomic. String input for mnemomic [a-z].
*/
public void buttonMnemomic(String mnemomicIn)
{
myButtonMnemonic = mnemomicIn;
boolean validInput = false;
do
{
if (myButtonMnemonic.length() > 1)
{
System.out.println("\nPlease enter a letter from a-z: ");
myButtonMnemonic = StringIn.get();
} else if (!myButtonMnemonic.matches("^[a-zA-Z]+$"))
{
System.out.println("\nPlease enter a letter from a-z: ");
myButtonMnemonic = StringIn.get();
} else if ((myButtonMnemonic.length() == 1) &&
(myButtonMnemonic.matches("^[a-zA-Z]+$")))
{
validInput = true;
}
} while (validInput == false);
}
/**
*Create button. Void method to create the button to the variables provided.
*/
public void createButton()
{
// create new button
myButton = new JButton(myButtonText);
// set text position
switch (myButtonVerticalTextPosition)
{
case "top":
myButton.setVerticalTextPosition(AbstractButton.TOP);
break;
case "centre":
myButton.setVerticalTextPosition(AbstractButton.CENTER);
break;
case "center":
myButton.setVerticalTextPosition(AbstractButton.CENTER);
break;
case "bottom":
myButton.setVerticalTextPosition(AbstractButton.BOTTOM);
break;
default:
System.err.format("%n%s is an invalid entry.", myButtonVerticalTextPosition);
break;
}
switch (myButtonHorizontalTextPosition)
{
case "left":
myButton.setHorizontalTextPosition(AbstractButton.LEADING);
break;
case "centre":
myButton.setHorizontalTextPosition(AbstractButton.CENTER);
break;
case "center":
myButton.setHorizontalTextPosition(AbstractButton.CENTER);
break;
case "right":
myButton.setHorizontalTextPosition(AbstractButton.TRAILING);
break;
default:
System.err.format("%n%s is an invalid entry.", myButtonVerticalTextPosition);
break;
}
// set button mnemonic
StringBuilder hotKey = new StringBuilder("KeyEvent.VK_");
hotKey.append(myButtonMnemonic.toUpperCase());
myButton.setMnemonic(hotKey.charAt(0));
// set tool tip text
myButton.setToolTipText("Push the button. You know you want to.");
}
/**
*Returns a JButton for the button type.
*/
public JButton returnButton()
{
return myButton;
}
}
This all works up to the part where you add the "createdButton". If I make it a default button it goes through the motions and puts up the default button.
FYI this is my StringIn code:
import java.util.Scanner;
import java.io.IOException;
/**
* This class will allow the user to type in string from the console.
*
*/
public class StringIn
{
// instance variables
private static String userIn;
public static String get()
{
try (Scanner in = new Scanner(System.in))
{
userIn = new String(in.nextLine()); // Read the string from console.
}
return userIn;
}
}
For the StringIn class just do:
import java.util.Scanner;
public class StringIn
{
// instance variables
private static Scanner scanner = new Scanner(System.in);
public static String get(){
return scanner.nextLine();
}
}
Edit2:
Okay, so I copied your code into my IDE and the error I got was an error originating in your StringIn class. ( I don't remember actually but that doesn't really matter. )
Your StringIn class should look like the above example.
For your createAndShowGUI() function:
private static void createAndShowGUI()
{
//Create and set up the window.
JFrame frame = new JFrame("Create a Button");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane. ( Why were you even doing this? )
frame.add(createdButton); //( To display the actual button you need to
//add it to the frame)
//Display the window.
frame.pack();
frame.setVisible(true);
}
Do like that^
I couldn't get the mnemonic related things to work properly, and I don't want to spend so much time on it, so I just removed them.
The orientation thingies didn't work either.
Put this at the bottom in main(String args[])
createdButton = userButton.createButton(); // Make createButton() return Button;
You don't need JButton anywhere in your code except for this part:
public class Button extends JButton
// And of course the import statement...
You can use Button.TOP instead of AbstractButton.TOP which will get rid of an import statement for you.