Results 1 to 2 of 2
  1. #1
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

    Default MigLayout not working

    Is it smart to put the whole GUI layout inside the constructor? I seen an example where someone made a method for each JPanel and just return the JPanel. I am assuming he just called the methods in the constructor and move the methods around in the frame. I have not tried this yet, just wanted an opinion of different designs.

    I am currently using MVC with this and application and putting it all in the constructor. I was using GridBagConstraints then someone mentioned MigLayout() so I downloaded the Jar file and implemented it. The error is below:

    Java Code:
    There was an error java.awt.event.ActionEvent[ACTION_PERFORMED,cmd=Add Item,when=1411410455116,modifiers=Button1] on javax.swing.JButton[,0,29,200x29,alignmentX=0.0,alignmentY=0.5,border=com.apple.laf.AquaButtonBorder$Dynamic@10bc5c9,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=2,bottom=0,right=2],paintBorder=true,paintFocus=true,pressedIcon=,rolloverEnabled=false,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=Add Item,defaultCapable=true]
    Java Code:
    package addItemBtn.Home.DataBase;
     
    import java.awt.FlowLayout;
    import java.awt.GridLayout;
    import java.awt.ScrollPane;
     
    import javax.swing.*;
     
    import net.miginfocom.swing.MigLayout;
     
    public class AddItemView
    {
     
    	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 MigLayout());
    		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 MigLayout());
    		textPanel.setSize(100,75);
     
    		textPanel.add(nameLbl);
    		textPanel.add(nameBox);
    		textPanel.add(priceLbl);
    		textPanel.add(nameBox);
    		textPanel.add(locationLbl);
    		textPanel.add(locationBox,"Wrap");
     
     
    		//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 MigLayout());
    		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,"Wrap");
     
    		//create description text area
    		descriptionLbl = new JLabel("Description:");
     
    		//add to description text area to panel
    		descriptionPanel = new JPanel();
    		descriptionPanel.setSize(150,100);
    		descriptionPanel.add(descriptionLbl);
     
    		//place textFields to top left of frame
    		frame.add(textPanel,"cell 0 1");
    		//place picture panel into top right of frame
    		frame.add(picturePanel,"cell 7,1");
    		//place descriptionPanel to the center of the frame
    		frame.add(descriptionPanel,"cell 5,7");
    		//place buttons on bottom of frame
    		frame.add(btnPanel,"dock north");
     
    		frame.setVisible(true);
    	}
     
    }
    Also posted at :
    Adding Panels to JFrame

  2. #2
    jocdrew21 is offline Senior Member
    Join Date
    Jan 2014
    Posts
    137
    Rep Power
    0

Similar Threads

  1. Replies: 4
    Last Post: 01-21-2014, 12:49 PM
  2. Replies: 10
    Last Post: 11-23-2013, 06:47 PM
  3. MigLayout constraint change
    By Karlovsky120 in forum AWT / Swing
    Replies: 0
    Last Post: 07-06-2013, 02:46 PM
  4. Simple MigLayout Question
    By Aerandir in forum AWT / Swing
    Replies: 5
    Last Post: 12-22-2012, 11:31 PM
  5. Replies: 0
    Last Post: 02-19-2012, 06:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •