Java Code to create a panel with Textbox, Button and File Upload

Below is the code for a GUI box having textboxes, browse button with file chooser for a file upload.

JButton Browse = new JButton("Browse");
JTextField t1,t2,t3;  
t1=new JTextField("",30);  
t2=new JTextField("",30);  
t3=new JTextField("",20);  

String t1_input = "";
String t2_input = "";
String t3_input ="";
final JFrame frame = new JFrame("Nodes Required");
JPanel panel2 = new JPanel(new GridLayout(4,0));
panel2.setBorder(BorderFactory.createTitledBorder("Example of GUI Panel JAVA"));

panel2.add(new JLabel("Enter name"));
panel2.add(t1);
panel2.add(new JLabel("Enter Age"));
panel2.add(t2);
panel2.add(new JLabel("Choose File"));
panel2.add(Browse);
panel2.add(new JLabel("File Selected is : "));
panel2.add(t3);
frame.setAlwaysOnTop(false);

//listener
Browse.addActionListener(new ActionListener() {
	  public void actionPerformed(ActionEvent e) {
		//fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		JFileChooser fileChooser = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		fileChooser.setAcceptAllFileFilterUsed(false);
		int rVal = fileChooser.showOpenDialog(null);
		if (rVal == JFileChooser.APPROVE_OPTION) {
			System.out.println("File Selected : "+fileChooser.getSelectedFile().toString());
			t3.setText(fileChooser.getSelectedFile().toString());
		}
	  }
});

int result11 = JOptionPane.showConfirmDialog(frame, panel2, 
		"Demo", JOptionPane.OK_CANCEL_OPTION);

if (result11 == JOptionPane.OK_OPTION) {

	t1_input = t1.getText().toString();						
	t2_input = t2.getText().toString();
	t3_input = t3.getText().toString();
}
if (result11 == JOptionPane.CANCEL_OPTION || result11 == JOptionPane.CLOSED_OPTION) 
{
	System.out.println("User cancelled the operation, Aborting, Please proceed manually : "+new Timestamp(System.currentTimeMillis()));
}
Output of the Code above
After clicking browse system window appears for choosing file
User inputs (Last textbox contains selected file path)

For any further doubts do post in comment section.

Happy Coding 🙂

By Tobo.

Posted in Uncategorized

5 thoughts on “Java Code to create a panel with Textbox, Button and File Upload

  1. After looking over a number of the blog articles on your blog, I truly like your way of blogging.
    I saved as a favorite it to my bookmark website list and will be checking
    back in the near future. Take a look at my website too and tell me what
    you think.

  2. Wonderful goods from you, man. I have understand
    your stuff previous to and you’re just too fantastic.
    I really like what you’ve acquired here, really like what you’re stating and the way in which you say it.
    You make it entertaining and you still care for to keep it wise.
    I cant wait to read far more from you. This is actually a terrific site.

  3. I’m curious to find out what blog system you have been utilizing?
    I’m having some minor security issues with my latest website and I’d like to find something more safe.

    Do you have any suggestions?

  4. Everything published made a lot of sense. However, think about this, suppose
    you were to create a killer headline? I am not suggesting your content isn’t good, however what if you added a title to possibly grab
    a person’s attention? I mean Java Code to create a panel with Textbox, Button and
    File Upload – Tobo's Point is a little plain. You ought to peek at Yahoo’s front page and watch how they create article headlines to grab people to open the links.
    You might add a video or a picture or two to get readers interested about what you’ve written. Just my opinion,
    it might make your posts a little bit more interesting.

Leave a Reply to t.co Cancel reply

Your email address will not be published.

Verified by MonsterInsights