10 Replies - 807 Views - Last Post: 13 January 2013 - 05:56 PM Rate Topic: -----

#1 kosmro   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

BorderLayout. function problem

Posted 31 December 2012 - 12:46 AM

I am trying to build a GUI in JAVA as an Applet (but may convert it to Application due to permission problems). HOWEVER, my problem is that I have 4 panes in the JPane, and it seems that I can only display 3; NORTH, CENTER and SOUTH.

Is this true? Or is there a way to get a 4th pane into the screen?


Sorry if this seems silly, but I can't make heads or tails of it at the moment...bit brain dead (been at it all day) :P


Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: BorderLayout. function problem

#2 blackcompe   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1159
  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: BorderLayout. function problem

Posted 31 December 2012 - 02:27 AM

Do you mean JPanel? What layout are you using? Sounds like BorderLayout. You can add many panels as you want using any layout. How do you want them to be laid out? Can you post code as an example of your problem?

This post has been edited by blackcompe: 31 December 2012 - 05:49 PM

Was This Post Helpful? 1
  • +
  • -

#3 kosmro   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: BorderLayout. function problem

Posted 31 December 2012 - 04:47 AM

I think I may have got some terms muddled there, I apologizes if I have.

Okay, so the code which (is part of my) trouble, is the following:

//Add the components to the applet
        add(p1, BorderLayout.NORTH);
        add(p2, BorderLayout.CENTER);
        add(p3, BorderLayout.CENTER);
        add(p4, BorderLayout.SOUTH);

Specifically, line 3 is not showing, only p1, p3 and p4 show.

What I'm after, is there are 4 main sections (or panels), however in every example and tutorial I have found, there has been no more than 3 'panels' in a row. Is it possible to line them in a vertical line of 4? or must I tweak the GUI to only have 3?



In case other parts of the code are required, here is the whole file (at present).
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.io.*;
import java.util.*;

public class Library extends JApplet{
    //declare standard global variables
    public ArrayList<LibItem> libarr = new ArrayList<LibItem>();
    public int counter = 0;
    public boolean flag = true;
    public String printReadOut;
    //private DescriptionPanel printOutPanel = new DescriptionPanel();
    //public Scanner input = new Scanner(System.in);
    //declare and create text feilds for loan and item entry
    //catCode, loanedTo, dueDate, title, author, subject, format
    //year, edition, onLoan
    
    private JTextField jtfCatCode = new JTextField();
    private JTextField jtfTitle = new JTextField();
    private JTextField jtfAuthor = new JTextField();
    private JTextField jtfSubject = new JTextField();
    private JTextField jtfFormat = new JTextField();
    private JTextField jtfYear = new JTextField();
    private JTextField jtfEdition = new JTextField();
    private JTextField jtfLoanedTo = new JTextField();
    private JTextField jtfDueDate = new JTextField();
    //private JTextField jtfOnLoan = new JTextField();
    
    //Declare and create buttons
    //search buttons
    private JButton jbtSCatCode = new JButton("Catalog Code");
    private JButton jbtSTitle = new JButton("Item Title");
    private JButton jbtSAuthor = new JButton("Item Author");
    //loan buttons
    private JButton jbtLoanOut = new JButton("Loan Item");
    private JButton jbtReturn = new JButton("Return Item");
    //Admin Menu
    private JButton jbtAddItem = new JButton("Add Item");
    private JButton jbtShowAll = new JButton("List All Items");
    private JButton jbtExitApp = new JButton("Quit");
    
