I added a few JPanels to a JFrame. Want I wanted is the JTextFields to be on the top right, the buttons to be on the bottom and the JFileChooser to be on the top right. Lastly I wanted the JTextArea to be in the center. I will attach a picture of what the result was below. Here is the code:
Database.jpgpackage addItemBtn.Home.DataBase; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.*; public class AddItemView extends JFrame { private static final long serialVersionUID = 1L; JFrame frame; JPanel btnPanel,descriptionPanel,picturePanel,textPanel; JTextField nameBox,priceBox,locationBox; JLabel nameLbl,descriptionLbl,priceLbl,locationLbl,pictureLbl; JTextArea descriptionBox; JButton submitBtn,cancelBtn,previewBtn,browseBtn; JFileChooser fileopen; public AddItemView() { //set up frame frame = new JFrame("Add Item"); frame.setSize(750,500); frame.setLayout(new BorderLayout()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //create text boxes and labels nameLbl = new JLabel("Name:"); nameBox = new JTextField(15); priceLbl = new JLabel("Price:"); priceBox = new JTextField(15); locationLbl = new JLabel("Location:"); locationBox = new JTextField(15); //add text fields to textPanel textPanel = new JPanel(); textPanel.setLayout(new GridLayout(6,4)); textPanel.setSize(100,75); textPanel.add(nameLbl); textPanel.add(nameBox); textPanel.add(priceLbl); textPanel.add(nameBox); textPanel.add(locationLbl); textPanel.add(locationBox); textPanel.setVisible(true); //create file chooser upload a picture fileopen = new JFileChooser(); browseBtn = new JButton("Browse"); //create and add file chooser to panel picturePanel = new JPanel(); picturePanel.add(fileopen); picturePanel.add(browseBtn); //create buttons btnPanel = new JPanel(); btnPanel.setLayout(new FlowLayout()); btnPanel.setSize(getPreferredSize()); submitBtn = new JButton("Submit"); cancelBtn = new JButton("Cancel"); previewBtn = new JButton("Preview"); //add buttons to panel btnPanel.add(submitBtn); btnPanel.add(cancelBtn); btnPanel.add(previewBtn); btnPanel.setVisible(true); //create description text area descriptionLbl = new JLabel("Description:"); descriptionBox = new JTextArea(); //add to description text area to panel descriptionPanel = new JPanel(); descriptionPanel.setLayout(new FlowLayout()); descriptionPanel.setSize(150,100); descriptionPanel.add(descriptionLbl); descriptionPanel.add(descriptionBox); //place textFields to top left of frame frame.add(textPanel,BorderLayout.NORTH); //place picture panel into top right of frame frame.add(picturePanel,BorderLayout.NORTH); //place descriptionPanel to the center of the frame frame.add(descriptionPanel,BorderLayout.CENTER); //place buttons on bottom of frame frame.add(btnPanel,BorderLayout.SOUTH); frame.setVisible(true); } }
The JFileChooser is huge, for some reason nameBox (its a JTextField) is not even displaying. Any advice?