1 Replies - 788 Views - Last Post: 10 December 2016 - 04:09 AM Rate Topic: -----

#1 subasuba   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-December 16

New to Swing, my JTextArea is not showing up

Posted 09 December 2016 - 07:30 PM

Hello all, I am very new to Swing in Java and I'm used to JavaFX, but for some reason, my professor would like for us to do everything in Swing. Point aside, Everything seems to be coming up just fine in my JFrame except my Text Area. I'm not sure why. Please see my code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Font;
public class PlantGUIDriverFinalAssignment extends JFrame{
        JRadioButton rdoBtnPlant, rdoBtnFlower, rdoBtnHerb, rdoBtnWeed, rdoBtnFungus;
        JCheckBox isPoisonous, isEdible, isMedicinal, isNicelyFlavored, isSeasonal, hasThorns, smellsNice;
        JButton btnAdd, btnRemove, btnFilter, btnClear, btnSort;
        JTextField txtPlantID, txtColor, txtName;
        JScrollPane scrollPane;
        JTextArea plantSummary;
        String plantColor, plantName, plantID;
        
	public PlantGUIDriverFinalAssignment() {
            try {
                this.setTitle("Plant Interface");
                setLayout(new FlowLayout());
                JPanel plantPanel1 = new JPanel(); //Create a Pane to add components within them to better organize the UI
                JPanel plantPanel2 = new JPanel(); //Create another Panel to hold more plant components (checkboxes, etc)
                JPanel plantPanel3 = new JPanel(); //Create this panel to hold buttons.
                JPanel plantPanel4 = new JPanel(); //Create this Panel to hold TextArea plantSummary
                
                rdoBtnPlant = new JRadioButton("Plant");
                rdoBtnFlower = new JRadioButton("Flower");
                rdoBtnWeed = new JRadioButton("Weed");
                rdoBtnHerb = new JRadioButton("Herb");
                rdoBtnFungus = new JRadioButton("Fungus");                

                rdoBtnPlant.setSelected(true);

                //Create a group of Radio Buttons
                ButtonGroup plantSelection = new ButtonGroup();
                plantSelection.add(rdoBtnPlant);
                plantSelection.add(rdoBtnFlower);
                plantSelection.add(rdoBtnHerb);
                plantSelection.add(rdoBtnWeed);
                plantSelection.add(rdoBtnFungus);
                JLabel plantSelectLabel = new JLabel("Please select from the following plants to add below: ");
                plantSelectLabel.setFont(new Font("Serif", 20, 20));
                
                //Add radio buttons to contentPane
                add(plantSelectLabel);
                add(rdoBtnPlant);
                add(rdoBtnFlower);
                add(rdoBtnWeed);
                add(rdoBtnHerb);
                add(rdoBtnFungus);
                
                rdoBtnPlant.addActionListener(new RadioHandler());
                rdoBtnFlower.addActionListener(new RadioHandler());
                rdoBtnHerb.addActionListener(new RadioHandler());
                rdoBtnWeed.addActionListener(new RadioHandler());
                rdoBtnFungus.addActionListener(new RadioHandler());
                JLabel clrLabel = new JLabel("Enter Color:");
                
                JTextField txtColor = new JTextField("",15);
                plantColor = txtColor.getText();
                JLabel plantIdentity = new JLabel("Enter Name:");
                
                JTextField txtName = new JTextField("",15);
                JLabel idLabel = new JLabel("Enter ID:");
                
                JTextField txtPlantID = new JTextField("", 15);
                plantID = txtPlantID.getText();
                plantName = txtName.getText();
                plantPanel1.add(clrLabel);
                plantPanel1.add(txtColor);
                plantPanel1.add(plantIdentity);
                plantPanel1.add(txtName);
                plantPanel1.add(idLabel);
                plantPanel1.add(txtPlantID);
                add(plantPanel1);
                
                //Create CheckBoxes for Boolean value representations (0 and 1 / True or False)
                JLabel traits = new JLabel("Traits:");
                isPoisonous = new JCheckBox("Poisonous");
                isEdible = new JCheckBox("Edible");
                isMedicinal = new JCheckBox("Medicinal");
                isNicelyFlavored = new JCheckBox("Nice Flavor");
                isSeasonal = new JCheckBox("Seasonal");
                hasThorns = new JCheckBox("Thorny");
                smellsNice = new JCheckBox("Smells Nice");
                plantPanel2.add(traits);
                plantPanel2.add(isPoisonous);
                plantPanel2.add(isEdible);
                plantPanel2.add(isMedicinal);
                plantPanel2.add(isNicelyFlavored);
                plantPanel2.add(isSeasonal);
                plantPanel2.add(hasThorns);
                plantPanel2.add(smellsNice);
                
                add(plantPanel2);
                
               
                
                btnAdd = new JButton("Add");
                btnRemove = new JButton("Remove");
                btnSort = new JButton("Sort");
                btnFilter = new JButton("Filter");
                btnClear = new JButton("Clear");
                btnAdd.addActionListener(new ButtonHandlerAdd());
                plantPanel3.add(btnAdd);
                plantPanel3.add(btnRemove);
                plantPanel3.add(btnSort);
                plantPanel3.add(btnFilter);
                plantPanel3.add(btnClear);
                plantSummary = new JTextArea();
                plantSummary.setBackground(Color.WHITE);
                plantSummary.setFont(new Font("Verdana",Font.PLAIN, 20));
                plantSummary.setSize(800,400);
                plantSummary.setEditable(false);
                plantSummary.setVisible(true);
                
               
                scrollPane = new JScrollPane(plantSummary);
                plantPanel4.add(scrollPane);
                plantPanel4.add(plantSummary);
                add(plantPanel3);
                add(plantPanel4);
                setSize(900, 600);
                
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setVisible(true);
                 
            } catch(Exception e) {
                System.out.println(e);
            }
            
        }
        