    /**Initialize user interface */
    public void init(){
        jtfCatCode.setHorizontalAlignment(JTextField.RIGHT);
        jtfTitle.setHorizontalAlignment(JTextField.RIGHT);
        jtfAuthor.setHorizontalAlignment(JTextField.RIGHT);
        jtfSubject.setHorizontalAlignment(JTextField.RIGHT);
        jtfFormat.setHorizontalAlignment(JTextField.RIGHT);
        jtfYear.setHorizontalAlignment(JTextField.RIGHT);
        jtfEdition.setHorizontalAlignment(JTextField.RIGHT);
        jtfLoanedTo.setHorizontalAlignment(JTextField.RIGHT);
        jtfDueDate.setHorizontalAlignment(JTextField.RIGHT);
        
        //Panel p1 for main entry
        JPanel p1 = new JPanel(new GridLayout(7, 2));
        p1.add(new JLabel("Cataloging Code"));
        p1.add(jtfCatCode);
        p1.add(new JLabel("Title"));
        p1.add(jtfTitle);
        p1.add(new JLabel("Author"));
        p1.add(jtfAuthor);
        p1.add(new JLabel("Subject"));
        p1.add(jtfSubject);
        p1.add(new JLabel("Media Format"));
        p1.add(jtfFormat);
        p1.add(new JLabel("Release Year"));
        p1.add(jtfYear);
        p1.add(new JLabel("Publish Edition"));
        p1.add(jtfEdition);
        p1.setBorder(new TitledBorder("Enter one or all of the following"));
        
        //Panel p2 for search buttons
        JPanel p2 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        p2.add(jbtSCatCode);
        p2.add(jbtSTitle);
        p2.add(jbtSAuthor);
        p2.setBorder(new TitledBorder("Search By"));
        
        //Panel p3 for Loans and Returns
        JPanel p3 = new JPanel(new GridLayout(3, 2));
        p3.add(new JLabel("Name and call sign"));
        p3.add(jtfLoanedTo);
        p3.add(new JLabel("Due date"));
        p3.add(jtfDueDate);
        p3.add(jbtLoanOut);
        p3.add(jbtReturn);
        p3.setBorder(new TitledBorder("Loan's and Return's"));
        
        //Panel p4 for Admin menu
        JPanel p4 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        p4.add(jbtAddItem);
        p4.add(jbtShowAll);
        p4.add(jbtExitApp);
        p4.setBorder(new TitledBorder("Administration Menu"));
        
        //Add the components to the applet
        add(p1, BorderLayout.NORTH);
        add(p2, BorderLayout.CENTER);
        add(p3, BorderLayout.CENTER);
        add(p4, BorderLayout.SOUTH);
        
        /**Register listener*/
        //Search buttons
        jbtSCatCode.addActionListener(new SearchCatCode());
        jbtSTitle.addActionListener(new SearchTitle());
        jbtSAuthor.addActionListener(new SearchAuthor());
        //Loan status buttons
        jbtLoanOut.addActionListener(new LoanOut());
        jbtReturn.addActionListener(new ReturnItem());
        //Admin menu buttons
        jbtAddItem.addActionListener(new AddItem());
        jbtShowAll.addActionListener(new ShowAll());
        jbtExitApp.addActionListener(new ExitApp());
    }//init method
    
    /**Run the Library method*/
    public Library(){
        readfile();
    }
    
    
    /**Handle SearchCatCode button*/
    private class SearchCatCode implements ActionListener{
        public void actionPerformed(ActionEvent e){
        }//action performed
    }//class
    
    /**Handle SearchTitle button*/
    private class SearchTitle implements ActionListener{
        public void actionPerformed(ActionEvent e){
        }//action performed
    }//class
    
    /**Handle SearchAuthor button*/
    private class SearchAuthor implements ActionListener{
        public void actionPerformed(ActionEvent e){
        }//action performed
    }//class
    
    /**Handle LoanOut button*/
    private class LoanOut implements ActionListener{
        public void actionPerformed(ActionEvent e){
        }//action performed
    }//class
    
    /**Handle ReturnItem button*/
    private class ReturnItem implements ActionListener{
        public void actionPerformed(ActionEvent e){
        }//action performed
    }//class
    
    /**Handle AddItem button*/
    private class AddItem implements ActionListener{
        public void actionPerformed(ActionEvent e){
        }//action performed
    }//class
    
    /**Handle ShowAll button*/
    private class ShowAll implements ActionListener{
        public void actionPerformed(ActionEvent e){
            
            for(int c=0; c<counter; c++){
                System.out.println("\n==============================\n" + libarr.get(c));
            }
            System.out.println("\n==============================\n");
            
        }//action performed
    }//class
    
    
    /**Handle ExitApp button*/
    private class ExitApp implements ActionListener{
        public void actionPerformed(ActionEvent e){
            savefile();
            
        }//action performed
    }//class
    
    
    
