1

My JText Area is not working. You will see if you run this code. The textArea keeps going to different places even though I have set a definitive spot that it should be. The code for the textArea is line 57.

What I exactly need is for the testArea to maintain a fixed position, which is explained in line 57.

Note: 'pain' is a JPanel containing the textArea

public Graphics g;

public void paint(Graphics g) {

    Graphics2D projectFinder = (Graphics2D) g;
    projectFinder.setColor(Color.GRAY);
    projectFinder.fillRect(0, 0, 1000, 50);
    projectFinder.setStroke(new BasicStroke(100));

    Graphics2D OutPut = (Graphics2D) g;
    OutPut.setColor(Color.LIGHT_GRAY);
    OutPut.fillRect(553, 60, 535, 670);
    OutPut.drawString("Project Input (Your Code)", 30, 90);

    Graphics2D console = (Graphics2D) g;
    console.setColor(Color.WHITE);
    console.fillRect(563, 620, 515, 100);

    console.setColor(Color.BLACK);
    console.drawString("Console.ChemLOG", 570, 640);

}

public static void main(String[] args) {

    JPanel pnlButton = new JPanel();
    pnlButton.setBounds(800, 100, 100, 100);

    JButton button = new JButton("Add Project");
    button.setBounds(1000, 0, 100, 50);

    JFrame frame = new JFrame("GenoTECH IDE 1.0.0");
    frame.add(new MainScreen());
    frame.setSize(1100, 800);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(button);
    button.addActionListener(new NewProject());

    // TextBox

    JPanel pain = new JPanel();
    JTextArea area = new JTextArea();
    area.setSize(535, 670);
    area.setLocation(0, 0);

    pain.setLocation(10, 60);
    pain.setSize(area.getSize());

    pain.add(area);

    frame.add(pain);
}

// add project button
static class NewProject implements ActionListener {
    public void actionPerformed(ActionEvent e) {

        JButton buttonOK = new JButton("Ok");
        buttonOK.setBounds(0, 233, 150, 45);

        JButton buttonCn = new JButton("Cancel");
        buttonCn.setBounds(150, 233, 150, 45);

        JFrame frame2 = new JFrame("New Project");
        frame2.setSize(300, 300);
        frame2.setVisible(true);
        frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame2.add(buttonOK);
        frame2.add(buttonCn);
        buttonCn.addActionListener(new buttonCN());

    }

    static class buttonCN implements ActionListener {
        public void actionPerformed(ActionEvent e) {

            System.exit(0);

        }
    }
}

}

Ok now for an update for the code, I have got the JTextArea to work without messing up, however the 2D graphics is not getting called for some reason. I am also working on another layout using some of the Java layout formats.

New Code:

private static final long serialVersionUID = 1L;
public Graphics g;

public void paint(Graphics g) {

    Graphics2D projectFinder = (Graphics2D) g;
    projectFinder.setColor(Color.GRAY);
    projectFinder.fillRect(0, 0, 1000, 50);
    projectFinder.setStroke(new BasicStroke(100));

    Graphics2D OutPut = (Graphics2D) g;
    OutPut.setColor(Color.LIGHT_GRAY);
    OutPut.fillRect(553, 60, 535, 670);
    OutPut.drawString("Project Input (Your Code)", 30, 90);

    Graphics2D console = (Graphics2D) g;
    console.setColor(Color.WHITE);
    console.fillRect(563, 620, 515, 100);

    console.setColor(Color.BLACK);
    console.drawString("Console.ChemLOG", 570, 640);

}

public static void main(String[] args) {        
    JButton button = new JButton("Add Project");
    button.setBounds(1000, 0, 100, 50);
    //button.setSize(5,5);
    //button.setLocation(1,1);

    JFrame frame = new JFrame("GenoTECH IDE 1.0.0");
    frame.setLayout(null);
    frame.add(new MainScreen());
    frame.setSize(1100, 800);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(button);
    button.addActionListener(new NewProject());

    // Thing to Change
    JTextArea area = new JTextArea();
    area.setSize(535, 670);
    area.setLocation(10, 60);

    frame.add(area);
}