        public static void main(String[] args) {
            new PlantGUIDriverFinalAssignment();
        }
	


    class RadioHandler implements ActionListener {



        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equals("Plant")) {

                        isPoisonous.setEnabled(false);
                        isEdible.setEnabled(false);
                        isMedicinal.setEnabled(false);
                        isNicelyFlavored.setEnabled(false);
                        isSeasonal.setEnabled(false);
                        hasThorns.setEnabled(false);
                        smellsNice.setEnabled(false);

            } else if (e.getActionCommand().equals("Fungus")) {
                        isPoisonous.setEnabled(true);
                        isEdible.setEnabled(false);
                        isMedicinal.setEnabled(false);
                        isNicelyFlavored.setEnabled(false);
                        isSeasonal.setEnabled(false);
                        hasThorns.setEnabled(false);
                        smellsNice.setEnabled(false);
            } else if (e.getActionCommand().equals("Weed")) {
                        isPoisonous.setEnabled(true);
                        isEdible.setEnabled(true);
                        isMedicinal.setEnabled(true);
                        isNicelyFlavored.setEnabled(false);
                        isSeasonal.setEnabled(false);
                        hasThorns.setEnabled(false);
                        smellsNice.setEnabled(false);
            } else if (e.getActionCommand().equals("Flower")) {
                        hasThorns.setEnabled(true);
                        smellsNice.setEnabled(true);
                        isPoisonous.setEnabled(false);
                        isMedicinal.setEnabled(false);
                        isSeasonal.setEnabled(false);
                        isNicelyFlavored.setEnabled(false);
                        isEdible.setEnabled(false);
            } else if (e.getActionCommand().equals("Herb")) {
                        isPoisonous.setEnabled(false);
                        isEdible.setEnabled(false);
                        isMedicinal.setEnabled(true);
                        isNicelyFlavored.setEnabled(true);
                        isSeasonal.setEnabled(true);
                        hasThorns.setEnabled(false);
                        smellsNice.setEnabled(false);
            }
        }

    }
    
    class ButtonHandlerAdd implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Add Button Clicked!");
            if (e.getActionCommand().equals("Add") && rdoBtnPlant.isSelected()) {
                Plant newPlant = new Plant(plantColor, plantID, plantName);
                plantSummary.append(newPlant.toString() + "\n");
            }
        }
    }
}



What am I doing wrong? Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: New to Swing, my JTextArea is not showing up

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3643
  • View blog
  • Posts: 16,753
  • Joined: 20-September 08

Re: New to Swing, my JTextArea is not showing up

Posted 10 December 2016 - 04:09 AM

Try

plantSummary.setPreferredSize(new Dimension(200, 60));


though it would be better laid out in a different way
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1