    /**Read from file method*/
    public void readfile(){
        try{
            Scanner readTextFile = new Scanner(new File("MRARC_Library.txt")).useDelimiter(":");
        
            while(readTextFile.hasNext()){
                String catCode = readTextFile.next();
                String loanedTo = readTextFile.next();
                String dueDate = readTextFile.next();
                String title = readTextFile.next();
                String author = readTextFile.next();
                String subject = readTextFile.next();
                String format = readTextFile.next();
                int year = readTextFile.nextInt();
                int edition = readTextFile.nextInt();
                boolean onLoan = readTextFile.nextBoolean();
                
                LibItem f = new LibItem(catCode, loanedTo, dueDate, title, author, subject, format, year, edition, onLoan);
                libarr.add(counter, f);
                readTextFile.nextLine();
                counter++;
            }
        }//try read
        catch(Exception e){
            System.out.println("Exception: " + e.getMessage());
        }//catch read
    }//readfile
    
    
    /**Save to file method*/
    public void savefile(){
        try{
            PrintWriter writer = new PrintWriter("MRARC_Library.txt");
            for(int c = 0; c<counter; c++){
                writer.print(libarr.get(c).getCatCode() + ":" + libarr.get(c).getLoanedTo() + ":" + libarr.get(c).getDueDate() +
                ":" + libarr.get(c).getTitle() + ":" + libarr.get(c).getAuthor() + ":" +
                libarr.get(c).getSubject() + ":" + libarr.get(c).getFormat() + ":" + libarr.get(c).getYear() +
                ":" + libarr.get(c).getEdition() + ":" + libarr.get(c).getOnLoan() + ":\n");
            }
            writer.flush();
            writer.close();
        }//try save
        catch(Exception e){
            System.out.print("Exception: " + e.getMessage());
        }//catch save
    }//savefile
    
    
}//class





The whole code is by no means anywhere near finished, nor is it likely overly efficent. I have been trying to expand my (slightly limited) Java programming knowledge into something useful, so I've been learning as I go with this Applet coding, so I'm sorry if I seem a bit stupid with this branch of Java.

I hope that code helps :/
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: BorderLayout. function problem

Posted 31 December 2012 - 07:38 AM

Any region of a BorderLayout can only have ONE component
so by doing

       add(p2, BorderLayout.CENTER);  
       add(p3, BorderLayout.CENTER);  


you second line of code simply remove p2 from the CENTER region and replace it by p3

You will need to put p2 or p3 BorderLayout.EAST or BorderLayout.WEST

A BorderLayout can only support 5 component at a single time.

And for an Applet you will have to set its layout to BorderLayout. BorderLayout is the default for a JFrame but not for an Applet.
Was This Post Helpful? 4
  • +
  • -

#5 blackcompe   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1159
  • View blog
  • Posts: 2,547
  • Joined: 05-May 05

Re: BorderLayout. function problem

Posted 31 December 2012 - 12:54 PM

Quote

A BorderLayout can only support 5 component at a single time.


I stand corrected.

Quote

Is it possible to line them in a vertical line of 4? or must I tweak the GUI to only have 3?


Use a BoxLayout.
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: BorderLayout. function problem

Posted 31 December 2012 - 04:32 PM

View Postblackcompe, on 31 December 2012 - 05:27 AM, said:

You can add many panels as you want using any layout.

Didn't note it as it might have been lost in the discussion but this assement is completly false
It is not true for BorderLayour and GridLayout :^:
Sorry my blackcompe friend but now that you insist it really desserve a -1
:) Love you anyhow Happy new year

macosxnerd101 will give you the +1 for sure :)
Was This Post Helpful? 0
  • +
  • -

#7 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: BorderLayout. function problem

Posted 31 December 2012 - 04:40 PM

And I gave you a +1 for a previous right answer in the same thread
Don't say I am no really correct :)
Was This Post Helpful? 0
  • +
  • -

#8 kosmro   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: BorderLayout. function problem

Posted 01 January 2013 - 12:34 AM

So just to clarify, I can't have the 4 in a vertical row for the way I have it setup now, it must be BoxLayout, correct?

Or did I miss something?

This post has been edited by kosmro: 01 January 2013 - 12:37 AM

Was This Post Helpful? 0
  • +
  • -

#9 farrell2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 874
  • View blog
  • Posts: 2,706
  • Joined: 29-July 11

Re: BorderLayout. function problem

Posted 01 January 2013 - 12:38 AM

With BoxLayout you can have as many as you can fit in a row. BorderLayout you can only have 5 items before you need to start nesting items within other items.
Was This Post Helpful? 2
  • +
  • -

#10 kosmro   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: BorderLayout. function problem

Posted 13 January 2013 - 04:35 PM

Well, I've looked into the BoxLayout manager, and I can't see how I would implement what I'm after into that. It could just be me over thinking it...but anyway.

How would I go about nesting with the BorderLayout?
So, in this case, I want to embed JPanel p2 into the bottom of JPanel p1. Is this possible with what I have or am I simply a bumbling idiot?


Cheers.
Was This Post Helpful? 0
  • +
  • -

#11 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: BorderLayout. function problem

Posted 13 January 2013 - 05:56 PM

Yes you can combine all the Layout you want to any depth you want
So you can have a JPanel with a BorderLayout having 5 JPanel with a BorderLayout (one in each of its region) that gives you 25 distinct areas where to add() a single JComponent
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1