// add project button
static class NewProject implements ActionListener {
    public void actionPerformed(ActionEvent e) {

        JButton buttonOK = new JButton("Ok");
        buttonOK.setBounds(0, 233, 150, 45);

        JButton buttonCn = new JButton("Cancel");
        buttonCn.setBounds(150, 233, 150, 45);

        JFrame frame2 = new JFrame("New Project");
        frame2.setSize(300, 300);
        frame2.setVisible(true);
        frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame2.add(buttonOK);
        frame2.add(buttonCn);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Does it appear in different places each time you run it (i.e. does it happen as soon as you run the program) or does something have to happen for this behaviour to occur? – user1849060 Jan 25 '15 at 19:38
  • Every time you type something, the JPanel changes size or location or both. – A. Radek Martinez Jan 25 '15 at 19:42
  • 4
    Use layout managers instead of hardcoding bounds and dimensions. http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html – JB Nizet Jan 25 '15 at 19:43
  • Ok I will look into that – A. Radek Martinez Jan 25 '15 at 19:44
  • 2
    While null layouts and `setBounds()` might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one. – Hovercraft Full Of Eels Jan 25 '15 at 19:56
  • 4
    "Swing programs should override `paintComponent()` instead of overriding `paint()`."—[*Painting in AWT and Swing: The Paint Methods*](http://www.oracle.com/technetwork/java/painting-140037.html#callbacks). – trashgod Jan 25 '15 at 20:38
  • Ok. Good to know, but can anything be done with the code as it is ? – A. Radek Martinez Jan 25 '15 at 20:39
  • Replying to trashgod's comment, it does not work. I thought of doing that before I posted my question. – A. Radek Martinez Jan 25 '15 at 20:42
  • 2
    But again the solution has been given to you already: don't setBounds, don't use null layouts and absolute positioning. Instead use the layout managers in a smart fashion, and if you follow these recommendations, your code will be fruitful and you will prosper. – Hovercraft Full Of Eels Jan 25 '15 at 21:53
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete Verifiable Example) or [SSCCE](http://www.sscce.org/) (Short, Self Contained, Correct Example). Voting to close this.. – Andrew Thompson Jan 26 '15 at 01:11
  • For what I was trying to do, I do not think Java is the best language to use. However, it should work regardless...? – A. Radek Martinez Sep 03 '15 at 17:01

1 Answers1

2

I'm not sure what you're trying to do. Here's a runnable version of your code with Swing layouts.

Here are the changes I made.

  1. I started the application with a call to the SwingUtilities invokeLater method. This ensures that the Swing components are created and modified on the Event Dispatch thread (EDT).

  2. The order that you execute the JFrame methods is very important. I fixed the order.

  3. You draw on a JPanel using the paintComponent method. You must make the call to super before your drawing code.

  4. You set the size of the drawing panel only. You use the JFrame pack method to size the rest of the Swing components.

  5. As others have mentioned, you use Swing layouts to lay out the Swing components.

.

package com.ggl.testing;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class GenoTech implements Runnable {

    @Override
    public void run() {
        JFrame frame = new JFrame("GenoTECH IDE 1.0.0");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS));

        mainPanel.add(new DrawingPanel());

        JPanel pnlButton = new JPanel();

        JButton button = new JButton("Add Project");
        button.addActionListener(new NewProjectActionListener());
        pnlButton.add(button);

        mainPanel.add(pnlButton);

        JPanel pain = new JPanel();
        JTextArea area = new JTextArea();
        pain.add(area);

        mainPanel.add(pain);

        frame.add(mainPanel);

        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new GenoTech());
    }

    public class DrawingPanel extends JPanel {

        private static final long serialVersionUID = -5718559965267054844L;

        public DrawingPanel() {
            this.setPreferredSize(new Dimension(700, 300));
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g;
            g2d.setColor(Color.GRAY);
            g2d.fillRect(0, 0, 700, 50);
            g2d.setStroke(new BasicStroke(100));

            g2d.setColor(Color.LIGHT_GRAY);
            g2d.fillRect(553, 60, 535, 300);
            g2d.drawString("Project Input (Your Code)", 30, 90);

            g2d.setColor(Color.WHITE);
            g2d.fillRect(563, 620, 515, 100);

            g2d.setColor(Color.BLACK);
            g2d.drawString("Console.ChemLOG", 570, 640);
        }

    }

    // add project button
    public class NewProjectActionListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {

            JFrame frame2 = new JFrame("New Project");
            frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

            JPanel mainPanel = new JPanel();

            JButton buttonOK = new JButton("Ok");
            mainPanel.add(buttonOK);

            JButton buttonCn = new JButton("Cancel");
            buttonCn.addActionListener(new ButtonCNActionListener());
            mainPanel.add(buttonCn);

            frame2.add(mainPanel);
            frame2.pack();
            frame2.setLocationByPlatform(true);
            frame2.setVisible(true);
        }

        public class ButtonCNActionListener implements ActionListener {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        }
    }

}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • Ok I understand what you did, however I do not think it quiet solved the problem. I used your code the clean up some parts of mine, but my main problem was that the JTextArea, when you type into it, it would mess up and go to the top of the screen. Your code did not exactly fix that, but thanks for the recommendations. I am going to try to use the layouts to fix this problem, I am working on it. – A. Radek Martinez Jan 26 '15 at 00:40