As I said in the title, I am writing a code for a small program in java. I used Internal Frames to implement it. The thing is I am trying to add a login option to the program. The login class is a jInternalFrame and I want it to Enable/Disable specific buttons in my main window. I tried google-ing a solution but didn't seem to find it. (Keep in mind that I'm really new to programing so I expect there might be other problems in my code I don't see at the moment).
Main Window code:
import javax.swing.JDesktopPane;
public class FereastraPrincipala extends JFrame {
private static final long serialVersionUID = 1L;
JDesktopPane jdpDesktop;
static int openFrameCount = 0;
public FereastraPrincipala() {
super("Fereastra Principala");
// Make the main window positioned as 50 pixels from each edge of the
// screen.
int inset = 50;
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setBounds(inset, inset, screenSize.width - inset * 2,screenSize.height - inset * 2);
// Add a Window Exit Listener
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
// Create and Set up the GUI.
jdpDesktop = new JDesktopPane();
// A specialized layered pane to be used with JInternalFrames
setContentPane(jdpDesktop);
setJMenuBar(createMenuBar());
// Make dragging faster by setting drag mode to Outline
jdpDesktop.putClientProperty("JDesktopPane.dragMode", "outline");
}
private JMenuBar createMenuBar() {
JMenuBar menuBar = new JMenuBar();
JMenu mnFile = new JMenu("File");
mnFile.setMnemonic(KeyEvent.VK_F);
JMenuItem mntmLogin = new JMenuItem("Login");
mnFile.add(mntmLogin);
mntmLogin.setMnemonic(KeyEvent.VK_N);
menuBar.add(mnFile);
mntmLogin.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
login();
}
});
JMenuItem mntmLogout = new JMenuItem("Logout");
mnFile.add(mntmLogout);
mntmLogout.setEnabled(true);
JMenuItem mntmCreazaUser = new JMenuItem("Creaza User");
mntmCreazaUser.setMnemonic(KeyEvent.VK_C);
mnFile.add(mntmCreazaUser);
mntmCreazaUser.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
creazaUser();
}
});
JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.setMnemonic(KeyEvent.VK_X);
mnFile.add(mntmExit);
mntmExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
JMenu mnAdministrator = new JMenu("Administrator");
mnAdministrator.setMnemonic(KeyEvent.VK_N);
menuBar.add(mnAdministrator);
mnAdministrator.setEnabled(true);
JMenu mnPresedinte = new JMenu("Presedinte");
mnPresedinte.setMnemonic(KeyEvent.VK_P);
menuBar.add(mnPresedinte);
mnPresedinte.setEnabled(true);
JMenuItem mnHelp = new JMenuItem("Help");
mnHelp.setMnemonic(KeyEvent.VK_H);
menuBar.add(mnHelp);
mnHelp.setEnabled(true);
mnHelp.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
help();
}
});
return menuBar;
}
protected void login() {
Login frame = new Login();
frame.setVisible(true);
// Every JInternalFrame must be added to content pane using JDesktopPane
jdpDesktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
protected void help() {
Help frame1 = new Help();
frame1.setVisible(true);
jdpDesktop.add(frame1);
try {
frame1.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
protected void creazaUser() {
CreazaUser frame = new CreazaUser();
frame.setVisible(true);
jdpDesktop.add(frame);
try {
frame.setSelected(true);
} catch (java.beans.PropertyVetoException e) {
}
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {
FereastraPrincipala frame = new FereastraPrincipala();
frame.setVisible(true);
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
}
Login class code:
import java.awt.EventQueue;
public class Login extends JInternalFrame {
private static Login instance = null;
private JTextField textField;
private JPasswordField passwordField;
public static Login getInstance(){
if(instance == null) {instance = new Login(); }
return instance;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Login frame = new Login();
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Login() {
super("Login", false, true, false, false);
setVisible(true);
setBounds(200, 200, 290, 173);
JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);
panel.setLayout(null);
JLabel lblNewLabel = new JLabel("Username:");
lblNewLabel.setBounds(22, 29, 79, 14);
panel.add(lblNewLabel);
JLabel label = new JLabel("Parola:");
label.setBounds(22, 54, 79, 14);
panel.add(label);
textField = new JTextField();
textField.setColumns(10);
textField.setBounds(123, 26, 98, 20);
panel.add(textField);
passwordField = new JPasswordField();
passwordField.setBounds(123, 51, 98, 20);
panel.add(passwordField);
JButton button = new JButton("Autentificare");
button.setBounds(80, 97, 112, 23);
panel.add(button);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String user = textField.getText();
String pass = passwordField.getText();
if(user.equals("admin") && pass.equals("admin")) {
FereastraPrincipala(mntmLogout.setEnabled(true));
FereastraPrincipala(mnAdministrator.setEnabled(true));
JOptionPane.showMessageDialog(null,"Login esuat","Login esuat",JOptionPane.ERROR_MESSAGE);
} else if (user.equals("prez") && pass.equals("prez")) {
/* FereastraPrincipala(mntmLogout.setEnabled(true)):
FereastraPrincipala(mnPresedinte.setEnabled(true)); */
JOptionPane.showMessageDialog(null,"Login esuat","Login esuat",JOptionPane.ERROR_MESSAGE);
} else { JOptionPane.showMessageDialog(null,"Login esuat","Login esuat",JOptionPane.ERROR_MESSAGE);}
}
});
}
}
Related
I display everything raw off the frame. But if I create a JPanel) and add the contents to that, then add the panel to the frame using frame.add(pane, BorderLayout.CENTER); but its displayed differently and isnt put properly.
For example normally raw everything is displayed properly. On a pane Text isn't displayed properly from a JTextPane, the start is cut off and it doesn't take up the whole window.
And when I add a JTextBox to the mainPane, it doesn't take the whole width of the window and is put at the half way line when i specifically put BorderLayout.SOUTH, also the text box looks like this [] and you can't type in it.
Here is code for start:
public class ConsoleApp {
public static JFrame frame;
public static JTextPane consoleOutput;
public static JTextField consoleInput;
public JScrollPane scroll;
public static JTabbedPane tabs;
public static JPanel mainPane;
public static JPanel backupPane;
public static InputStream commandInput;
public static StyledDocument doc;
public static boolean trace = false;
public boolean debug = false;
public Font consoleFont = new Font("Courier New", Font.PLAIN, 12);
//public Color consoleColour = new Color(50, 50, 50);
public Color consoleColour = new Color(255, 255, 255);
//public Logger internalConsole;
public void start()
{
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch(Exception e)
{
}
initializeFrame();
}
public void initializeFrame()
{
frame = new JFrame();
frame.setTitle("sebagius7110's Minecraft Server Wrapper");
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
consoleOutput = new JTextPane();
consoleOutput.setEditable(false);
consoleOutput.setFont(consoleFont);
consoleOutput.setBorder(null);
consoleOutput.setOpaque(false);
doc = consoleOutput.getStyledDocument();
consoleInput = new JTextField();
consoleInput.setFont(consoleFont);
//consoleInput.setBorder(null);
consoleInput.setOpaque(false);
consoleInput.setEditable(true);
Property.readProperties();
consoleInput.setCaret(new ConsoleCaret());
consoleInput.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent e)
{
String text = consoleInput.getText();
if(text.length() >= 1)
{
if(text.startsWith("."))
{
onCommand(text);
} else {
try{
InputStream is = new ByteArrayInputStream(consoleInput.getText().getBytes(Charset.forName("UTF-8")));
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String input;
//input = br.readLine();
while(Util.ServerRunning && (input=br.readLine())!=null){
Stream.PutString(input);
}
br.close();
}catch(IOException io){
io.printStackTrace();
}
}
consoleInput.selectAll();
}
}
});
consoleInput.addKeyListener(new KeyListener()
{
#Override
public void keyPressed(KeyEvent arg0) {}
#Override
public void keyReleased(KeyEvent arg0) {}
#Override
public void keyTyped(KeyEvent arg0) {}
});
scroll = new JScrollPane(consoleOutput);
scroll.setBorder(null);
scroll.setOpaque(false);
scroll.getViewport().setOpaque(false);
scroll.setBackground(GetPropValues.cbcolor);
tabs = new JTabbedPane();
mainPane = new JPanel();
backupPane = new JPanel();
//frame.setUndecorated(true);
tabs.addTab("Main", mainPane);
tabs.addTab("Backup", backupPane);
mainPane.setBorder(null);
mainPane.setOpaque(false);
mainPane.add(scroll, BorderLayout.CENTER);
mainPane.add(consoleInput, BorderLayout.SOUTH);
frame.setContentPane(mainPane);
//frame.add(scroll, BorderLayout.CENTER);
//frame.add(consoleInput, BorderLayout.SOUTH);
frame.setSize(660, 350);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
}
That isn't the whole class, just some of it, so the brackets are not giving errors.
To set window size with position for any swing component use :
eg. compVariable.setBounds(x,y,width,height);
My program is written in Java using Eclipse and what my program does is first you have to login your username and password to the server and once that is verified, then you go to a new page (frame) where I have a JMenuBar, JMenu, and JMenuItem. The JMenuItem that I am working on is called "Logout" under the JMenu Exit, when you click on logout at the moment, it closes the screen. What I want to happen is to redirect me back to the login page. I've tried many functions, but none of them worked. I would appreciate the help.
Here is my program:
public class ScanTest2 extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
private static LoginDialog loginDialog;
public ScanTest2() {
loginDialog = new LoginDialog(this, true);
loginDialog.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
// create new frame for new page
JFrame frame = new ScanTest2();
//create a Menu bar
JMenuBar menuBar = new JMenuBar();
frame.getContentPane().setBackground(Color.BLUE);
try {
frame.setContentPane(new JLabel(new ImageIcon(ImageIO.read(new File("/home/a002384/logo2.bmp")))));
} catch (IOException e) {
e.printStackTrace();
}
frame.setTitle("Scan Gun Information");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
// create Menus
JMenu databaseMenu = new JMenu("Database");
JMenu ticketMenu = new JMenu("Tickets");
JMenu inquiriesMenu = new JMenu("Inquiries");
JMenu reportsMenu = new JMenu("Reports");
JMenu exitMenu = new JMenu("Exit");
// create Menu Items
JMenuItem lotMenuItem = new JMenuItem("Lots");
lotMenuItem.setMnemonic(KeyEvent.VK_L);
lotMenuItem.setActionCommand("Lots");
JMenuItem gunMenuItem = new JMenuItem("Guns");
gunMenuItem.setMnemonic(KeyEvent.VK_G);
gunMenuItem.setActionCommand("Guns");
JMenuItem batteryMenuItem = new JMenuItem("Batteries");
batteryMenuItem.setMnemonic(KeyEvent.VK_B);
batteryMenuItem.setActionCommand("Batteries");
JMenuItem logoutMenuItem = new JMenuItem("Logout");
logoutMenuItem.setMnemonic(KeyEvent.VK_L);
logoutMenuItem.setActionCommand("Logout");
MenuItemListener menuItemListener = new MenuItemListener();
lotMenuItem.addActionListener(menuItemListener);
gunMenuItem.addActionListener(menuItemListener);
batteryMenuItem.addActionListener(menuItemListener);
logoutMenuItem.addActionListener(menuItemListener);
// add Menu Items to Menus
databaseMenu.add(lotMenuItem);
databaseMenu.add(gunMenuItem);
databaseMenu.add(batteryMenuItem);
exitMenu.add(logoutMenuItem);
// add menu to MenuBar
menuBar.add(databaseMenu);
menuBar.add(ticketMenu);
menuBar.add(inquiriesMenu);
menuBar.add(reportsMenu);
menuBar.add(exitMenu);
frame.setJMenuBar(menuBar);
frame.setVisible(true);
} // end of run method
class MenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("Logout"))
System.exit(0);
// go back to login screen
}
}
}); // end of Runnable
}// end of Main
}
class LoginDialog extends JDialog {
/**
*
*/
private static final long serialVersionUID = 1L;
private final JLabel loginlbl = new JLabel("Username");
private final JLabel Passwordlbl = new JLabel("Password");
private final JTextField loginName = new JTextField(15);
private final JPasswordField loginPassword = new JPasswordField();
private final JButton loginButton = new JButton("Login");
private final JButton cancelButton = new JButton("Cancel");
private final JLabel Status = new JLabel(" ");
private String username;
private String password;
private int m_errCounter = 0;
public static final int MAX_LOGIN_ATTEMPTS = 3;
public LoginDialog() {
this(null, true);
}
public LoginDialog(final JFrame parent, boolean modal) {
super(parent, modal);
JPanel p3 = new JPanel(new GridLayout(2, 1));
p3.add(loginlbl);
p3.add(Passwordlbl);
JPanel p4 = new JPanel(new GridLayout(2, 1));
p4.add(loginName);
p4.add(loginPassword);
JPanel p1 = new JPanel();
p1.add(p3);
p1.add(p4);
JPanel p2 = new JPanel();
p2.add(loginButton);
p2.add(cancelButton);
JPanel p5 = new JPanel(new BorderLayout());
p5.add(p2, BorderLayout.CENTER);
p5.add(Status, BorderLayout.NORTH);
Status.setForeground(Color.RED);
Status.setHorizontalAlignment(SwingConstants.CENTER);
setLayout(new BorderLayout());
add(p1, BorderLayout.CENTER);
add(p5, BorderLayout.SOUTH);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
loginPassword.enableInputMethods(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
loginButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
UniJava uJava = new UniJava();
try {
username = loginName.getText();
password = new String (loginPassword.getPassword());
if (username == " " && password == " ")
{
Status.setText("Invalid username or password!");
}
else
{
UniSession session = uJava.openSession();
session.setHostName("docdbtst.starcalif.com");
session.setUserName(username);
session.setPassword(password);
session.setAccountPath("/mnt/data1/DD");
session.connect();
parent.setVisible(true);
setVisible(false);
}
} catch (UniSessionException e1) {
if (++m_errCounter > MAX_LOGIN_ATTEMPTS)
{
JOptionPane.showMessageDialog(LoginDialog.this,
"All Login attempts failed!",
"Error", JOptionPane.ERROR_MESSAGE);
System.exit(1);
}
else
{
Status.setText("Invalid Login! Cannot Connect!");
loginName.setText("");
loginPassword.setText("");
}
e1.printStackTrace();
}
}
});
cancelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
setVisible(false);
parent.dispose();
System.exit(0);
}
});
}
}
System.exit() everything will be cleaned up
JFrame.dispose() vs System.exit()
logoutItem.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
f.dispose();
// northInformation.removeAll();
// init();
LoginWindow login = new LoginWindow();
}
});
I have a home page with title and a few buttons I cannot get a new window to open when i click on the button. Here is the code i have for the home page aswell as the class with next screen i am attempting to open trimed for what seems relevant. The NewTicketWindow class is also attached it is plain at the moment. Any help is appreciated.
public class Home
{
private JFrame frame;
JInternalFrame internalFrame;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
Home window = new Home();
window.frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Home()
{
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize()
{
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel title1 = new JLabel("City of Murphy");
JLabel title2 = new JLabel("Traffic Ticket Input System");
JButton newTicketButton = new JButton("New Ticket");
newTicketButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
}
});
JButton payTicketButton = new JButton("Make a Payment");
JButton reportButtton = new JButton("Ticket Report");
JButton exitButton = new JButton("Exit");
GroupLayout groupLayout = new GroupLayout(frame.getContentPane());
}
second class (the screen i want to open upon newticket button being pressed
public class NewTicketWindow extends JFrame
{
private JPanel contentPane;
/**
* Launch the application.
*/
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
NewTicketWindow frame = new NewTicketWindow();
frame.setVisible(true);
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public NewTicketWindow()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
JLabel lblEnterNewTicket = new JLabel("Enter New Ticket Information");
GroupLayout gl_contentPane = new GroupLayout(contentPane);
}
just add these lines into your action performed code -
NewTicketWindow frame = new NewTicketWindow();
frame.setVisible(true);
The ActionListener of newTicketButton should create the new frame by calling the constructor of NewTicketWindow (same thing you are doing in the main of NewTicketWindow):
JButton newTicketButton = new JButton("New Ticket");
newTicketButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
NewTicketWindow newTicketWindow = new NewTicketWindow();
newTicketWindow.setVisible(true);
}
catch (Exception ex)
{
ex.printStackTrace();
}
}
});
Also you need to add the newTicketButton to the home window:
frame.add(newTicketButton);
I've got this strange thing, I'm trying to add to the ArrayList but it does not add, it get the values and everything. Please check the code and brigthen me up.
The class where I am trying to add:
public class ManualProductGUI extends JDialog {
private final JPanel contentPanel = new JPanel();
private JTextField barcodeField;
private JTextField idField;
private JTextField nameField;
private JTextField priceField;
private JTextField quantityField;
private JTextField infoField;
BasketContainer bc = new BasketContainer();
private static final long serialVersionUID = 1L;
/**
* Launch the application.
*/
public static void main(String[] args) {
try {
ManualProductGUI dialog = new ManualProductGUI();
dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Create the dialog.
*/
public ManualProductGUI() {
setTitle("Product search");
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);
barcodeField = new JTextField();
barcodeField.setText("Enter barcode");
barcodeField.setBounds(10, 11, 157, 20);
contentPanel.add(barcodeField);
barcodeField.setColumns(10);
barcodeField.addMouseListener(new MouseAdapter()
{
public void mouseClicked(MouseEvent e)
{
barcodeField.setText("");
}
});
barcodeField.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
int barcode = Integer.parseInt(barcodeField.getText());
ProductCtr prodCtr = new ProductCtr();
Product prod = prodCtr.searchProductByBarcode(barcode);
buildFields(prod);
}
});
infoField = new JTextField();
infoField.setEditable(false);
infoField.setBounds(177, 133, 86, 20);
contentPanel.add(infoField);
infoField.setColumns(10);
{
JPanel buttonPane = new JPanel();
buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
getContentPane().add(buttonPane, BorderLayout.SOUTH);
{
JButton okButton = new JButton("Add");
okButton.setActionCommand("Add");
buttonPane.add(okButton);
okButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
addToBasket();
setVisible(false);
dispose();
}
});
getRootPane().setDefaultButton(okButton);
}
{
JButton cancelButton = new JButton("Cancel");
cancelButton.setActionCommand("Cancel");
cancelButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
setVisible(false);
dispose();
}
});
buttonPane.add(cancelButton);
}
}
}
private void addToBasket()
{
int barcode = Integer.parseInt(barcodeField.getText());
ProductCtr prodCtr = new ProductCtr();
Product prod = prodCtr.searchProductByBarcode(barcode);
bc.addProduct(prod);
}
}
And here is a part of the Container class:
private ArrayList<Product> listOfItems;
public BasketContainer()
{
listOfItems = new ArrayList<>();
}
public void addProduct(Product prod)
{
listOfItems.add(prod);
}
public ArrayList<Product> getProducts()
{
return listOfItems;
}
It prints out the info on the screen, but it does not add. Am I missing something?
Thank you.
You create a new instance of BasketContainer every time, so that new instances will have a new clear ArrayList.
To fix it you should create your BasketContainer instance somewhere (maybe in an init block?) and save the reference, then use it to add the elements.
You're creating a new BasketContainer within the scope of the method.
BasketContainer bc = new BasketContainer();
bc.addProduct(prod);
The addToBasket method should call addProduct on a field of the class it's in.
Trying to develop a GUI, but I've hit a snag:
I am using a submit button, which will look at a txtEnter field. If the user types "yes" in the txtEnter field and clicks submit, it will execute a shell script. If the user types "no" there will be no action. I know the command to run shell script is Runtime.getRuntime().exec(myShellScript);
How I can use an if-else statement in the SubmitListner to check for the user's input?
import javax.swing.*; import javax.swing.event.DocumentListener;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class Executer private JLabel lblCommand;
private JTextField txtEnter;
private JButton btNext, btPrevious, btSubmit;
private JPanel panel;
public static void main(String[] args) {
new Executer();
}
public Executer() {
JFrame frame = new JFrame("Script Executer");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,300);
frame.setVisible(true);
myPanel();
Text();
Fields();
Buttons();
frame.add(panel);
frame.setVisible(true);
}
public void myPanel() {
panel = new JPanel();
panel.setLayout(null);
}
public void Text(){
lblCommand = new JLabel("Enter Here");
lblCommand.setBounds(145, 100, 150, 20);
Font styleOne = new Font("Arial", Font.BOLD, 13);
lblCommand.setFont(styleOne);
panel.add(lblCommand);
}
public void Fields () {
txtEnter = new JTextField();
txtEnter.setBounds(230, 100, 120, 20);
panel.add(txtEnter);
}
public void Buttons() {
btNext = new JButton ("Next");
btNext.setBounds(300,215,100,20);
panel.add(btNext);
btPrevious = new JButton ("Previous");
btPrevious.setBounds(190,215,100,20);
panel.add(btPrevious);
btSubmit = new JButton("Submit");
btSubmit.setBounds(80,215,100,20);
panel.add(btSubmit);
}
class SubmitListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
}}}
You have to assign your Actionlistener to your button:
btSubmit = new JButton();
btSubmit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
// here the click happend so you can check your Textfield
String userEntered = txtEnter.getText();
if(userEntered.equalsIgnoreCase("yes"))
{
//run your script
}
}
});