Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

GUI basics setText method ?

843785Mar 14 2009 — edited Mar 15 2009
Hi forum participants,

I am trying the develop a GUI to convert a lower case character to uppercase and visa versa
from an inputted string.
I am new at GUI classes and am having trouble getting this program to work.
The main problem as you will notice is with the method setText() where I am trying to set
the text of the output text field. I am not using this method correctly because I would like to output the
new character (that gets changed from LC to UC or UC to LC) as I step through the inputted string.
Any suggestions on how to do this (e.g. outputting the characters after I change the case (lower or upper)
to the output text field)? Here is my code below.

Thanks in advance,
M
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Chp7_Q6_ConvertCase extends JFrame {
        
    private static final int WIDTH = 500; //width of JFrame
    private static final int HEIGHT = 50;  // height of JFrame
    private JLabel caseInput, caseOutput;  // declare label objects
    private JTextField caseInputTF, caseOutputTF;  // declare textfield obj's
    private CaseInHandler inputHandler;  // declare a listener obj
        
    public Chp7_Q6_ConvertCase() {
    
        setTitle("Letter Case Converter");  // title of JFrame
        Container pane = getContentPane();  // declare obj to manage pane
        pane.setLayout(new GridLayout(1, 4));  // set layout of pane
        
        caseInput = new JLabel("Input string: ", 
                SwingConstants.RIGHT); // create label obj and initialize
        caseOutput = new JLabel("Output string: ", 
                SwingConstants.RIGHT); // create label obj and initialize
        
        caseInputTF = new JTextField(20); // create TF obj and init
        caseOutputTF = new JTextField(20); // create TF obj and init
        
        pane.add(caseInput);  // add Label to pane
        pane.add(caseInputTF); // add TF to pane
        pane.add(caseOutput); // add Label to pane
        pane.add(caseOutputTF); // add TF to pane
        
        inputHandler = new CaseInHandler(); // create a listener obj
               
        caseInputTF.addActionListener(inputHandler);  // add TF to  
                                                      // ActionListener
                                                      // and associate
                                                      // with listener obj
        setSize(WIDTH, HEIGHT); // set size of pane
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);  // make visible    
    
    } // end Chp7_Q6_ConvertCase class
    
    private class CaseInHandler implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            
            String stringIn = caseInputTF.getText(); // retrieve input
                                                     // store in StringIn
                                  
            // if character is uppercase convert to lower case and 
            // visa versa; step through string and output new case
            for(int count = 0; count <= stringIn.length() - 1; count++) {
                
                char c = stringIn.charAt(count);
                                              
                if(Character.isUpperCase(c)) 
                    caseOutputTF.setText(String.format(stringIn,  
                            Character.toLowerCase(c)));
                else
                    caseOutputTF.setText(String.format(stringIn, 
                            Character.toUpperCase(c)));
                    
            } // end for
        } // end actionPerformed
    } // end CaseInHandler
    
    
    public static void main(String[] args) {
        
        Chp7_Q6_ConvertCase example = new Chp7_Q6_ConvertCase(); 
    } // end main 
} // end Chp7_Q6_ConvertCase extends JFrame class
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 12 2009
Added on Mar 14 2009
2 comments
282 views