Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Renaming labels & text boxes

  1. #1
    Junior Member
    Join Date
    Mar 2011
    Posts
    18
    My Mood
    Stressed
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Renaming labels & text boxes

    I’ve started to create a GUI that consists of a few tabs. Right now I am focusing on two of them. The Pool tab and the Hot Tub tab. When I first started I got everything to work fine on the pool tab. So I figured since all of the label and text box placement would be the same for the Hot Tub tab I would just copy the coding over. Well, I did that and tried naming all the labels and text boxes the same just with the number 2 after them. It’s not working. Now the Hot Tub tab works, but the Pool tab doesn’t, plus the text boxes are gone. I’m also having alignment issues with the text boxes too, but I think that has to do with the naming of the labels and text boxes, I’m not sure. I know the code is kind of long, but I’m just concentrating on the pool and hot tub tabs.

    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
    import java.io.*;
     
     
    public class A extends JFrame implements ActionListener{
    private JTabbedPane jtabbedPane;
    private JPanel General;
    private JPanel Pools;
    private JPanel HotTub;
    private JPanel TempCalc;
     
    JTextField lengthText, widthText, depthText, volumeText, lengthText2, widthText2, depthText2, volumeText2;
     
    public A(){
    setTitle("Volume Calculator");
    setSize(300, 200);
     
    JPanel topPanel = new JPanel();
    topPanel.setLayout( new BorderLayout() );
    getContentPane().add( topPanel );
     
    createGeneral();
    createPools();
    createHotTub();
    createTempCalc();
     
    jtabbedPane = new JTabbedPane();
    jtabbedPane.addTab("General", General);
    jtabbedPane.addTab("Pool", Pools);
    jtabbedPane.addTab("Hot Tub", HotTub);
    jtabbedPane.addTab("Temp Conversion", TempCalc);
     
    topPanel.add(jtabbedPane, BorderLayout.CENTER);
                  }
    public void createGeneral(){
    General = new JPanel();
    General.setLayout( null );
     
    JLabel dateLabel = new JLabel("Today's Date");
    dateLabel.setBounds(10, 15, 150, 20);
    General.add( dateLabel );
     
    JFormattedTextField date = new JFormattedTextField(
    java.util.Calendar.getInstance().getTime());
    date.setEditable(false);
    date.setBounds(90,15,150,20);
    General.add(date);
     
    JButton Close = new JButton("Close");
    Close.setBounds(20,50,80,20);
    Close.addActionListener(this);
    Close.setBackground(Color.white);
    General.add(Close);
                             }
     
    /*        CREATE POOL        */
     
    public void createPools(){
        Pools = new JPanel();
        Pools.setLayout( null );
    JLabel lengthLabel = new JLabel("Length of pool (ft):");
        lengthLabel.setBounds(10, 15, 260, 20);
        Pools.add( lengthLabel );
    lengthText = new JTextField();
        lengthText.setBounds(260, 15, 150, 20);
        Pools.add( lengthText );
    JLabel widthLabel = new JLabel("Width of pool (ft):");
        widthLabel.setBounds(10, 60, 260, 20);
        Pools.add( widthLabel );
    widthText = new JTextField();
        widthText.setBounds(260, 60, 150, 20);
        Pools.add( widthText );
    JLabel depthLabel = new JLabel("Average Depth of pool (ft):");
        depthLabel.setBounds( 10, 100, 260, 20 );
        Pools.add( depthLabel );
    depthText = new JTextField();
        depthText.setBounds(260, 100, 150, 20);
        Pools.add( depthText );
    JLabel volumeLabel = new JLabel("The pool's volume is:(ft ^3");
        volumeLabel.setBounds(10, 200, 260, 20);
        Pools.add( volumeLabel );
        volumeText = new JTextField();
        volumeText.setBounds(260, 200, 150, 20);
        volumeText.setEditable(false);
    Pools.add(volumeText); 
     
    JButton calcVolume = new JButton("Calculate Pool Volume");
        calcVolume.setBounds(150,250,150,20);
        calcVolume.addActionListener(this);
        calcVolume.setBackground(Color.white);
        Pools.add(calcVolume);
     
    JButton Close = new JButton("Close");
        Close.setBounds(350,250,80,20);
        Close.addActionListener(this);
        Close.setBackground(Color.white);
        Pools.add(Close);
                           }
     
    /*        CREATE HOT TUB        */
     
    public void createHotTub(){
    	HotTub = new JPanel();
    	HotTub.setLayout( null );
     
    JLabel lengthLabel2 = new JLabel("Length of hot tub (ft):");
    	lengthLabel2.setBounds(10, 15, 260, 20);
    	HotTub.add( lengthLabel2 );
    lengthText2 = new JTextField();
    	lengthText2.setBounds(260, 15, 150, 20);
    	HotTub.add( lengthText );
    JLabel widthLabel2 = new JLabel("Width of hot tub (ft):");
    	widthLabel2.setBounds(10, 40, 260, 20);
    	HotTub.add( widthLabel2 );
    widthText2 = new JTextField();
    	widthText2.setBounds(260, 40, 150, 20);
    	HotTub.add( widthText );
    JLabel depthLabel2 = new JLabel("Average Depth of hot tub (ft):");
    	depthLabel2.setBounds(10, 65, 260, 20);
    	HotTub.add( depthLabel2 );
    depthText2 = new JTextField();
    	depthText2.setBounds(260, 65, 150, 20);
    	HotTub.add( depthText );
    JLabel volumeLabel2 = new JLabel("The hot tub's volume is:(ft ^3");
    	volumeLabel2.setBounds(10, 200, 260, 20);
    	HotTub.add( volumeLabel2 );
     
    	volumeText2 = new JTextField();
    	volumeText2.setBounds(260, 200, 150, 20);
    	volumeText2.setEditable(false);
    	HotTub.add( volumeText); 
     
     
    JButton calcVolume2 = new JButton("Calculate Hot Tub Volume");
    	calcVolume2.setBounds(150,250,150,20);
    	calcVolume2.addActionListener(this);
    	calcVolume2.setBackground(Color.white);
    	HotTub.add(calcVolume2);
     
    JButton Close = new JButton("Close");
    	Close.setBounds(350,250,80,20);
    	Close.addActionListener(this);
    	Close.setBackground(Color.white);
    	HotTub.add(Close);
                             }
    /*        CREATE TEMPERATURE CONVERSION        */
     
    public void createTempCalc(){
        TempCalc = new JPanel();
        TempCalc.setLayout( null );
        JLabel tempLabel = new JLabel("Enter temperature:");
        tempLabel.setBounds(10, 15, 260, 20);
    TempCalc.add( tempLabel );
     
    JTextField temp = new JTextField();
        temp.setBounds(260, 15, 150, 20);
    TempCalc.add( temp );
     
    JLabel resultsLabel = new JLabel("Calculated Temp:");
    resultsLabel.setBounds(10, 60, 260, 20);
    TempCalc.add( resultsLabel );
     
    JTextField results = new JTextField();
        results.setBounds(260, 60, 150, 20);
        results.setEditable(false);
    TempCalc.add( results );
     
    JButton calcVol = new JButton("Temperature");
        calcVol.setBounds(100,115,110,20);
        calcVol.addActionListener(this);
        calcVol.setBackground(Color.white);
    TempCalc.add(calcVol);
     
    JButton Close = new JButton("Close");
        Close.setBounds(250,115,80,20);
        Close.addActionListener(this);
        Close.setBackground(Color.white);
    TempCalc.add(Close);
                              }
    public void actionPerformed(ActionEvent event){
    JButton button = (JButton)event.getSource();
    String buttonLabel = button.getText();
    if ("Close".equalsIgnoreCase(buttonLabel)){
    Exit_pressed(); return;
        }
        if ("Calculate Pool Volume".equalsIgnoreCase(buttonLabel)){
            Calculate_Volume(); return;
        }
            if ("Calculate Hot Tub Volume".equalsIgnoreCase(buttonLabel)){
                Calculate_Volume2(); return;
        }
                                                 }
    private void Exit_pressed(){
    System.exit(0);
                               }
    private void Calculate_Volume(){
    String lengthString, widthString, depthString;
        int length=0;
        int width=0;
        int depth=0;
     
    lengthString = lengthText.getText();
    widthString = widthText.getText();
    depthString = depthText.getText();
    if (lengthString.length() < 1 || widthString.length() < 1 || depthString.length() < 1 ){
        volumeText.setText("Enter All 3 Numbers"); return;
        }
            length = Integer.parseInt(lengthString);
            width = Integer.parseInt(widthString);
            depth = Integer.parseInt(depthString);
                if (length != 0 || width != 0 || depth != 0 ){
                    volumeText.setText((length * width * depth) + "");
        } else{
            volumeText.setText("Enter All 3 Numbers"); return;
          }
                                   }
    private void Calculate_Volume2(){
    	String length2String, width2String, depth2String;
    	    int length2=0;
    	    int width2=0;
    	    int depth2=0;
     
    	length2String = lengthText.getText();
    	width2String = widthText.getText();
    	depth2String = depthText.getText();
    	if (length2String.length() < 1 || width2String.length() < 1 || depth2String.length() < 1 ){
    	    volumeText2.setText("Enter All 3 Numbers"); return;
    	    }
    	        length2 = Integer.parseInt(length2String);
    	        width2 = Integer.parseInt(width2String);
    	        depth2 = Integer.parseInt(depth2String);
    	            if (length2 != 0 || width2 != 0 || depth2 != 0 ){
    	                volumeText.setText((length2 * width2 * depth2) + "");
    	    } else{
    	        volumeText2.setText("Enter All 3 Numbers"); return;
    	      }
    	                               }
     
    public static void main(String[] args){
    JFrame frame = new A();
    frame.setSize(500, 350);
    frame.setVisible(true);
    }
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Renaming labels & text boxes

    Define "its not working". Just from quickly inspecting your code, I'd highly recommend using a LayoutManager rather than a null layout. See A Visual Guide to Layout Managers (The Java™ Tutorials > Creating a GUI With JFC/Swing > Laying Out Components Within a Container)

Similar Threads

  1. renaming/deleting file
    By The_Mexican in forum What's Wrong With My Code?
    Replies: 15
    Last Post: January 15th, 2011, 12:18 AM
  2. Swing Dialog Boxes --- Have no clue why its not working
    By jap2008 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: November 18th, 2010, 04:20 PM
  3. Applet Help: Parsing and displaying labels
    By katyprim in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 11th, 2010, 09:32 AM
  4. Need help understanding GUI screen layouts and labels
    By Bill_H in forum AWT / Java Swing
    Replies: 3
    Last Post: December 2nd, 2009, 11:50 PM