0

I have this applet that will add 2 numbers together and display their sum in a third text box.

https://pastebin.com/4ga1brD1

I want the text boxes to be arranged horizontally but more importantly I need the third text box to be uneditable.

import java.applet.*;

import java.awt.*;
import java.awt.event.*;
public class Question extends Applet implements ActionListener
{
   TextField firstNum, secondNum, resultNum;
   public Question()
   {                      
      setLayout(new GridLayout(3, 2, 10, 15));  
      setBackground(Color.cyan);
 
      firstNum = new TextField(15);
      secondNum = new TextField(15);
      resultNum = new TextField(15);
 
      secondNum.addActionListener(this);
 
      add(new Label("Enter First Number"));  
      add(firstNum);
      add(new Label("Enter Second Number")); 
      add(secondNum);
      add(new Label("S U M"));               
      add(resultNum);
   }
   public void actionPerformed(ActionEvent e)
   {                        
      String str1 = firstNum.getText();
      double fn = Double.parseDouble(str1);
      double sn = Double.parseDouble(secondNum.getText());
 
      resultNum.setText("Sum is " + (fn+sn));
   }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Before proceeding further, I just want to make sure you understand that you're not using Swing, and that applets are obsolete. Are you certain that this is the strategy you wish to pursue? This approach would put you at least fifteen years behind the curve. – MarsAtomic May 07 '21 at 00:11
  • @MarsAtomic Yes. – Riley White May 07 '21 at 00:54
  • *I need the third text box to be uneditable.* - Well, people don't write applets or use AWT anymore. So if you want to continue along this path you will need to problem solve on your own. Part of that would be reading the API to find the appropriate method to use. – camickr May 07 '21 at 02:35
  • *"Yes."* Uh-huh. ***Why?*** Even back when I still provided support for applets, if it was a layout / component problem, I'd advise to first sort it in a `Panel` (or a `JPanel`) shown in a `Frame` (or `JFrame`). The reason is that it is simpler to launch and debug a frame. Once it's fixed., the panel can then be dropped straight into an applet. Now it is at the stage were most people cannot even run applets, thus our opportunity to help is stymied. And as to AWT, it was replaced so long ago that most of us with experience in it have forgotten most of the details! – Andrew Thompson May 07 '21 at 02:38
  • BTW - [this](https://i.stack.imgur.com/pt7le.png) is how it looks with the grid layout, and that layout would be better than having all the label / field combos in a single row. Why would the user want that 'one row'? Provide ASCII art or a simple drawing of the *intended* layout of the **entire GUI.** – Andrew Thompson May 07 '21 at 02:48
  • As an aside, each question thread should be self contained with a single question, so this thread should be split in two. Having said that, the answer to the 2nd question re 'editable' is so simple it can be expressed in a single link in a comment: [`TextComponent.setEditable(false)`](https://docs.oracle.com/en/java/javase/15/docs/api/java.desktop/java/awt/TextComponent.html#setEditable(boolean)). As @camickr alluded, this is a method that is mentioned in the JavaDocs of `TextField`, inherited from `TextComponent` (as linked). – Andrew Thompson May 07 '21 at 02:54

1 Answers1

0

To arrange your TextField components horizontally, don't add them directly to the Applet. Instead, create a Panel, give it a FlowLayout and add your TextField components to the Panel. Then you can add the Panel directly to your Applet. Make sure your Applet is wide enough to fit all three boxes side by side or the layout will stack the third box under the first one.

Note: I would test this for you, but no browser has supported Applets for at least five years.

To make the third TextField uneditable, you want to use the setEnabled() method. setEditable() would be the way to accomplish the same thing if you were using JTextField, as opposed to the simple AWT TextField that you're using. You should get in the habit of looking at the official documentation, where the answer is readily found.

resultNum.setEnabled(false);

Bear in mind that a disabled box will appear greyed out, which may be visually unappealing. The solution is to convert to Java Swing, which will allow you to use setEditable, which simply prevents your user from clicking into the box without changing the appearance. Swing would also allow you to use layout managers with much more sophistication and flexibility.

MarsAtomic
  • 10,436
  • 5
  • 35
  • 56
  • *"..or the layout will stack the third box under the first one."* This is one of the reasons a flow layout would not be my first choice for laying out label / field pairs. I'd tend toward a grid **bag** layout, or the 'poor man's equivalent' that the OP is already using, a grid layout which will give all cells the same size. A [`group layout`](https://stackoverflow.com/a/21659516/418556) can produce the same result you could get with grid bag layout, but it takes more effort. – Andrew Thompson May 07 '21 at 07:14
  • @AndrewThompson I hear you, but he's insistent on a Swing-less approach. I haven't touched AWT in some time, so there might well be a better layout that fits the parameters of the question, but I deliberately excluded Swing solutions. – MarsAtomic May 07 '21 at 14:26
  • 1
    OK forget group layout, but `GridBagLayout` is as much a part of AWT as `GridLayout` and `FlowLayout`. – Andrew Thompson May 07 '21 at 14:50