spot7.org logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

taking text from a JTextField and placing it into a JTextArea


As Andrew Thompson already indicated, make sure your code at least compiles and has a main method. The TextEditor class you provided is missing the loadMenuBar & loadToolBar methods, the actionPerformed & loadCalculator methods are sort of merged together, and a main method is missing in both classes. Please formulate your question as best as you can to get good answers.

I think the reason that the result of the conversion does not show up is that the TextEditor and Calculator objects are not connected to each other. In the Calculator.degreeConversion method you try to pass the result to a TextEditor object, but this object is created by the calculator itself (see the field declaration) and this text editor is not visible. This could be solved by passing a reference of the text editor to the calculator constructor.

In code:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;


/**
 * TextEditor class.
 */
public class TextEditorVersion2 extends JFrame
implements ActionListener {
    JTextArea textArea;

    public static void main(String[] args) {
        final TextEditorVersion2 textEditor = new
TextEditorVersion2();
        textEditor.setVisible(true);
        final CalculatorVersion2
calculatorVersion2 = new
CalculatorVersion2(textEditor);
        calculatorVersion2.setVisible(true);
    }

    public TextEditorVersion2(){
        super("TextMe");
        this.setLayout(new BorderLayout());
        // Freek: these methods are missing.
        //loadMenuBar();
        //loadToolBar();
        // Freek: these methods are missing.
        loadTextArea();
        this.pack();
    }

    private void loadTextArea() {
        // TODO Auto-generated method stub
        textArea = new JTextArea();
        textArea.setPreferredSize(new
Dimension(800,600));
        this.add(BorderLayout.CENTER, textArea);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
    }

    private void loadCalculator() {
        // TODO Auto-generated method stub
        CalculatorVersion2 c = new
CalculatorVersion2(this);
        CalculatorVersion2 calculatorVersion2 =
new CalculatorVersion2(this);
        calculatorVersion2.setVisible(true);
       
calculatorVersion2.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);

       
textArea.setText(String.valueOf(c.fahrenheit));
    }
}


/**
 * Calculator class.
 */
class CalculatorVersion2 extends JFrame implements
ActionListener {
    JButton fToCButton;
    JTextField fToC;
    double fahrenheit;
    TextEditorVersion2 a;

    public CalculatorVersion2(final
TextEditorVersion2 textEditor) {
        super("Unit Converter");
        this.a = textEditor;
        this.setLayout(new FlowLayout());

        fToC = new JTextField(5);

        fToCButton = new JButton("Ferenheit To
Celcius");
        fToCButton.addActionListener(this);

        add(fToCButton, BorderLayout.WEST);
        add(fToC);
        this.pack();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource() == fToCButton){
            degreeConversion();
        }
    }

    private void degreeConversion() {
        // TODO Auto-generated method stub
        double conversion =
Double.parseDouble(fToC.getText());
        fahrenheit = (((conversion -32) * 5) / 9);
       
a.textArea.setText(String.valueOf(fahrenheit));
        System.out.println(fahrenheit);
    }
}

Categories : Java

Related to : taking text from a JTextField and placing it into a JTextArea
JTextArea issues
The problem is with the way you are adding the text area to your frame: You did this: this.add(BorderLayout.CENTER, textArea); instead of the usual: this.add(textArea, BorderLayout.CENTER); The code will still compile, but you have just used a deprecated add method to add the component to the content pane. According to the documentation for this add signature This method changes layout-

Categories : Java
GridBagLayout is not positioning my JTextArea where I want it to
Seeing you are new to GUIs, I advise utilizing a GUI builder; they make designing a GUI considerably easier and more productive. This question names a few options: Best GUI designer for eclipse? Also, is there a reason that you are using the GridBagLayout? For beginners, it may be a difficult to understand and to use. As pointed out by camickr, using the BorderLayout will do the job just fine.

Categories : Java
Usage of Jtextarea in JTable
Create a Color object and invoke the setBackground(Color) method on your JTextArea, after checking if it has focus using the hasFocus(). For example: JTextArea area = new JTextArea(); Color c = Color.CYAN; if(area.hasFocus()) { area.setBackground(c); } Don't forget to save the original color of the (accessible by calling area.getBackground()

Categories : Java
Setting the font of a JTextArea
new Font(fontNameSet, fontSizeSet, Font.PLAIN) This has the arguments in the wrong order. It should be: new Font(fontNameSet, Font.PLAIN, fontSizeSet) Also, as mentioned in a comment: scroll.setFont(new Font(fontNameSet, fontSizeSet, Font.PLAIN)); Should be: texty.setFont(new Font(fontNameSet, Font.PLAIN, fontSizeSet));

Categories : Java
Displaying an array of Strings in a JTextArea
Basically, you are calling setText every time you want to add a new character to the JTextArea in question, this is, first, clearing any existing text and then adding the new String, which, in this case, is the last thing you entered. There are a few ways you could fix this, but because you want to remove the existing text first and then replace it, it's probably better to build a buffer of what

Categories : Java
Recently Add
Concurrent collection to 50/50 read/write
How to get unique members of object class (groupofUniqueNames) from an ldap server using java
Custom error reporting: how to log down function arguments (and possibly locals) in an exception handler?
Websphere MQ message redelivery
How to use BigDecimal to achieve following code instead of using double or float?
What exactly do the service() method of this HttpServlet?
Why does the Java API use int instead of short or byte?
Hibernate. One to one mapping without cross references
How to pass array to a java method
BooleanFieldEditor doesn't load value from PreferenceStore
NoClassDefFoundError when I call the class through commnad line
Shopizer Search
Java - Bank System + Transaction Function
Hadoop security using Apache DS / Apache Directory Studio
Multiple property-placeholder with overlapping locations does not work?
could not remove/add jpanel in jframe
Java garbage collection verbose not working
If child class has a variable with same name as private variable in parent what will be used?
Jackson parse json with a one-to-many relationship in a single step
Excel opens wrong view after generating in Apache POI
On Switch How to use logic operator on case JAVA
Java: What is the absolut first second in Calendar?
Waiting One Second for a button click, if button not clicked call function
Fastest way to extract part of a large JSON File
copy directory operation using linux command in java
Figuring out how to Format Printing
Why does @FunctionalInterface have a RUNTIME retention?
Collision detection Libgdx rectangles
Cannot find symbol in method changeRecL but declared in main method
Receiving "java.net.SocketException: Connection reset" when connecting from dotnet client to a java server
© Copyright 2017 spot7.org Publishing Limited. All rights reserved.