Results 1 to 3 of 3
  1. #1
    Zookey is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default Java Panels and Layouts help

    First of all i want to say hi, because I am new here.

    I am doing some school project, and I need some help with Panels and Layouts.

    Here is picture how program looks when I run it, and as you see JTabbedPane called Pacijenti is on center of screen, but I dont want to look like that.

    I want to stretch it from toolbar to statusbar. I hope you can help me.



    Here is code of the main window.

    Java Code:
    package test2;
    
    import javax.swing.*;
    import javax.swing.border.EmptyBorder;
    import javax.swing.border.LineBorder;
    
    
    
    
    
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    public class GlavniProzor extends JFrame {
    
    	JMenuBar meni;
    	JToolBar toolbar;
    	JButton dugme1, dugme2, dugme3, dugme4;
    	JLabel l1,l2,l3,l4,l5;
    	JTextField txt1,txt2,txt3,txt4,txt5;
    	JTabbedPane jtp;
    	GraphicsDevice myDevice ;
    	Window myWindow;
    	
    	public GlavniProzor(){
    		super("Test");
    		Container cont = getContentPane();
    		cont.setLayout(new GridLayout(3,0));
    		// centriranje prozora
    		Toolkit tk = Toolkit.getDefaultToolkit();
    	    Dimension screenSize = tk.getScreenSize();
    	    int screenHeight = screenSize.height;
    	    int screenWidth = screenSize.width;
    	    setSize(screenWidth / 2, screenHeight / 2);
    	    setLocation(screenWidth / 4, screenHeight / 4);
    	    JPanel panel2 = new JPanel();
    		panel2.setLayout(new BorderLayout());
    	    jtp = new JTabbedPane();
    		jtp.add(new PacijentProzor(), "Pacijenti");
    		
    		
    		add(jtp);
    		
    		
    
    		
    		
    	    //meni
    	    meni = new JMenuBar();
    		setJMenuBar(meni);
    		// Dodadavanje FILE
    		JMenu file = new JMenu("File");
    		meni.add(file);
    		meni.setBorder(LineBorder.createGrayLineBorder());
    		JMenu edit = new JMenu("Edit");
    		meni.add(edit);
    		JMenu help = new JMenu("Help");
    		meni.add(help);
    		
    		JPanel panel1 = new JPanel();
    		panel1.setLayout(new BorderLayout());
    		
    		JToolBar toolbar = new JToolBar();
    		toolbar.setFloatable(false);
    		
    		ImageIcon tbExit = new ImageIcon("exit.png");
    		JButton exitDugme = new JButton(tbExit);
    		exitDugme.setText("Izlaz");
    		// ubacivanje dugmica u toolbar
    		toolbar.add(exitDugme);
    		// dodavnje granice oko dugmica
    		exitDugme.setBorder(new EmptyBorder(0,0,0,0));
    		exitDugme.setToolTipText("Izlaz");
    		exitDugme.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent e){
    				System.exit(0);
    			}
    		});
    		
    		panel1.add(toolbar, BorderLayout.NORTH);
    		panel2.add(jtp);
    		cont.add(panel1);
    		cont.add(panel2);
    		
    		
    				
    		JPanel panel3 = new JPanel();
    		panel3.setLayout(new BorderLayout());
    		JLabel statusbar = new JLabel("Status: Pokrenut je program TEST...");
            statusbar.setPreferredSize(new Dimension(-1, 22));
            statusbar.setBorder(LineBorder.createGrayLineBorder());
            panel3.add(statusbar, BorderLayout.SOUTH);
            cont.add(panel3);
    	}
    
    	public static void main(String[] args) {
    		GlavniProzor a = new GlavniProzor();
    		a.setVisible(true);
    		a.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		
    		
    	}
    
    }
    And here is code of another class, where I created stuff in JTabbedPane

    Java Code:
    package test2;
    
    
    
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.FocusEvent;
    import java.awt.event.FocusListener;
    import java.text.ParseException;
    import java.util.Date;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class PacijentProzor extends JPanel implements ActionListener, FocusListener{
    	private static final long serialVersionUID = 3073076482598940379L;
    	
    	private JLabel lblId;
    	private JTextField tfPrezime, tfIme, tfDatum, tfTelefon, tfAdresa, tfGrad, tfStomatolog;
    	private JButton btnPrethodni, btnSljedeci, btnBrisi, btnNovi;
    
    
    	private int pnum = 0;
    	private JFrame prozor = null;
    	
    	public PacijentProzor()
    	{
    		postaviGUI();
    	}
    	
    	public PacijentProzor(boolean noviProzor)
    	{
    		postaviGUI();
    		if(noviProzor)
    		{
    			prozor = new JFrame("Pacijenti");
    			prozor.add(this);
    			prozor.pack();
    			prozor.setVisible(true);
    		}
    	}
    	
    	public void prikazi()
    	{
    		if(prozor == null) return;
    		prozor.setVisible(true);
    		prozor.toFront();
    	}
    	
    	private void postaviGUI()
    	{
    		addFocusListener(this);
    		setLayout(new GridLayout(10, 2));
    		
    		btnPrethodni = new JButton("< Prethodni"); btnPrethodni.addActionListener(this);
    		add(btnPrethodni);
    		
    		btnSljedeci = new JButton("Sljedeći >"); btnSljedeci.addActionListener(this);
    		add(btnSljedeci);
    		
    		add(new JLabel("ID"));
    		lblId = new JLabel("0");
    		add(lblId);
    		
    		add(new JLabel("Prezime"));
    		tfPrezime = new JTextField();
    		tfPrezime.addFocusListener(this);
    		add(tfPrezime);
    		
    		add(new JLabel("Ime"));
    		tfIme = new JTextField();
    		tfIme.addFocusListener(this);
    		add(tfIme);
    		
    		add(new JLabel("Datum rođenja"));
    		tfDatum = new JTextField();
    		tfDatum.addFocusListener(this);
    		add(tfDatum);
    		
    		add(new JLabel("Broj telefona"));
    		tfTelefon = new JTextField();
    		tfTelefon.addFocusListener(this);
    		add(tfTelefon);
    		
    		add(new JLabel("Adresa"));
    		tfAdresa = new JTextField();
    		tfAdresa.addFocusListener(this);
    		add(tfAdresa);
    		
    		add(new JLabel("Grad"));
    		tfGrad = new JTextField();
    		tfGrad.addFocusListener(this);
    		add(tfGrad);
    		
    		add(new JLabel("Stomatolog"));
    		tfStomatolog = new JTextField();
    		tfStomatolog.addFocusListener(this);
    		add(tfStomatolog);
    		
    		btnBrisi = new JButton("Obriši"); btnBrisi.addActionListener(this);
    		add(btnBrisi);
    		
    		btnNovi = new JButton("Dodaj"); btnNovi.addActionListener(this);
    		add(btnNovi);
    		
    	
    	}
    
    	@Override
    	public void focusGained(FocusEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void focusLost(FocusEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		// TODO Auto-generated method stub
    		
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Then don't use a GridLayout for the JFrame's contentPane. Rather, why not use a BorderLayout or a mixture of layouts?

  3. #3
    Zookey is offline Member
    Join Date
    Jan 2011
    Posts
    6
    Rep Power
    0

    Default

    Thank you very much. I solved problem with BroderLayout. :)

Similar Threads

  1. problems with layouts !
    By ron2794 in forum NetBeans
    Replies: 1
    Last Post: 12-15-2010, 02:23 AM
  2. Deleting panels
    By dilpreet28 in forum New To Java
    Replies: 13
    Last Post: 08-11-2010, 10:23 AM
  3. Can you have multiple layouts using swing?
    By fresh83 in forum New To Java
    Replies: 2
    Last Post: 12-27-2009, 10:38 PM
  4. resizeable panels
    By simo_mon in forum AWT / Swing
    Replies: 4
    Last Post: 07-29-2009, 02:45 AM
  5. Layouts ?
    By plodos in forum Eclipse
    Replies: 3
    Last Post: 07-30-2007, 04:03 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
  •