Applet JTextField Size Problem


Posted on 16th Feb 2014 01:33 pm by admin

Hello,

I recently started making an applet to calculate certain values for airplanes. However, Gui has proved to be a problem. The JFormattedTextFields I am using have worked perfectly fine until I discovered this problem. Basically, when you enter numbers into the text boxes and then re size the browser or window, when run from my IDE, they change size based on how much space the number takes up. This messes up the layout and is quite annoying. My only suspicion is that resizing the window calls some event that updates their sizes..though why they change size I do not know. I am using box layout.

I would post the images, but I do not have enough posts yet, this being my first so those links should do.

Normal default layout on start of applet:

i583.photobucket.com/albums/ss271/a13w/Normal.png

Resized layout with input:

i583.photobucket.com/albums/ss271/a13w/Resized.png

This is the code:
Code: import java.awt.*;import java.applet.*;import java.awt.event.*;import javax.swing.JApplet;import javax.swing.JButton;import javax.swing.JLabel;import javax.swing.JFormattedTextField;import javax.swing.JTextField;import javax.swing.JPanel;import javax.swing.BoxLayout;import javax.swing.Box;import javax.swing.text.NumberFormatter;public class WeightBalanceCalcuator extends JApplet { Container contentPane; Listener listen; //------------Calculator JObjects--------- //Main Calculator Frame JPanel calculatorPanel; JPanel calculatorPanelRow0; //Blank Row JPanel calculatorPanelRow1; JPanel calculatorPanelRow2; JPanel calculatorPanelRow3; JPanel calculatorPanelRow4; JPanel calculatorPanelRow5; JPanel calculatorPanelRow6; JPanel calculatorPanelRow7; //Blank Row JPanel calculatorPanelRow8; //Row 1 Components JLabel emptyWeightLabel; JFormattedTextField emptyWeight; JFormattedTextField emptyLever; JFormattedTextField emptyMoment; //Row 2 Components JLabel frontWeightLabel; JFormattedTextField frontWeight; JFormattedTextField frontLever; JFormattedTextField frontMoment; //Row 3 Components JLabel rearWeightLabel; JFormattedTextField rearWeight; JFormattedTextField rearLever; JFormattedTextField rearMoment; //Row 4 Components JLabel fuelWeightLabel; JFormattedTextField fuelWeight; JFormattedTextField fuelLever; JFormattedTextField fuelMoment; //Row 5 Components JLabel oilWeightLabel; JFormattedTextField oilWeight; JFormattedTextField oilLever; JFormattedTextField oilMoment; //Row 6 Components JLabel baggageWeightLabel; JFormattedTextField baggageWeight; JFormattedTextField baggageLever; JFormattedTextField baggageMoment; //Row 8 Components JLabel totalWeightLabel; JFormattedTextField totalWeight; JLabel totalMomentLabel; JFormattedTextField totalMoment; //---------------------------------------- public void init() { listen = new Listener(); initCalculatorPanel(this); } //Declares and adds all graphical components which have to do with the calculator section public void initCalculatorPanel(JApplet window) { contentPane = window.getContentPane(); //Initalize Panels calculatorPanel = new JPanel(); calculatorPanelRow0 = new JPanel(); //Blank Row calculatorPanelRow1 = new JPanel(); calculatorPanelRow2 = new JPanel(); calculatorPanelRow3 = new JPanel(); calculatorPanelRow4 = new JPanel(); calculatorPanelRow5 = new JPanel(); calculatorPanelRow6 = new JPanel(); calculatorPanelRow7 = new JPanel(); calculatorPanelRow8 = new JPanel(); calculatorPanel.setBackground(Color.WHITE); calculatorPanelRow0.setBackground(Color.WHITE); calculatorPanelRow1.setBackground(Color.WHITE); calculatorPanelRow2.setBackground(Color.WHITE); calculatorPanelRow3.setBackground(Color.WHITE); calculatorPanelRow4.setBackground(Color.WHITE); calculatorPanelRow5.setBackground(Color.WHITE); calculatorPanelRow6.setBackground(Color.WHITE); calculatorPanelRow7.setBackground(Color.WHITE); calculatorPanelRow8.setBackground(Color.WHITE); contentPane.add(calculatorPanel, BorderLayout.NORTH); calculatorPanel.setLayout(new GridLayout(9,1)); calculatorPanel.add(calculatorPanelRow0); calculatorPanel.add(calculatorPanelRow1); calculatorPanel.add(calculatorPanelRow2); calculatorPanel.add(calculatorPanelRow3); calculatorPanel.add(calculatorPanelRow4); calculatorPanel.add(calculatorPanelRow5); calculatorPanel.add(calculatorPanelRow6); calculatorPanel.add(calculatorPanelRow7); calculatorPanel.add(calculatorPanelRow8); calculatorPanelRow1.setLayout(new BoxLayout(calculatorPanelRow1, BoxLayout.X_AXIS)); calculatorPanelRow2.setLayout(new BoxLayout(calculatorPanelRow2, BoxLayout.X_AXIS)); calculatorPanelRow3.setLayout(new BoxLayout(calculatorPanelRow3, BoxLayout.X_AXIS)); calculatorPanelRow4.setLayout(new BoxLayout(calculatorPanelRow4, BoxLayout.X_AXIS)); calculatorPanelRow5.setLayout(new BoxLayout(calculatorPanelRow5, BoxLayout.X_AXIS)); calculatorPanelRow6.setLayout(new BoxLayout(calculatorPanelRow6, BoxLayout.X_AXIS)); calculatorPanelRow8.setLayout(new BoxLayout(calculatorPanelRow8, BoxLayout.X_AXIS)); //Initalize elements //Initalize Row 1 Components emptyWeightLabel = new JLabel("Aircraft empty weight:"); emptyWeightLabel.setHorizontalAlignment(JLabel.RIGHT); emptyWeight = new JFormattedTextField(new NumberFormatter()); emptyWeight.setMinimumSize(new Dimension(73 ,0)); emptyWeight.addFocusListener(listen); emptyLever = new JFormattedTextField(new NumberFormatter()); emptyMoment = new JFormattedTextField(new NumberFormatter()); emptyMoment.setEditable(false); //Initalize Row 2 Components frontWeightLabel = new JLabel("Pilot & front passenger weight:"); frontWeightLabel.setHorizontalAlignment(JLabel.RIGHT); frontWeight = new JFormattedTextField(new NumberFormatter()); frontWeight.addFocusListener(listen); frontLever = new JFormattedTextField(new NumberFormatter()); frontMoment = new JFormattedTextField(new NumberFormatter()); frontMoment.setEditable(false); //Initalize Row 3 Components rearWeightLabel = new JLabel("Rear passengers weight:"); rearWeightLabel.setHorizontalAlignment(JLabel.RIGHT); rearWeight = new JFormattedTextField(new NumberFormatter()); rearWeight.addFocusListener(listen); rearLever = new JFormattedTextField(new NumberFormatter()); rearMoment = new JFormattedTextField(new NumberFormatter()); rearMoment.setEditable(false); //Initalize Row 4 Components fuelWeightLabel = new JLabel("Fuel weight:"); fuelWeightLabel.setHorizontalAlignment(JLabel.RIGHT); fuelWeight = new JFormattedTextField(new NumberFormatter()); fuelWeight.addFocusListener(listen); fuelLever = new JFormattedTextField(new NumberFormatter()); fuelMoment = new JFormattedTextField(new NumberFormatter()); fuelMoment.setEditable(false); //Initalize Row 5 Components oilWeightLabel = new JLabel("Oil weight:"); oilWeightLabel.setHorizontalAlignment(JLabel.RIGHT); oilWeight = new JFormattedTextField(new NumberFormatter()); oilWeight.addFocusListener(listen); oilLever = new JFormattedTextField(new NumberFormatter()); oilMoment = new JFormattedTextField(new NumberFormatter()); oilMoment.setEditable(false); //Initalize Row 6 Components baggageWeightLabel = new JLabel("Baggage weight:"); baggageWeightLabel.setHorizontalAlignment(JLabel.RIGHT); baggageWeight = new JFormattedTextField(new NumberFormatter()); baggageWeight.addFocusListener(listen); baggageLever = new JFormattedTextField(new NumberFormatter()); baggageMoment = new JFormattedTextField(new NumberFormatter()); baggageMoment.setEditable(false); //Initalize Row 8 Components totalWeightLabel = new JLabel("Total weight:"); totalWeight = new JFormattedTextField(new NumberFormatter()); totalWeight.setEditable(false); totalMomentLabel = new JLabel("Total moment:"); totalMoment = new JFormattedTextField(new NumberFormatter()); totalMoment.setEditable(false); //Add calculator panel elements, divided into rows //Add row 1 components calculatorPanelRow1.add(Box.createHorizontalGlue()); calculatorPanelRow1.add(Box.createRigidArea(new Dimension(80,0))); calculatorPanelRow1.add(emptyWeightLabel); calculatorPanelRow1.add(emptyWeight); calculatorPanelRow1.add(new JLabel("lb")); calculatorPanelRow1.add(Box.createRigidArea(new Dimension(20,0))); calculatorPanelRow1.add(emptyLever); calculatorPanelRow1.add(new JLabel("inch")); calculatorPanelRow1.add(Box.createRigidArea(new Dimension(20,0))); calculatorPanelRow1.add(emptyMoment); calculatorPanelRow1.add(new JLabel("inch-pounds")); calculatorPanelRow1.add(Box.createRigidArea(new Dimension(30,0))); //Add row 2 components calculatorPanelRow2.add(Box.createHorizontalGlue()); calculatorPanelRow2.add(Box.createRigidArea(new Dimension(30,0))); calculatorPanelRow2.add(frontWeightLabel); calculatorPanelRow2.add(frontWeight); calculatorPanelRow2.add(new JLabel("lb")); calculatorPanelRow2.add(Box.createRigidArea(new Dimension(20,0))); calculatorPanelRow2.add(frontLever); calculatorPanelRow2.add(new JLabel("inch")); calculatorPanelRow2.add(Box.createRigidArea(new Dimension(20,0))); calculatorPanelRow2.add(frontMoment); calculatorPanelRow2.add(new JLabel("inch-pounds")); calculatorPanelRow2.add(Box.createRigidArea(new Dimension(30,0))); //Add row 3 components calculatorPanelRow3.add(Box.createHorizontalGlue()); calculatorPanelRow3.add(Box.createRigidArea(new Dimension(63,0))); calculatorPanelRow3.add(rearWeightLabel); calculatorPanelRow3.add(rearWeight); calculatorPanelRow3.add(new JLabel("lb")); calculatorPanelRow3.add(Box.createRigidArea(new Dimension(20,0))); calculatorPanelRow3.add(rearLever); calculatorPanelRow3.add(new JLabel("inch")); calculatorPanelRow3.add(Box.createRigidArea(new Dimension(20,0))); calculatorPanelRow3.add(rearMoment); calculatorPanelRow3.add(new JLabel("inch-pounds")); calculatorPanelRow3.add(Box.createRigidArea(new Dimension(30,0))); //Add row 4 components calculatorPanelRow4.add(Box.createHorizontalGlue()); calculatorPanelRow4.add(Box.createRigidArea(new Dimension(138,0))); calculatorPanelRow4.add(fuelWeightLabel); calculatorPanelRow4.add(fuelWeight); calculatorPanelRow4.add(new JLabel("lb")); calculatorPanelRow4.add(Box.createRigidArea(new Dimension(20,0))); calculatorPanelRow4.add(fuelLever); calculatorPanelRow4.add(new JLabel("inch")); calculatorPanelRow4.add(Box.createRigidArea(new Dimension(20,0))); calculatorPanelRow4.add(fuelMoment); calculatorPanelRow4.add(new JLabel("inch-pounds")); calculatorPanelRow4.add(Box.createRigidArea(new Dimension(30,0))); //Add row 5 components calculatorPanelRow5.add(Box.createHorizontalGlue()); calculatorPanelRow5.add(Box.createRigidArea(new Dimension(146,0))); calculatorPanelRow5.add(oilWeightLabel); calculatorPanelRow5.add(oilWeight); calculatorPanelRow5.add(new JLabel("lb")); calculatorPanelRow5.add(Box.createRigidArea(new Dimension(20,0))); calculatorPanelRow5.add(oilLever); calculatorPanelRow5.add(new JLabel("inch")); calculatorPanelRow5.add(Box.createRigidArea(new Dimension(20,0))); calculatorPanelRow5.add(oilMoment); calculatorPanelRow5.add(new JLabel("inch-pounds")); calculatorPanelRow5.add(Box.createRigidArea(new Dimension(30,0))); //Add row 6 components calculatorPanelRow6.add(Box.createHorizontalGlue()); calculatorPanelRow6.add(Box.createRigidArea(new Dimension(111,0))); calculatorPanelRow6.add(baggageWeightLabel); calculatorPanelRow6.add(baggageWeight); calculatorPanelRow6.add(new JLabel("lb")); calculatorPanelRow6.add(Box.createRigidArea(new Dimension(20,0))); calculatorPanelRow6.add(baggageLever); calculatorPanelRow6.add(new JLabel("inch")); calculatorPanelRow6.add(Box.createRigidArea(new Dimension(20,0))); calculatorPanelRow6.add(baggageMoment); calculatorPanelRow6.add(new JLabel("inch-pounds")); calculatorPanelRow6.add(Box.createRigidArea(new Dimension(30,0))); //Add row 8 Components calculatorPanelRow8.add(Box.createRigidArea(new Dimension(133,0))); calculatorPanelRow8.add(totalWeightLabel); calculatorPanelRow8.add(totalWeight); calculatorPanelRow8.add(new JLabel("lb")); calculatorPanelRow8.add(Box.createRigidArea(new Dimension(55,0))); calculatorPanelRow8.add(totalMomentLabel); calculatorPanelRow8.add(totalMoment); calculatorPanelRow8.add(new JLabel("inch-pounds")); calculatorPanelRow8.add(Box.createRigidArea(new Dimension(30,0))); calculatorPanelRow8.add(Box.createHorizontalGlue()); } //Updates moment text fields public void updateMoment() {// emptyMoment.setText(); } //Updates both bottom total text fields public void updateTotal() { if (weightFieldsNotEmpty()) { totalWeight.setText(String.valueOf(Double.parseDouble(emptyWeight.getText()) + Double.parseDouble(frontWeight.getText()) + Double.parseDouble(rearWeight.getText()) + Double.parseDouble(fuelWeight.getText()) + Double.parseDouble(oilWeight.getText()) + Double.parseDouble(baggageWeight.getText()))); if (momentFieldsNotEmpty()) { totalMoment.setText(String.valueOf(Double.parseDouble(emptyMoment.getText()) + Double.parseDouble(frontMoment.getText()) + Double.parseDouble(rearMoment.getText()) + Double.parseDouble(fuelMoment.getText()) + Double.parseDouble(oilMoment.getText()) + Double.parseDouble(baggageMoment.getText()))); } } }; //Checks to see if any weight field is empty, if so returns false private boolean weightFieldsNotEmpty() { if (emptyWeight.getText().equals("") || frontWeight.getText().equals("")|| rearWeight.getText().equals("") || fuelWeight.getText().equals("") || oilWeight.getText().equals("") || baggageWeight.getText().equals("") ) return false; return true; } //Checks to see if any moment field is empty, if so returns false private boolean momentFieldsNotEmpty() { if (emptyMoment.getText().equals("") || frontMoment.getText().equals("")|| rearMoment.getText().equals("") || fuelMoment.getText().equals("") || oilMoment.getText().equals("") || baggageMoment.getText().equals("") ) return false; return true; } //Listens for when a weight text box is unfocuses then updates Moment and Totals. public class Listener implements FocusListener { public Listener() { } public void focusGained(FocusEvent e) { System.out.println(emptyWeight.getBounds().getWidth()); } public void focusLost(FocusEvent e) { updateMoment(); updateTotal(); } }} Thank you

No comments posted yet

Your Answer:

Login to answer
143 Like 13 Dislike
Previous forums Next forums
Other forums

Getting PHP to ammended the selected in option field
Hey Guys

thanks for everyones help so far. This site and its users are oozing awesomness.

PHP hyperlinks generator
Hi

I need some help to get this done using php:


1 - I have few hyperlinks say 500

Pasha de Cartier
santos de cartier - männer
ballon bleu catier uhren
[b][url=http://www.cartierwatches.cn/

button.click += new eventhandler() not works in if(!ispostback)
i want to use button.clcik event in function VIewScrap() which is called in if(!ispostback) but itdo

Logging and nologging bulk insert
Hi,

oracle version: 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Google+ Indyaspeak @ Pinterest RSS