Nooby FlowLayout problem...


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Nooby FlowLayout problem...

  1. #1
    Join Date
    Jan 2005
    Posts
    61

    Nooby FlowLayout problem...

    Hi,

    I've just started learning Java, and after a few chapters of theory, I decided to try a bit of GUI to see if it helped drive the theory home. I think it's working a bit, but I've got a problem that I"m sure demonstrates a pretty fundamental part of Java prgramming.

    The source below is from a GUI doc that I've been doing, using FlowLayout manager. For some reason, all the panels that I've created are controlled by the layout manager of the last panel, and have no individual control. As far as I"m aware, every panel is a new object with a new FlowLayout manager, so each should be individually controllable, but it isn't working. I'd really appreciate any help - and please don't laugh - it's only a play thing to try to teach me some of the basics...

    Source:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class CurrencyConverter extends JFrame {

    FlowLayout primaryFlow = new FlowLayout(FlowLayout.CENTER);


    String[] currency = {"Convert From:", " ", "UK Pounds", "US Dollars", "French Francs", "Spanish Pesetas", "German Marks",
    "Welsh Sheep", "Arabian Camels", "Essex Virgins* "};

    String[] reverseCurrency = {"Convert To:", " ", "Essex Virgins* ", "Arabian Camels","Welsh Sheep",
    "German Marks", "Spanish Pesetas", "French Francs", "US Dollars", "UK Pounds" };


    public CurrencyConverter() {
    super("Currency Converter");
    setSize(550, 600);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



    //declare all new panels
    JPanel topList = new JPanel();
    JPanel selectCurrencyFrom = new JPanel();
    JPanel selectCurrencyTo = new JPanel();
    JPanel availability = new JPanel();
    JPanel exchangeDetails = new JPanel();
    JPanel changeAmount = new JPanel();
    JPanel actionButtons = new JPanel();
    JPanel result = new JPanel();
    JPanel checkBox = new JPanel();
    //declare all new lists
    JList convertFrom = new JList(currency);
    JList convertTo = new JList(reverseCurrency);
    //declare all new text fields
    JTextField exchangeRate = new JTextField(8);
    JTextField amountToChange = new JTextField(8);
    JTextField conversionResult = new JTextField(8);
    //declare all new labels
    JLabel select = new JLabel("Please select the currency to convert FROM and TO in the boxes below:");
    JLabel subjectToAvailability = new JLabel("* Subject to Availability - first come first served.");
    JLabel rate = new JLabel("Enter Current Exchange Rate (2 Decimal Places):");
    JLabel enterAmount = new JLabel("Enter the Amount of Money You Want to Change: ");
    JLabel conversionSum = new JLabel("The Conversion Result is: ");
    //declare all new buttons and check boxes
    JButton calculate = new JButton("Calculate");
    JButton cancel = new JButton("Cancel");
    JCheckBox please = new JCheckBox("Oh yes please - inundate me with useless information!", true);
    //add new flow layouts
    FlowLayout topListFlow = new FlowLayout(FlowLayout.CENTER, 10, 10);
    FlowLayout currencyFromFlow = new FlowLayout(FlowLayout.CENTER, 10, 10);
    FlowLayout toListFlow = new FlowLayout(FlowLayout.CENTER, 100, 10);
    FlowLayout subjectToAvailabilityFlow = new FlowLayout(FlowLayout.CENTER, 100, 10);
    FlowLayout exchangeRateFlow = new FlowLayout(FlowLayout.RIGHT, 100, 10);
    FlowLayout amountFlow = new FlowLayout(FlowLayout.CENTER, 100, 10);
    FlowLayout buttonFlow = new FlowLayout(FlowLayout.LEFT, 10, 10);
    FlowLayout answerFlow = new FlowLayout(FlowLayout.CENTER, 100, 10);
    FlowLayout pleaseFlow = new FlowLayout(FlowLayout.CENTER, 100, 10);



    //create topList
    topList.add(select);
    getContentPane().add(topList);
    getContentPane().setLayout(topListFlow);
    setVisible(true);

    //create select currency From list
    getContentPane().setLayout(currencyFromFlow);
    convertFrom.setVisibleRowCount(7);
    JScrollPane scroller1 = new JScrollPane(convertFrom);
    selectCurrencyFrom.add(scroller1);
    getContentPane().add(selectCurrencyFrom);
    setVisible(true);

    //create select currency To list
    getContentPane().setLayout(toListFlow);
    convertTo.setVisibleRowCount(7);
    JScrollPane scroller2 = new JScrollPane(convertTo);
    selectCurrencyTo.add(scroller2);
    getContentPane().add(selectCurrencyTo);
    setVisible(true);

    //create subjectToAvailability
    getContentPane().setLayout(subjectToAvailabilityFlow);
    availability.add(subjectToAvailability);
    getContentPane().add(availability);
    setVisible(true);


    //add current exchange rate panel
    getContentPane().setLayout(exchangeRateFlow);
    exchangeDetails.add(rate);
    exchangeDetails.add(exchangeRate);
    getContentPane().add(exchangeDetails);
    setVisible(true);

    //add amount field
    getContentPane().setLayout(amountFlow);
    changeAmount.add(enterAmount);
    changeAmount.add(amountToChange);
    getContentPane().add(changeAmount);
    setVisible(true);

    //add calculate and cancel buttons
    actionButtons.add(calculate);
    actionButtons.add(cancel);
    getContentPane().add(actionButtons);
    getContentPane().setLayout(buttonFlow);
    setVisible(true);

    //add answer field
    getContentPane().setLayout(answerFlow);
    result.add(conversionSum);
    result.add(conversionResult);
    getContentPane().add(result);
    setVisible(true);

    //add inundate me with junk panel
    getContentPane().setLayout(pleaseFlow);
    checkBox.add(please);
    getContentPane().add(please);
    setVisible(true);

    }

    public static void main(String[] args) {
    CurrencyConverter app = new CurrencyConverter();
    }
    }



    Sorry if that's silly amounts of code, but I figured it was all likely to be useful.

  2. #2
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Your setup needs additional layoutmanagers to the
    flowlayout in order to behave properly during resize.

    Here I have rearranged it a bit using
    a mixture of gridlayout, borderlayout and flowlayout. The last one only for the simplest components.

    Flowlayout is basically just way of "smearing" the
    components into the container.
    Attached Files Attached Files
    eschew obfuscation

  3. #3
    Join Date
    Jan 2005
    Posts
    61
    Wow, that's fantastic - thank you very much!!!!

    It'll take me some time to decipher what's going on there, but a mixture of layouts was something I'd aready looked at, so it'll be good learning for me. Thank you again, that's just great

  4. #4
    Join Date
    Jan 2005
    Posts
    61
    This needs a follow up - I've just compiled it and realised all the stuff you did. That bit of source has given me enough learning to keep me busy for a week or two Really, grateful, thanks a million!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center