Link to home
Start Free TrialLog in
Avatar of rachelee
racheleeFlag for Germany

asked on

Java Swing Layout

Hello there,

Will you please let me know how shall I add one mor panel to  the same jframe which would look like the same as one I have already, only the name will be diffrent..lets say Suppliers instead of Search.
Provide one button and a label to it!thats all I would like to do.How can I add it  to my frame?
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
 
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;
 
public class SomeTool extends JFrame{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
		public SomeTool(){
			
			super ("Some Tool");
			newPanel panel1 = new newPanel();
			add(panel1);
			pack();
			setVisible(true);
			setSize(600, 550);
	
		}
		
		
		static class newPanel extends JPanel{
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
 
			public newPanel(){
				
			
				JPanel pan1 = new JPanel();
				setLayout(new BorderLayout());
				
				
				pan1.setLayout(new GridBagLayout());
				GridBagConstraints gBC = new GridBagConstraints();
				
				JLabel jLb1= new JLabel("Some Number 1:");
				
				gBC.gridx=0;
				gBC.gridy=0;
				pan1.add(jLb1, gBC);
				
				JTextField jTf1 = new JTextField(10);
				gBC.ipady = 5;
				gBC.weightx = 0.0;
		        gBC.gridwidth = 1;
				gBC.gridx=1;
				gBC.gridy=0;
				pan1.add(jTf1, gBC);
				
				JLabel jLb2= new JLabel("Some number 2:");
				
				gBC.gridx=0;
				gBC.gridy=1;
				pan1.add(jLb2, gBC);
				
				JTextField jTf2 = new JTextField(10);
				gBC.ipady = 5;
				gBC.weightx = 0.0;
		        gBC.gridwidth = 1;
				gBC.gridx=1;
				gBC.gridy=1;
				pan1.add(jTf2, gBC);
				
				JButton jBn1 = new JButton("Search");
				gBC.ipady = 1;
				gBC.weightx = 0.0;
		        gBC.gridwidth = 1;
				gBC.gridx=4;
				gBC.gridy=0;
				pan1.add(jBn1, gBC);
				
				JLabel jLb3= new JLabel("The Text 1 :");
				
				gBC.gridx=5;
				gBC.gridy=0;
				pan1.add(jLb3, gBC);
				
				JComboBox jCb3 = new JComboBox();
				jCb3.setEditable(true);
				gBC.ipady = 1;
				gBC.weightx = 0.0;
		        gBC.gridwidth = 1;
				gBC.gridx=6;
				gBC.gridy=0;
				pan1.add(jCb3, gBC);
				
				JLabel jLb4= new JLabel("The Text 2 :");
				gBC.gridx=5;
				gBC.gridy=1;
				pan1.add(jLb4, gBC);
				
				JTextField jTf4 = new JTextField(10);
				gBC.ipady = 5;
				gBC.weightx = 0.0;
		        gBC.gridwidth = 4;
				gBC.gridx=6;
				gBC.gridy=1;
				pan1.add(jTf4, gBC);
 
		        pan1.setBorder(
		                BorderFactory.createCompoundBorder(
		                                BorderFactory.createTitledBorder("Search"),
		                                BorderFactory.createEmptyBorder(5,5,5,5)));
		        //New Pane called Text pane
		        
		        JTextArea textPane = new JTextArea(5, 10);
		        JScrollPane paneScrollPane = new JScrollPane(textPane);
		        paneScrollPane.setVerticalScrollBarPolicy(
		                        JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		        paneScrollPane.setPreferredSize(new Dimension(250, 155));
		        paneScrollPane.setMinimumSize(new Dimension(10, 10));
 
		       
		        textPane.setBorder(BorderFactory.createCompoundBorder(
		                        BorderFactory.createTitledBorder("Lieferanten auswählen"),
		                        BorderFactory.createEmptyBorder(5,5,5,5)));
 
		       
 
				JPanel wrapper = new JPanel(new BorderLayout());
			    wrapper.add(pan1, BorderLayout.WEST);
	            this.add(wrapper, BorderLayout.NORTH);
 
			}
		 }
 
