Nao Chan
Nao Chan

Reputation: 15

Connection of JButton and JTextField?

I just want to know how to do a new frame via typing a specific character then if you click the Jbutton, all the information (of that character) will pop up - for example a picture or a text.

For example, if I type the word "Dog", a picture of a dog and the information about it will pop out on a new window. Is this possible without database?

I want to do it without a database if it's possible.

Here's my code:

 import java.awt.FlowLayout;
 import java.awt.BorderLayout;
 import java.awt.Font;

 import java.awt.event.ActionListener;
 import java.awt.event.ActionEvent;

 import javax.swing.JOptionPane;
 import javax.swing.ImageIcon;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
 import javax.swing.JPanel;
 import javax.swing.SwingUtilities;
 import javax.swing.JTextField;
 import javax.swing.JButton;
 import javax.swing.BorderFactory;
 import javax.swing.JOptionPane;

 //Bago
 import java.awt.GridBagLayout; // Para mahatihati yung panel
 import java.awt.GridBagConstraints; // para customize yung pagkahati ng panel

class ProgDraftMain {

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            ProgDraft gui = new ProgDraft();
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setResizable(true);
            gui.pack();

            gui.setVisible(true);
        }
    });
}
}

class ProgDraft extends JFrame {

private ImageIcon image1;
private JLabel label1;
private JTextField textField1;
private JButton butones;
private JTextField textField;

ProgDraft() {


    JPanel mainPanel = new JPanel(new BorderLayout());


    JLabel title = new JLabel("Dog Check", JLabel.CENTER);
    Font font = new Font("Gigi", Font.BOLD, 50);
    title.setFont(font);
    mainPanel.add(title, BorderLayout.NORTH);   


    String text = "Dogs" + "<br>"
            + "Cute dogs are everywhere" + "<br>" + "<br>"
            + "Take care and stay safe!" + "<br>"
            + "I love my dogs" + "<br>" + "<br>" + "<br>"
            + "Please help!";
    JLabel dog = new JLabel("<html><div style=\"text-align: center;\">" + text + "</html>");
    dog.setHorizontalAlignment(JLabel.CENTER);
    mainPanel.add(dog);   

    ImageIcon pics = new ImageIcon(getClass().getResource("Capture.png"));

    JLabel logo = new JLabel(pics);
    logo.setHorizontalAlignment(JLabel.CENTER);
    logo.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0));
    logo.setToolTipText("PIcture.");




    JPanel iconFieldPanel = new JPanel(); 
    iconFieldPanel.setLayout( new GridBagLayout() );


    GridBagConstraints gc = new GridBagConstraints();



    gc.anchor = GridBagConstraints.CENTER; // Saan ikakabit yung component
    gc.weightx = 0.5; // (left and right)Space between sa edge nung panel at component
    gc.weighty = 0.5; // (top and bottom)^ 
    gc.gridx = 0; //saang cell x-axis
    gc.gridy = 0; //^ y axis naman


    iconFieldPanel.add(logo, gc);



    gc.gridy = 1;
    JLabel titleBut = new JLabel("Enter Dog Code:");
    iconFieldPanel.add(titleBut, gc);    


    gc.gridy = 2; 
    textField = new JTextField(10);
    iconFieldPanel.add(textField, gc );


    JButton buton1 = new JButton("OK");           

    gc.gridy = 3;

    iconFieldPanel.add( buton1, gc);





    JPanel iconFieldWrapper = new JPanel();
    iconFieldWrapper.add(iconFieldPanel);



    mainPanel.add(iconFieldWrapper, BorderLayout.SOUTH);  // add icon and field to bottom


    getContentPane().add(mainPanel);



}



}

Upvotes: 0

Views: 779

Answers (3)

Anptk
Anptk

Reputation: 1123

The following code may help you to solve your problem,

import java.awt.Button;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class PopupExample implements ActionListener
{
    JFrame frame;
    JTextField t1;
    JButton btn;
    public PopupExample()
    {
       frame=new JFrame();
       frame.setLayout(null);
       frame.setSize(700,700);
       frame. setLocation(300,10);

       t1=new JTextField();
       t1.setBounds(82,10,100,20);

       frame.add(t1);

       btn=new JButton("SUBMIT");
       btn.setBounds(200,10,100,20);
        btn.addActionListener(this);
       frame.add(btn);
       frame.setVisible(true);
     }

     public static void main(String ar[])
     {
        PopupExample obj=new PopupExample();
     }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==btn)
        {
            String input=t1.getText();
            if(input.equals("dog"))
            {
                JOptionPane.showMessageDialog(null,"my dog");
                //you can popup new frame here about dog
                //create the object of new class (which contain dog details)here.
                //you can use show()
            }
        }
    }
}

Upvotes: 1

Nagarjuna
Nagarjuna

Reputation: 1

Yes, we can do that without database, but you need to write different logic i.e. hard coding like based on the condition

Example: After getting the data from the text field

String a=xx.getText().toString().trim();if(a.equals("dog"))
{
    //call the iframe which u need
}

Note: As you need to store all the images in your working directory, or store all the frames with different names which includes images and information regarding the image and call them according to your requirement.

Upvotes: 0

CharlesX
CharlesX

Reputation: 128

It is possible to write a program without database but you have to think about a way to store the data. For database concern, I guess you mean how to store the code and picture of these dogs.

Upvotes: 1

Related Questions