Related
I want to assign dir array to comboBox. Is there any error in my code. I tried to display the dir array it contains the values but cannot assign it comboBox. Here is the code.
import java.awt.EventQueue;
public class ExpenseManager {
private JFrame frame;
private JTextField txtUserName;
private JLabel lblNewUserName;
private JButton btnDone;
private JButton btnLogin;
private JComboBox<?> comboBox;
private String[] dir = new String[100];
private String[] hello = {"Hii", "Hello"};
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ExpenseManager window = new ExpenseManager();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ExpenseManager() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
//#SuppressWarnings("unchecked")
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewUser = new JButton("New User");
btnNewUser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
lblNewUserName.setVisible(true);
txtUserName.setVisible(true);
btnDone.setVisible(true);
}
});
btnNewUser.setBounds(23, 34, 89, 23);
frame.getContentPane().add(btnNewUser);
txtUserName = new JTextField();
txtUserName.setBounds(240, 63, 134, 20);
frame.getContentPane().add(txtUserName);
txtUserName.setVisible(false);
txtUserName.setColumns(10);
lblNewUserName = new JLabel("Enter New UserName");
lblNewUserName.setBounds(240, 38, 134, 14);
lblNewUserName.setVisible(false);
frame.getContentPane().add(lblNewUserName);
btnDone = new JButton("Done");
btnDone.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String newUserName = txtUserName.getText();
if(!newUserName.isEmpty())
{
File f = new File("S:/Expense/" + newUserName);
if (f.exists() && f.isDirectory()) {
JOptionPane.showMessageDialog(null, "User already exists");
txtUserName.setText(null);
}
else{
boolean success = (new File("S:/Expense/" + newUserName)).mkdir();
if(!success){
JOptionPane.showMessageDialog(null, "Unable to register");
}else{
JOptionPane.showMessageDialog(null, "User registered Successfully");
}
}
}
else
{
JOptionPane.showMessageDialog(null, "Kindly enter User Name", "Invalid User Name", JOptionPane.ERROR_MESSAGE);
}
}
});
btnDone.setBounds(240, 103, 89, 23);
btnDone.setVisible(false);
frame.getContentPane().add(btnDone);
btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
File[] directories = new File("S:/Expense/").listFiles();
for(int i = 0; i < directories.length; i++)
{
dir[i] = directories[i].getName();
}
comboBox.setVisible(true);
}
});
btnLogin.setBounds(23, 68, 89, 23);
frame.getContentPane().add(btnLogin);
comboBox = new JComboBox<Object>(dir);
comboBox.setBounds(24, 138, 72, 20);
comboBox.setSelectedIndex(1);
comboBox.setVisible(false);
frame.getContentPane().add(comboBox);
}
}
You can use setModel with DefaultComboBoxModel which take an array instead like :
comboBox.setModel(new DefaultComboBoxModel(dir));
Most of the code posted is not relevant to the question asked, so it should be removed (see MCVE).
Review the following, note the comments:
public class ExpenseManager {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
new ExpenseManager();
} catch (Exception e) { e.printStackTrace(); }
}
});
}
public ExpenseManager() { initialize(); }
private void initialize() {
JFrame frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null); //better use a layout manger
JComboBox<String> comboBox = new JComboBox<>();
comboBox.setBounds(24, 138, 72, 20);
comboBox.setVisible(false);
frame.getContentPane().add(comboBox);
JButton btn = new JButton("Populate combo");
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
//populate combo
String[] dir = new String[5];
for(int i = 0; i < dir.length; i++) { dir[i] = "String "+ i;}
comboBox.setModel(new DefaultComboBoxModel<>(dir));
comboBox.setVisible(true);
btn.setEnabled(false); //disable button
}
});
btn.setBounds(23, 68, 89, 23);
frame.getContentPane().add(btn);
frame.setVisible(true);
}
}
EDIT implementation using a layout manager :
private void initialize() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JComboBox<String> comboBox = new JComboBox<>();
comboBox.setVisible(false);
frame.add(comboBox, BorderLayout.SOUTH); //using BorderLayout which is the default
JButton btn = new JButton("Populate combo");
btn.setPreferredSize(new Dimension(150,35));
btn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
//populate combo
String[] dir = new String[5];
for(int i = 0; i < dir.length; i++) { dir[i] = "String "+ i;}
comboBox.setModel(new DefaultComboBoxModel<>(dir));
comboBox.setVisible(true);
btn.setEnabled(false); //disable button
frame.pack(); //resize fram to fit the preferred size and layouts
}
});
frame.getContentPane().add(btn, BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
}
My problem is that I have a class that when the user types out the text displayed dispose() is called, which works the first time but if you don't close the program and open it again, dispose() is called, but doesn't do anything which breaks the program.
public class TypeMenu extends JDialog {
protected final JPanel contentPanel = new JPanel();
protected static JTextField inputTxtField;
protected static JTextField textField;
protected static JTextField introTxtField;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
Easy dialog = new Easy();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
* #param introTxtField2
* #param textField2
* #param inputTxtField2
*/
public TypeMenu(JTextField inputTxtField2, JTextField introTxtField2, JTextField textField2) {
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
contentPanel.add(getInputTxtField());
contentPanel.add(getTextField());
contentPanel.add(getIntroTxtField());
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
}
}
protected JTextField getInputTxtField() {
if (inputTxtField == null) {
inputTxtField = new JTextField();
inputTxtField.setHorizontalAlignment(SwingConstants.CENTER);
inputTxtField.addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent arg0) {
String strField = textField.getText();
char key = arg0.getKeyChar();
int length = strField.length();
if (Character.toLowerCase(strField.charAt(0)) == Character.toLowerCase(key)) {
inputTxtField.setText(" ");
textField.setText(strField.substring(1));
System.out.println(length);
System.out.println(strField);
if (length - 1 <= 0) {
dispose();
}
} else {
inputTxtField.setText(" ");
}
}
});
inputTxtField.setBounds(56, 177, 314, 40);
inputTxtField.setColumns(10);
}
return inputTxtField;
}
protected JTextField getIntroTxtField() {
if (introTxtField == null) {
introTxtField = new JTextField();
introTxtField.setHorizontalAlignment(SwingConstants.CENTER);
introTxtField.setFont(new Font("Tahoma", Font.BOLD, 15));
introTxtField.setText("Easy Mode");
introTxtField.setEditable(false);
introTxtField.setBounds(56, 11, 314, 29);
introTxtField.setColumns(10);
}
return introTxtField;
}
private JTextField getTextField() {
if (textField == null) {
textField = new JTextField();
textField.setHorizontalAlignment(SwingConstants.CENTER);
textField.setFont(new Font("Monospaced", Font.BOLD, 20));
textField.setBounds(10, 51, 414, 40);
}
return textField;
}
}
This is one of the child classes
public class Easy extends TypeMenu {
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
Easy dialog = new Easy();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public Easy() {
super(inputTxtField, introTxtField, textField);
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
contentPanel.add(getInputTxtField());
contentPanel.add(getIntroTxtField());
getString();
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
textField.selectAll();
}
}
private void getString() {
String str = textField.getText();
if (str.equals("")) {
String generator = StringGenerator.medium();
String nStr = "" + generator;
textField.setText(nStr);
}
}
}
The code that calls this class
public class StartMenu extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField introTxt;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
StartMenu dialog = new StartMenu();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Creates the dialog and creates the buttons that take the user to each variation of the game when pressed.
*/
public StartMenu() {
setBounds(100, 100, 450, 300);
getContentPane().setLayout(new BorderLayout());
contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
getContentPane().add(contentPanel, BorderLayout.CENTER);
contentPanel.setLayout(null);
{
introTxt = new JTextField();
introTxt.setFont(new Font("Tahoma", Font.BOLD, 12));
introTxt.setHorizontalAlignment(SwingConstants.CENTER);
introTxt.setText("Start Menu\r\n");
introTxt.setEditable(false);
introTxt.setBounds(75, 11, 276, 20);
contentPanel.add(introTxt);
introTxt.setColumns(10);
}
{
JButton btnEasyButton = new JButton("Easy Mode");
btnEasyButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
new Easy().setVisible(true);
}
});
btnEasyButton.setBounds(141, 42, 140, 23);
contentPanel.add(btnEasyButton);
}
{
JButton btnMediumButton = new JButton("Medium Mode");
btnMediumButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
new Medium().setVisible(true);
}
});
btnMediumButton.setBounds(141, 81, 140, 23);
contentPanel.add(btnMediumButton);
}
{
JButton btnHardButton = new JButton("Hard Mode");
btnHardButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
new Hard().setVisible(true);
}
});
btnHardButton.setBounds(141, 120, 140, 23);
contentPanel.add(btnHardButton);
}
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("OK");
okButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dispose();
}
});
okButton.setActionCommand("OK");
buttonPane.add(okButton);
getRootPane().setDefaultButton(okButton);
}
}
}
}
your text field is static so he will have only one instance across the application.
so in the method getIntroTxtField() you have if statement that says :
if (introTxtField == null)
in the first time this condition is true but when you create the new instance this condition is false because the instance of the static field is all ready created in the first and you are adding the key listener inside the condition
so the action listener will be added only in the first creation.
if you need to keep the static because you need in other class you need to remove the == null
protected JTextField getInputTxtField() {
inputTxtField = null;
{
inputTxtField = new JTextField();
inputTxtField.setHorizontalAlignment(SwingConstants.CENTER);
inputTxtField.addKeyListener(new KeyAdapter() {
#Override
public void keyReleased(KeyEvent arg0) {
String strField = textField.getText();
char key = arg0.getKeyChar();
int length = strField.length();
if (Character.toLowerCase(strField.charAt(0)) == Character.toLowerCase(key)) {
inputTxtField.setText(" ");
textField.setText(strField.substring(1));
System.out.println(length);
System.out.println(strField);
if (length - 1 <= 0) {
dispose();
}
} else {
inputTxtField.setText(" ");
}
}
});
inputTxtField.setBounds(56, 177, 314, 40);
inputTxtField.setColumns(10);
}
return inputTxtField;
}
or remove the static from you field declaration static is used in
shared instace only when you are using only one instace across the
application like sessionFactory or any thing that need to be created
only one time.
you code is a lot to read but i think your problem is that you are calling the dispose() of the wrong object. may you are calling it always for the first object and you are creating a new one, and the dispose of this new dialog will never be call,Check you code may be you are creating many object and the dispose() is not called at all. make sure that you have full control of your objects instance to know if you are diposing the wanted dialog.
I am trying to create a hangman game with a button for each letter of the alphabet. I have done this the hard way, using absolute positioning and multiple actionlisteners. Is there any way I can do both with a for loop?
Also, how can I implement my hangman using a polymorphic array? The way I have it now, I will use an if statement for each of the limbs. I'd wrather create the man on his own panel, then set the visibility for each limg to true as the use fails at guessing.
Any help is appreciated.
public class HangmanGui extends JFrame{
private JLabel headerLabel;
private JPanel man;
private Graphics gobj;
private JButton aButton ;
private JButton bButton ;
private JButton cButton ;
private JButton dButton ;
private JButton eButton ;
private JButton fButton ;
private JButton gButton ;
private JButton hButton ;
private JButton iButton ;
private JButton jButton ;
private JButton kButton ;
private JButton lButton ;
private JButton mButton ;
private JButton nButton ;
private JButton oButton ;
private JButton pButton ;
private JButton qButton ;
private JButton rButton ;
private JButton sButton ;
private JButton tButton ;
private JButton uButton ;
private JButton vButton ;
private JButton wButton ;
private JButton xButton ;
private JButton yButton ;
private JButton zButton ;
private JButton newWButton ;
private JButton showWButton ;
private JButton quitButton ;
private JButton startButton ;
private JLabel blankWord;
private JLabel titleWord;
private JFrame frame;
private JPanel hangman;
private FlowLayout layout;
private Container container;
/*
public static void main (String[] args){
GUITest gui = new GUITest();
gui.setSize(800,900);
gui.setVisible(true);
}
*/
//
// public GUITest()
// {
// }
public HangmanGui(){
buildGui();
}
public void buildGui(){
frame = new JFrame();
frame.getContentPane().setBackground(Color.WHITE);
frame.setBackground(Color.WHITE);
frame.setBounds(100, 100, 450, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
Font lblFont= new Font("Serif", Font.BOLD, 30);
JLabel[] uscore = new JLabel[15];
Man man = new Man();
titleWord = new JLabel("A Game of Hangman.");
titleWord.setBounds(260,10,500,150);
titleWord.setFont(lblFont);
add(titleWord);
// add(blankWord);
//frame.add(man);
this.add(man);
man.setBounds(100,100,400,400);
JPanel panel = new JPanel();
panel.setBounds(6, 232, 400, 400);
frame.getContentPane().add(panel);
layout = new FlowLayout();
container = getContentPane();
setLayout(null);
aButton = new JButton("A");
add(aButton);
aButton.setBounds(30, 520, 50, 29);
aButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
bButton = new JButton("B");
add(bButton);
bButton.setBounds(80, 520, 50, 29);
bButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
cButton = new JButton("C");
add(cButton);
cButton.setBounds(130, 520, 50, 29);
cButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
dButton = new JButton("D");
add(dButton);
dButton.setBounds(180, 520, 50, 29);
dButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
eButton = new JButton("E");
add(eButton);
eButton.setBounds(230, 520, 50, 29);
eButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
fButton = new JButton("F");
add(fButton);
fButton.setBounds(280, 520, 50, 29);
fButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
gButton = new JButton("G");
add(gButton);
gButton.setBounds(330, 520, 50, 29);
gButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
hButton = new JButton("H");
add(hButton);
hButton.setBounds(380, 520, 50, 29);
hButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
iButton = new JButton("I");
add(iButton);
iButton.setBounds(430, 520, 50, 29);
iButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
jButton = new JButton("J");
add(jButton);
jButton.setBounds(480, 520, 50, 29);
jButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
kButton = new JButton("K");
add(kButton);
kButton.setBounds(530, 520, 50, 29);
kButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
lButton = new JButton("L");
add(lButton);
lButton.setBounds(580, 520, 50, 29);
lButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
mButton = new JButton("M");
add(mButton);
mButton.setBounds(630, 520, 50, 29);
mButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
nButton = new JButton("N");
add(nButton);
nButton.setBounds(680, 520, 50, 29);
nButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
oButton = new JButton("O");
add(oButton);
oButton.setBounds(30, 550, 50, 29);
oButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
pButton = new JButton("P");
add(pButton);
pButton.setBounds(80, 550, 50, 29);
pButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
qButton = new JButton("Q");
add(qButton);
qButton.setBounds(130, 550, 50, 29);
qButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
rButton = new JButton("R");
add(rButton);
rButton.setBounds(180, 550, 50, 29);
rButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
sButton = new JButton("S");
add(sButton);
sButton.setBounds(230, 550, 50, 29);
sButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
tButton = new JButton("T");
add(tButton);
tButton.setBounds(280, 550, 50, 29);
tButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
uButton = new JButton("U");
add(uButton);
uButton.setBounds(330, 550, 50, 29);
uButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
vButton = new JButton("V");
add(vButton);
vButton.setBounds(380, 550, 50, 29);
vButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
wButton = new JButton("W");
add(wButton);
wButton.setBounds(430, 550, 50, 29);
wButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
xButton = new JButton("X");
add(xButton);
xButton.setBounds(480, 550, 50, 29);
xButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
yButton = new JButton("Y");
add(yButton);
yButton.setBounds(530, 550, 50, 29);
yButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
zButton = new JButton("Z");
add(zButton);
zButton.setBounds(580, 550, 50, 29);
zButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
startButton = new JButton("Start Game");
add(startButton);
startButton.setBounds(100, 700, 120, 29);
startButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
newWButton = new JButton("New Word");
add(newWButton);
newWButton.setBounds(250, 700, 120, 29);
newWButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
showWButton = new JButton("Show Word");
add(showWButton);
showWButton.setBounds(400, 700, 120, 29);
showWButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
quitButton = new JButton("Quit Game");
add(quitButton);
quitButton.setBounds(550, 700, 120, 29);
quitButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
}
}
import java.awt.Graphics;
import java.awt.Color;
import javax.swing.JPanel;
public class Man extends JPanel {
protected void paintComponent(Graphics g ){
super.paintComponent(g);
//gallows
g.fillRect(10, 250, 150, 20);
g.fillRect(40,70,10,200);
g.fillRect(40,70,60,10);
g.setColor(Color.yellow);
g.fillRect(95,70,5,25);
//head
g.setColor(Color.black);
g.drawOval(82,95,30,30);
//body
g.drawLine(97,125,97,150);
//left leg
g.drawLine(97,150,117,183);
//right leg
g.drawLine(97,150,77,183);
// right arm
g.drawLine(97,125,117,135);
//left arm
g.drawLine(97,125,77,135);
}
}
Use a layout manager to manage the sizing of your buttons, don't use setBounds(). You currently create one but don't set it and so its not used.
layout = new FlowLayout();
setLayout(null); // Why set null and not layout
Set the layout correctly and then you could can use a char in your for loop like
for(char c = 'A'; c <= 'Z'; c++)
{
JButton button = new JButton("" + c);
// add action listener also
add(button); // adding will add it using the layout manager
}
You can learn more about layout managers here.
You could try something like this:
// create a JPanel to hold the buttons that uses a GridLayout with 3 rows
JPanel buttonPanel = new JPanel(new GridLayout(3, ));
for (int i = 0; i < 26; ++i)
{
wButton = new JButton('A' + i);
buttonPanel.add(wButton);
// wButton.setBounds(30 + i * 50, 550, 50, 29); // avoid this
wButton.addActionListener(
new ActionListener()
{
#Override
public void actionPerformed(ActionEvent event)
{
}
}
);
}
// here add the buttonPanel to the main GUI
To break this down:
1) We use a for loop to go thru the 26 letters of the alphabet.
2) We create a button with the title being the letter. Because characters are actually just integers, adding 1 to 'A' yields 'B'. So, we can use 'A'+i as a little trick. Also, you could use an array of all the letters instead and do something like letter[i] there.
3) Avoid use of setBounds but rather use the layout managers to more easily and simply place and size the buttons.
Disclaimer: I haven't actually tested this, it's just something to get you on the right track.
Suggestions:
Yes, use an array or a collection such as either a simple array of JButton or a List<JButton> or its concrete implementation, an ArrayList<JButton>
Yes, create your letter JButtons in a for loop as many have suggested.
But don't create your special buttons there, buttons such as your reset button or your exit button.
Create one AbstractAction or ActionListener class for all the letter buttons to share and a unique AbstractAction or ActionListener class for each special JButton
Use layout managers, but not just one -- nest them. For instance, the overall GUI can use a BorderLayout with the drawing held BorderLayout.CENTER and the buttons in the SOUTH or PAGE_END position, the JPanels that hold your buttons could be held in a BoxLayout using JPanel, the letter buttons in a GridLayout using JPanel and the same for the specialty buttons.
Use a separate JPanel for drawing the hangman image. Keep its logic separate from everything else. All it cares about is the number of wrong letters guessed, and then it should draw the appropriate images depending on this value. So I'd give it a public method for incrementing this number as well as a public method for resetting the number and its drawing.
For instance
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
public class LayoutFoo extends JPanel {
// using a List of JButtons to hold my collection
private List<JButton> letterButtons = new ArrayList<>();
private DrawingPanel drawingPanel = new DrawingPanel();
public LayoutFoo() {
JPanel letterButtonPanel = new JPanel(new GridLayout(3, 0, 3, 3));
letterButtonPanel.setBorder(BorderFactory.createTitledBorder("Letters"));
ButtonListener buttonListener = new ButtonListener();
for (char c = 'A'; c <= 'Z'; c++) {
String text = String.valueOf(c);
JButton button = new JButton(text);
button.addActionListener(buttonListener);
letterButtons.add(button); // add JButton to List<JButton>
letterButtonPanel.add(button); // and add to GridLayout-using JPanel
}
// JPanel to hold non-letter JButtons
JPanel specialBtnsPanel = new JPanel(new GridLayout(1, 0, 3, 3));
specialBtnsPanel.add(new JButton(new ResetAction("Reset", KeyEvent.VK_R)));
specialBtnsPanel.add(new JButton(new ExitAction("Exit", KeyEvent.VK_X)));
// JPanel to hold non-drawing JPanels. It uses BoxLayout
JPanel bottomPanel = new JPanel();
bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.PAGE_AXIS));
bottomPanel.add(letterButtonPanel);
bottomPanel.add(specialBtnsPanel);
// set layout and border of main JPanel and add other JPanels to it
setBorder(BorderFactory.createEmptyBorder(3, 3, 3, 3));
setLayout(new BorderLayout(3, 3));
add(drawingPanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.PAGE_END);
}
private class ButtonListener implements ActionListener {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button pressed: " + e.getActionCommand());
((AbstractButton) e.getSource()).setEnabled(false);
}
}
private class ResetAction extends AbstractAction {
public ResetAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
for (JButton button : letterButtons) {
button.setEnabled(true);
}
}
}
private class ExitAction extends AbstractAction {
public ExitAction(String name, int mnemonic) {
super(name);
putValue(MNEMONIC_KEY, mnemonic);
}
#Override
public void actionPerformed(ActionEvent e) {
Component component = (Component) e.getSource();
Window win = SwingUtilities.getWindowAncestor(component);
win.dispose();
}
}
private static void createAndShowGui() {
LayoutFoo mainPanel = new LayoutFoo();
JFrame frame = new JFrame("LayoutFoo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class DrawingPanel extends JPanel {
private static final int PREF_W = 500;
private static final int PREF_H = PREF_W;
private int wrongLetterCount = 0;
public DrawingPanel() {
setBorder(BorderFactory.createTitledBorder("Hang Man"));
}
#Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
// TODO: draw hangman here based on wrong letter count!
}
public void incrementWrongLetterCount() {
wrongLetterCount++;
repaint();
}
public void reset() {
wrongLetterCount = 0;
repaint();
}
}
I'm creating the app using Eclipse Kepler...I've created 3 database in Eclipse- database, entity and UI. In database, it contains ContactDAO,UserDAO and DBManager(for database connection) . In Entity, it contains Contact and User. In UI, it contains ContactAdd, COntactDetails , ContactList, LoginFrame and MenuFrame JFrames. Now, I'm stuck at ContactList frame... it contains ComboBox and I've tried to run it but I can't get the names... this is the code:
public class ContactList extends JFrame {
private JPanel contentPane;
private JTextField txtSearch;
private JComboBox cbContactNames;
private MenuFrame parentFrame;
private Contact selContact;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ContactList frame = new ContactList(null);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ContactList(MenuFrame f) {
this.parentFrame = f;
setTitle("Personal Assistant");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
txtSearch = new JTextField();
txtSearch.setBounds(22, 11, 294, 20);
contentPane.add(txtSearch);
txtSearch.setColumns(10);
JButton btnSearch = new JButton("Search");
btnSearch.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
searchContact();
}
});
btnSearch.setBounds(326, 10, 89, 23);
contentPane.add(btnSearch);
JLabel lblName = new JLabel("Name");
lblName.setBounds(42, 88, 46, 14);
contentPane.add(lblName);
cbContactNames = new JComboBox();
cbContactNames.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange()==ItemEvent.SELECTED)
{
if (cbContactNames.getSelectedIndex()>0){
String name=(String)cbContactNames.getSelectedItem();
selContact=ContactDAO.findContactByName(name); //adjusted
showContact();
}
}
}
});
ArrayList<Contact> ContactList=ContactDAO.getAllContacts();
cbContactNames.addItem("--Select Name--");
for (int i=0; i<ContactList.size(); i++)
{
Contact contact = ContactList.get(i);
cbContactNames.addItem(contact.getName());
}
cbContactNames.setBounds(87, 85, 229, 20);
contentPane.add(cbContactNames);
JButton btnClose = new JButton("Close");
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
goBack();
}
});
btnClose.setBounds(158, 203, 89, 23);
contentPane.add(btnClose);
}
private void goBack() {
// TODO Auto-generated method stub
parentFrame.setVisible(true);
this.dispose();
}
private void showContact() {
// TODO Auto-generated method stub
ContactDetails contactdetails=new ContactDetails(this,selContact);
contactdetails.setVisible(true);
this.setVisible(false);
}
private void searchContact() {
String name=txtSearch.getText();
if (name!=null){
selContact=ContactDAO.findContactByName(txtSearch.getText());
}
showContact();
}
}
Then, I have also created the ContactDetails frame that contains the contact details, edit save and close buttons, and Delete label set as Image. The project says when the edit button is clicked, the save button will be visible... I don't know how to do that...Here's my code :
public class ContactDetails extends JFrame {
private JPanel contentPane;
private JTextField txtName;
private JTextField txtMobile;
private JTextField txtHome;
private JTextField txtEmail;
private JButton btnEdit;
private JButton btnSave;
private ContactList parentFrame;
private Contact contact;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ContactDetails frame = new ContactDetails(null,null);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public ContactDetails(ContactList f,Contact c) {
this.parentFrame = f;
this.contact=c;
setTitle("Contact Details");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblImage = new JLabel("");
lblImage.setIcon(new ImageIcon(ContactDetails.class.getResource("/images/kitty.png")));
lblImage.setBounds(34, 28, 79, 70);
contentPane.add(lblImage);
JLabel lblName = new JLabel("Name:");
lblName.setBounds(119, 48, 46, 14);
contentPane.add(lblName);
txtName = new JTextField(contact.getName());
txtName.setBounds(162, 45, 169, 20);
contentPane.add(txtName);
txtName.setColumns(10);
JLabel lblMobile = new JLabel("Mobile:");
lblMobile.setBounds(119, 93, 46, 14);
contentPane.add(lblMobile);
txtMobile = new JTextField();
txtMobile.setBounds(162, 90, 169, 20);
contentPane.add(txtMobile);
txtMobile.setColumns(10);
JLabel lblHome = new JLabel("Home:");
lblHome.setBounds(119, 141, 46, 14);
contentPane.add(lblHome);
txtHome = new JTextField();
txtHome.setBounds(162, 138, 169, 20);
contentPane.add(txtHome);
txtHome.setColumns(10);
JLabel lblEmail = new JLabel("Email:");
lblEmail.setBounds(119, 186, 46, 14);
contentPane.add(lblEmail);
txtEmail = new JTextField();
txtEmail.setBounds(162, 183, 169, 20);
contentPane.add(txtEmail);
txtEmail.setColumns(10);
JButton btnSave = new JButton("Save");
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
contactAdded();
}
});
btnSave.setBounds(184, 227, 74, 23);
contentPane.add(btnSave);
JButton btnEdit = new JButton("Edit");
btnEdit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
editContact();
}
});
btnEdit.setBounds(110, 227, 74, 23);
contentPane.add(btnEdit);
JButton btnClose = new JButton("Close");
btnClose.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
goBack();
}
});
btnClose.setBounds(257, 227, 74, 23);
contentPane.add(btnClose);
JLabel lblDelete = new JLabel("");
lblDelete.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent arg0) {
removeContact();
}
});
lblDelete.setIcon(new ImageIcon(ContactDetails.class.getResource("/images/trush.png")));
lblDelete.setBounds(365, 28, 46, 54);
contentPane.add(lblDelete);
}
private void contactAdded() {
// TODO Auto-generated method stub
String name=txtName.getText();
String mobile=txtMobile.getText();
String home=txtHome.getText();
String email=txtEmail.getText();
Contact contact = new Contact(name, mobile, home, email);
if(ContactDAO.create(contact)){
JOptionPane.showMessageDialog(contentPane, "Contact successfull saved!");
}
else {
JOptionPane.showMessageDialog(contentPane, "Contact not save!");
}
}
private void goBack() {
// TODO Auto-generated method stub
parentFrame.setVisible(true);
this.dispose();
}
private void removeContact() {
// TODO Auto-generated method stub
String name=txtName.getText();
String mobile=txtMobile.getText();
String home=txtHome.getText();
String email=txtEmail.getText();
Contact contact = new Contact(name, mobile, home, email);
if(ContactDAO.delete(contact)){
JOptionPane.showMessageDialog(contentPane, "Contact deleted");
}
else {
JOptionPane.showMessageDialog(contentPane, "Contact delete unsuccessful");
}
}
private void editContact() {
// TODO Auto-generated method stub
String name=txtName.getText();
String mobile=txtMobile.getText();
String home=txtHome.getText();
String email=txtEmail.getText();
Contact contact = new Contact(name, mobile, home, email);
if (ContactDAO.update(contact)){
JOptionPane.showMessageDialog(contentPane, "Contact successfully updated");
}
else {
JOptionPane.showMessageDialog(contentPane, "Contact update unsuccessful");
}
}
}
Also, the Save button is tricky for me... It seems that after clicking the Save button, a message should pop up for confirmation that should include (OK) button. When the (ok)button is clicked, Save button will be disabled and Edit button will be enabled again. All Text fields will be disabled. The (Delete Icon button) also needs a prompt box that includes (Yes, no and cancel) buttons. If confirm, contact will be deleted and user will be brought back to the contacts list frame... How should I do it?
Tell me if I have to create another JFrame or DAO...
You need somthing like this for your save button and same for remove button:
int showConfirmDialog = JOptionPane.showConfirmDialog(new JFrame(), "Approve?","Title",JOptionPane.YES_NO_OPTION);
if(showConfirmDialog == JOptionPane.YES_OPTION){
btnSave.setEnabled(false);
btnEdit.setEnabled(true);
txtName.setEnabled(false);
//other fields
} else {
//If no or cancel actions
}
Read tutorial for dialogs.
Watch showConfirmDialog(...)
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.*;
/*
* Created by JFormDesigner on Wed May 11 16:42:17 PDT 2011
*/
public class usernameForm extends JFrame {
private void passwordField1KeyPressed(KeyEvent e) {
// TODO add your code here
}
private void button1ActionPerformed(ActionEvent e) {
// TODO add your code here
}
private void textArea1MouseClicked(MouseEvent e) {
// TODO add your code here
}
private void textArea1FocusGained(FocusEvent e) {
// TODO add your code here
}
private void textArea1FocusLost(FocusEvent e) {
// TODO add your code here
}
private void button2ActionPerformed(ActionEvent e) {
// TODO add your code here
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
// Generated using JFormDesigner Evaluation license - ryan icknem
UsernameChecker = new JPanel();
panel1 = new JPanel();
textField1 = new JTextField();
passwordField1 = new JPasswordField();
button1 = new JButton();
label1 = new JLabel();
label2 = new JLabel();
scrollPane1 = new JScrollPane();
textArea1 = new JTextArea();
button2 = new JButton();
//======== UsernameChecker ========
{
// JFormDesigner evaluation mark
UsernameChecker.setBorder(new javax.swing.border.CompoundBorder(
new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0),
"JFormDesigner Evaluation", javax.swing.border.TitledBorder.CENTER,
javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialog", java.awt.Font.BOLD, 12),
java.awt.Color.red), UsernameChecker.getBorder())); UsernameChecker.addPropertyChangeListener(new java.beans.PropertyChangeListener(){public void propertyChange(java.beans.PropertyChangeEvent e){if("border".equals(e.getPropertyName()))throw new RuntimeException();}});
UsernameChecker.setLayout(null);
//======== panel1 ========
{
panel1.setBorder(new TitledBorder("Username Availability Checker"));
panel1.setLayout(null);
panel1.add(textField1);
textField1.setBounds(75, 25, 115, textField1.getPreferredSize().height);
//---- passwordField1 ----
passwordField1.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
passwordField1KeyPressed(e);
}
});
panel1.add(passwordField1);
passwordField1.setBounds(75, 57, 115, passwordField1.getPreferredSize().height);
//---- button1 ----
button1.setText("Login");
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
button1ActionPerformed(e);
}
});
panel1.add(button1);
button1.setBounds(75, 89, 115, button1.getPreferredSize().height);
//---- label1 ----
label1.setText("Username");
label1.setLabelFor(textField1);
panel1.add(label1);
label1.setBounds(10, 30, 65, label1.getPreferredSize().height);
//---- label2 ----
label2.setText("Password");
label2.setLabelFor(passwordField1);
panel1.add(label2);
label2.setBounds(15, 65, 60, 16);
//======== scrollPane1 ========
{
//---- textArea1 ----
textArea1.setText("Separate by comma and space");
textArea1.setTabSize(2);
textArea1.setLineWrap(true);
textArea1.setWrapStyleWord(true);
textArea1.setFont(new Font("Calibri", Font.PLAIN, 12));
textArea1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textArea1MouseClicked(e);
}
});
textArea1.addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent e) {
textArea1FocusGained(e);
}
#Override
public void focusLost(FocusEvent e) {
textArea1FocusLost(e);
}
});
scrollPane1.setViewportView(textArea1);
}
panel1.add(scrollPane1);
scrollPane1.setBounds(195, 25, 125, 90);
//---- button2 ----
button2.setText("Check Names");
button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
button2ActionPerformed(e);
}
});
panel1.add(button2);
button2.setBounds(195, 115, 125, button2.getPreferredSize().height);
}
UsernameChecker.add(panel1);
panel1.setBounds(0, 0, 330, 155);
UsernameChecker.setPreferredSize(new Dimension(330, 155));
}
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
// Generated using JFormDesigner Evaluation license - ryan icknem
private JPanel UsernameChecker;
private JPanel panel1;
private JTextField textField1;
private JPasswordField passwordField1;
private JButton button1;
private JLabel label1;
private JLabel label2;
private JScrollPane scrollPane1;
private JTextArea textArea1;
private JButton button2;
// JFormDesigner - End of variables declaration //GEN-END:variables
}
I just generated the form with jFormdesigner but when I try to include it in my main class like so:
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
usernameForm GUI = new usernameForm();
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.setVisible(true);
}
});
It displays a grey applet in the smallest dimension. I apologize for this noob question but how do I view the applet with the input boxes & buttons? + Make it auto size to the proper dimension?
It looks like initComponents() is never called. You should create a constructor and add the method call:
public usernameForm(){
initComponents();
}
Note: You should also begin your class names with an uppercase letter, i.e. UsernameForm.
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.*;
/*
* Created by JFormDesigner on Wed May 11 16:42:17 PDT 2011
*/
public class UsernameForm extends JFrame {
UsernameForm() {
initComponents();
pack();
}
private void passwordField1KeyPressed(KeyEvent e) {
// TODO add your code here
}
private void button1ActionPerformed(ActionEvent e) {
// TODO add your code here
}
private void textArea1MouseClicked(MouseEvent e) {
// TODO add your code here
}
private void textArea1FocusGained(FocusEvent e) {
// TODO add your code here
}
private void textArea1FocusLost(FocusEvent e) {
// TODO add your code here
}
private void button2ActionPerformed(ActionEvent e) {
// TODO add your code here
}
private void initComponents() {
// JFormDesigner - Component initialization - DO NOT MODIFY //GEN-BEGIN:initComponents
// Generated using JFormDesigner Evaluation license - ryan icknem
usernameChecker = new JPanel();
panel1 = new JPanel();
textField1 = new JTextField();
passwordField1 = new JPasswordField();
button1 = new JButton();
label1 = new JLabel();
label2 = new JLabel();
scrollPane1 = new JScrollPane();
textArea1 = new JTextArea();
button2 = new JButton();
//======== usernameChecker ========
{
// JFormDesigner evaluation mark
usernameChecker.setBorder(new javax.swing.border.CompoundBorder(
new javax.swing.border.TitledBorder(new javax.swing.border.EmptyBorder(0, 0, 0, 0),
"JFormDesigner Evaluation", javax.swing.border.TitledBorder.CENTER,
javax.swing.border.TitledBorder.BOTTOM, new java.awt.Font("Dialog", java.awt.Font.BOLD, 12),
java.awt.Color.red), usernameChecker.getBorder())); usernameChecker.addPropertyChangeListener(new java.beans.PropertyChangeListener(){public void propertyChange(java.beans.PropertyChangeEvent e){if("border".equals(e.getPropertyName()))throw new RuntimeException();}});
usernameChecker.setLayout(null);
//======== panel1 ========
{
panel1.setBorder(new TitledBorder("Username Availability Checker"));
panel1.setLayout(null);
panel1.add(textField1);
textField1.setBounds(75, 25, 115, textField1.getPreferredSize().height);
//---- passwordField1 ----
passwordField1.addKeyListener(new KeyAdapter() {
#Override
public void keyPressed(KeyEvent e) {
passwordField1KeyPressed(e);
}
});
panel1.add(passwordField1);
passwordField1.setBounds(75, 57, 115, passwordField1.getPreferredSize().height);
//---- button1 ----
button1.setText("Login");
button1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
button1ActionPerformed(e);
}
});
panel1.add(button1);
button1.setBounds(75, 89, 115, button1.getPreferredSize().height);
//---- label1 ----
label1.setText("Username");
label1.setLabelFor(textField1);
panel1.add(label1);
label1.setBounds(10, 30, 65, label1.getPreferredSize().height);
//---- label2 ----
label2.setText("Password");
label2.setLabelFor(passwordField1);
panel1.add(label2);
label2.setBounds(15, 65, 60, 16);
//======== scrollPane1 ========
{
//---- textArea1 ----
textArea1.setText("Separate by comma and space");
textArea1.setTabSize(2);
textArea1.setLineWrap(true);
textArea1.setWrapStyleWord(true);
textArea1.setFont(new Font("Calibri", Font.PLAIN, 12));
textArea1.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
textArea1MouseClicked(e);
}
});
textArea1.addFocusListener(new FocusAdapter() {
#Override
public void focusGained(FocusEvent e) {
textArea1FocusGained(e);
}
#Override
public void focusLost(FocusEvent e) {
textArea1FocusLost(e);
}
});
scrollPane1.setViewportView(textArea1);
}
panel1.add(scrollPane1);
scrollPane1.setBounds(195, 25, 125, 90);
//---- button2 ----
button2.setText("Check Names");
button2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
button2ActionPerformed(e);
}
});
panel1.add(button2);
button2.setBounds(195, 115, 125, button2.getPreferredSize().height);
}
usernameChecker.add(panel1);
panel1.setBounds(0, 0, 330, 155);
usernameChecker.setPreferredSize(new Dimension(330, 155));
// Add it to the FRAME!!
this.setContentPane(usernameChecker);
}
// JFormDesigner - End of component initialization //GEN-END:initComponents
}
// JFormDesigner - Variables declaration - DO NOT MODIFY //GEN-BEGIN:variables
// Generated using JFormDesigner Evaluation license - ryan icknem
private JPanel usernameChecker;
private JPanel panel1;
private JTextField textField1;
private JPasswordField passwordField1;
private JButton button1;
private JLabel label1;
private JLabel label2;
private JScrollPane scrollPane1;
private JTextArea textArea1;
private JButton button2;
// JFormDesigner - End of variables declaration //GEN-END:variables
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
UsernameForm GUI = new UsernameForm();
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GUI.setVisible(true);
}
});
}
}
Screen Shot