		public static void main(String[] args){
		new SomeTool();
		
	}
 
}

Open in new window

Avatar of Mick Barry
Mick Barry
Flag of Australia image

be something like this:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledDocument;

public class SomeTool extends JFrame {

      /**
       *
       */
      private static final long serialVersionUID = 1L;

      public SomeTool() {

            super("Some Tool");
            getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
            newPanel panel1 = new newPanel("Search");
            getContentPane().add(panel1);
            newPanel panel2 = new newPanel("Suppliers");
            getContentPane().add(panel2);
            pack();
            setVisible(true);
            setSize(600, 550);

      }

      static class newPanel extends JPanel {

            /**
             *
             */
            private static final long serialVersionUID = 1L;

            public newPanel(String title) {

                  JPanel pan1 = new JPanel();
                  setLayout(new BorderLayout());

                  pan1.setLayout(new GridBagLayout());
                  GridBagConstraints gBC = new GridBagConstraints();

                  JLabel jLb1 = new JLabel("Some Number 1:");

                  gBC.gridx = 0;
                  gBC.gridy = 0;
                  pan1.add(jLb1, gBC);

                  JTextField jTf1 = new JTextField(10);
                  gBC.ipady = 5;
                  gBC.weightx = 0.0;
                  gBC.gridwidth = 1;
                  gBC.gridx = 1;
                  gBC.gridy = 0;
                  pan1.add(jTf1, gBC);

                  JLabel jLb2 = new JLabel("Some number 2:");

                  gBC.gridx = 0;
                  gBC.gridy = 1;
                  pan1.add(jLb2, gBC);

                  JTextField jTf2 = new JTextField(10);
                  gBC.ipady = 5;
                  gBC.weightx = 0.0;
                  gBC.gridwidth = 1;
                  gBC.gridx = 1;
                  gBC.gridy = 1;
                  pan1.add(jTf2, gBC);

                  JButton jBn1 = new JButton("Search");
                  gBC.ipady = 1;
                  gBC.weightx = 0.0;
                  gBC.gridwidth = 1;
                  gBC.gridx = 4;
                  gBC.gridy = 0;
                  pan1.add(jBn1, gBC);

                  JLabel jLb3 = new JLabel("The Text 1 :");

                  gBC.gridx = 5;
                  gBC.gridy = 0;
                  pan1.add(jLb3, gBC);

                  JComboBox jCb3 = new JComboBox();
                  jCb3.setEditable(true);
                  gBC.ipady = 1;
                  gBC.weightx = 0.0;
                  gBC.gridwidth = 1;
                  gBC.gridx = 6;
                  gBC.gridy = 0;
                  pan1.add(jCb3, gBC);

                  JLabel jLb4 = new JLabel("The Text 2 :");
                  gBC.gridx = 5;
                  gBC.gridy = 1;
                  pan1.add(jLb4, gBC);

                  JTextField jTf4 = new JTextField(10);
                  gBC.ipady = 5;
                  gBC.weightx = 0.0;
                  gBC.gridwidth = 4;
                  gBC.gridx = 6;
                  gBC.gridy = 1;
                  pan1.add(jTf4, gBC);

                  pan1.setBorder(BorderFactory.createCompoundBorder(BorderFactory
                              .createTitledBorder(title), BorderFactory
                              .createEmptyBorder(5, 5, 5, 5)));
                  // New Pane called Text pane

                  JTextArea textPane = new JTextArea(5, 10);
                  JScrollPane paneScrollPane = new JScrollPane(textPane);
                  paneScrollPane
                              .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
                  paneScrollPane.setPreferredSize(new Dimension(250, 155));
                  paneScrollPane.setMinimumSize(new Dimension(10, 10));

                  textPane.setBorder(BorderFactory.createCompoundBorder(BorderFactory
                              .createTitledBorder("Lieferanten auswählen"), BorderFactory
                              .createEmptyBorder(5, 5, 5, 5)));

                  JPanel wrapper = new JPanel(new BorderLayout());
                  wrapper.add(pan1, BorderLayout.WEST);
                  this.add(wrapper, BorderLayout.NORTH);

            }
      }

      public static void main(String[] args) {
            new SomeTool();

      }

}
Avatar of rachelee

ASKER

No it does not work Objects...
Goes blank..console shows nothing
works fine here. how are you running it?

Dear Objects,

Jsut check it, I cud do it.

Will you please tell me how shall I take Label 5  and the textfield to the (x, y) = (00) and (1,0) respt. at start of the page ??Exactly down to the start of Big text area I have appended to th epanel 2.
gives lots of small problemsssss

The End button shud also be exactly down to this Text area.


thnx in advance:-)


package frameCreate;
 
 
 
 
 
	import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
 
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
 
	
 
		public class DeleteIt extends JPanel{
			
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;
 
			 public DeleteIt(){
				
						setLayout(new BorderLayout());
						
						// Panel 1
						JPanel textControlsPane = new JPanel();
						textControlsPane.setLayout(new GridBagLayout());
						GridBagConstraints gBC = new GridBagConstraints();
						
						
						JLabel jLb1= new JLabel("Some Number 1:");
						gBC.gridx=0;
						gBC.gridy=0;
						gBC.insets = new Insets(0,10,0,10);  //top padding
						textControlsPane.add(jLb1, gBC);
						
						JTextField jTf1 = new JTextField(10);
						gBC.gridx=1;
						gBC.gridy=0;
						gBC.ipady=5;
						gBC.ipadx=5;
						gBC.insets = new Insets(0,0,0,0);  //top padding
						textControlsPane.add(jTf1, gBC);
						
						
						JLabel jLb2= new JLabel("Some number 2:");
						gBC.gridx=0;
						gBC.gridy=1;
						gBC.insets = new Insets(0,10,0,10);  //top padding
						textControlsPane.add(jLb2, gBC);
						
						JTextField jTf2 = new JTextField(10);
						jTf2.setEditable(false);
						gBC.gridx=1;
						gBC.gridy=1;
						gBC.ipady=3;
						gBC.ipadx=3;
						gBC.weightx=0.1;
						gBC.insets = new Insets(0,0,0,0);  //top padding
						textControlsPane.add(jTf2, gBC);
						
						
						JButton jBn1 = new JButton("Search");
						gBC.gridx=3;
						gBC.gridy=0;
						gBC.insets = new Insets(0,0,5,60);  //top padding
						textControlsPane.add(jBn1, gBC);
						
						
						JLabel jLb3= new JLabel("The Text 1 :");
						gBC.gridx=6;
						gBC.gridy=0;
						gBC.weightx=0.0;
						gBC.ipady=0;
						gBC.ipadx=0;
						gBC.insets = new Insets(0,10,0,10);  //top padding
						textControlsPane.add(jLb3, gBC);
						
						JComboBox jCb3 = new JComboBox();
						jCb3.setEditable(true);
						gBC.gridx=7;
						gBC.gridy=0;
						gBC.weightx=0.1;
						gBC.insets = new Insets(0,0,0,0);  //top padding
						textControlsPane.add(jCb3, gBC);
						
						
						JLabel jLb4= new JLabel("The Text 2 :");
						gBC.gridx=6;
						gBC.gridy=1;
						gBC.insets = new Insets(0,10,0,10);  //top padding
						textControlsPane.add(jLb4, gBC);
						
						JTextField jTf4 = new JTextField(11);
						jTf4.setEditable(false);
						gBC.gridx=7;
						gBC.gridy=1;
						gBC.ipady=3;
						gBC.ipadx=3;
						gBC.insets = new Insets(0,0,0,0);  //top padding
						textControlsPane.add(jTf4, gBC);
						
						// Outer border named with "Search"
				        textControlsPane.setBorder(
				                BorderFactory.createCompoundBorder(
				                                BorderFactory.createTitledBorder("Search"),
				                                BorderFactory.createEmptyBorder(5,5,5,5)));
		        
			            //Panel 2
			            JPanel liefAus = new JPanel();
			            liefAus.setLayout(new GridBagLayout());
			            GridBagConstraints gbC = new GridBagConstraints();
			            
			            JScrollPane jTa1 = new JScrollPane(new JTextArea(15, 50));  
			            
						gbC.gridx=0;
						gbC.gridy=0;
						
						liefAus.add(jTa1, gbC);
						
						
						JLabel jLb5 = new JLabel("Label5");
						gbC.gridx=0;
						gbC.gridy=4;
						gbC.weightx=0.9;
						gbC.insets = new Insets(10,10,0,0);  //top padding
						liefAus.add(jLb5, gbC);
						
						JTextField jTa2 = new JTextField(15);
						jTa2.setEditable(false);
						gbC.gridx=1;
						gbC.gridy=4;
						gbC.weightx=0.0;
						gbC.insets = new Insets(10,10,0,0);  //top padding
						liefAus.add(jTa2, gbC);
 
 
						// Outer border named with "Label 5"
			            liefAus.setBorder(BorderFactory.createCompoundBorder(
			                            BorderFactory.createTitledBorder("Label 5"),
			                            BorderFactory.createEmptyBorder(5,5,5,5)));
		 
			            
			            //Panel 3
			            JPanel exit = new JPanel(new GridBagLayout());
			            exit.setLayout(new GridBagLayout());
			            GridBagConstraints Gbc = new GridBagConstraints();
			            
			            JButton jBn2 = new JButton("The End");
			            Gbc.gridx=0;
						Gbc.gridy=0;
						Gbc.weightx=1.0;
						Gbc.insets = new Insets(50, 50, 10 , 10);  //top padding
						exit.add(jBn2, Gbc);
						
						JPanel exit2 = new JPanel(new GridBagLayout());
			            exit2.setLayout(new GridBagLayout());
			            GridBagConstraints Gbc1 = new GridBagConstraints();
						JButton jBn3 = new JButton("Edit");	
				        Gbc1.gridx=1;
				        Gbc1.gridy=0;  
						Gbc1.insets = new Insets(50, 50, 10 , 10);  //top padding	
						exit2.add(jBn3, Gbc1); 
			
						// Get all 3 panels together
				       
					    JPanel myPanel = new JPanel(new BorderLayout());
					    
					    myPanel.add(textControlsPane, BorderLayout.LINE_START);
					    this.add(textControlsPane, BorderLayout.NORTH);
					    
					    myPanel.add(liefAus, BorderLayout.LINE_START);
					    this.add(liefAus, BorderLayout.CENTER);
					    
					    myPanel.add(exit, BorderLayout.LINE_START);   
				        myPanel.add(exit2, BorderLayout.LINE_END);   
					    this.add(myPanel, BorderLayout.SOUTH);
					    
					    
					}
			 	
			 // Executes here (Main method)
				public static void main(String[] args){
					 
						JFrame frame = new JFrame("TextSamplerDemo");
				        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 
				        //Add content to the window.
				        frame.add(new DeleteIt());
 
				        //Display the window.
				        frame.pack();
				        frame.setVisible(true);
				        frame.setSize(800, 650)	;
 
				}
			
		}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Worked Bingooooo:-))))))))))))

Would u please care to answer  a little problem I still face:-(

How can one fit outer border to the window size or lets say frame size.

Please use panel 2 as an eg.

 // Outer border named with "Label 5"
            liefAus.setBorder(BorderFactory.createCompoundBorder(BorderFactory
                        .createTitledBorder("Label 5"), BorderFactory
                        .createEmptyBorder(5, 5, 5, 5)));


thnxxx:


sorry I don't entirely follow. where do u want the border?

Border to Panel2  where I have a text area , a label, and one text field all together....
that has a border arounds it already (Label 5)
did you want that border to be different?

Dear Object,

It has a border but I want to know if i can change the border's dimensions?how?

thnx being with me:-)
you mean the thickness of the line
or the padding around it (this can be done using empty border)

Ya the padding around it...
You are right I 'll try to do it with empty border:-)

thnx
Hi Object,

This was easy..with your last comment...

thnxxx
Rachel


no worries, glad I could help :)