0

I'm working in java using Jpanel and my work is compiling fine however is showing no output. hopefully, someone could tell me why this is. I'm using jscrollpane and I'm calling it at the end idk if it's something to do with the listener or what.

FileDemoPanel.java

package Tutoiral03Task01;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

  public class FileDemoPanel extends JPanel implements ActionListener {

   public void actionPerformed(ActionEvent e) {

    JButton openBtn, saveBtn;
    JTextArea  workTa;

    openBtn = new JButton ("Open");
    openBtn.setEnabled (false);
    openBtn.setMnemonic('g');
    openBtn.setToolTipText("open button");

    setLayout(new BorderLayout());


    saveBtn = new JButton ("Save");
    saveBtn.setEnabled (false);
    saveBtn.setMnemonic('f');
    saveBtn.setToolTipText("Save button");

    JTextArea logTA = new JTextArea (5, 100);
    logTA.setEditable(false);
    logTA.setBackground(Color.lightGray);
    logTA.setMargin(new Insets(5,5,5,5));
    JScrollPane logScrollPane = new JScrollPane(logTA);

    add(logScrollPane);

    }

}

FileDemo.java

package Tutoiral03Task01;

import javax.swing.*;

public class FileDemo {
   public static void main (String[] args){
       JFrame frame = new JFrame("Working with files");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       frame.add(new FileDemoPanel());
       frame.pack();
       frame.setVisible(true);
   }
}  
camickr
  • 321,443
  • 19
  • 166
  • 288
Angus.J
  • 1
  • 1
  • 2
    Why is all the code to set up the `FileDemoPanel` in the `actionPerformed` method? This method will never be called in your current scenario. You would get the expected result if you move the code you currently have in the `actionPerformed` method to the constructor of `FileDemoPanel`. – maloomeister Apr 07 '21 at 12:00
  • its part of the assignment i gotta do. it has to be setup like this – Angus.J Apr 07 '21 at 12:04
  • 1
    Well, then you need something that actually calls that method. It could be that you are supposed to add a `JButton` (for example) to your `JFrame`, which then has the `FileDemoPanel` object added as an `ActionListener`. Do you know what an `ActionListener` is and how it works? Which part of this code did you actually write yourself? – maloomeister Apr 07 '21 at 12:06
  • i was given the filedemo.java and the imports for filedemopanel. the rest I wrote myself – Angus.J Apr 07 '21 at 12:25
  • Well in that case, you should consider my first comment again. Move the code currently in the `actionPerformed` method, to the constructor of `FileDemoPanel`. – maloomeister Apr 07 '21 at 12:33
  • okay thanks i got it work – Angus.J Apr 07 '21 at 13:07

1 Answers1

0

The problem is that you create all the buttons and others in a actionperformed method. This is wrong, because that is used as a ButtonListener, so if you dont press a button nothing will happened. We use to write the GUI frame in the constructor of the class.Then we create an object type of the GUI class. So i think i fixed it and i did some extra changes to make the program more simple. The step i didnt do is to add a ButtonListener, so Buttons do nothing. i wish it will helps you.

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class FileDemoPanel extends JFrame {
private   JPanel panel = new JPanel();
private   JButton openBtn = new JButton("Open");
private   JButton saveBtn =  new JButton ("Save");
private   JTextArea  workTa;

public  FileDemoPanel(){

    openBtn.setEnabled (false);
    openBtn.setMnemonic('g');
    openBtn.setToolTipText("open button");

    setLayout(new BorderLayout());



    saveBtn.setEnabled (false);
    saveBtn.setMnemonic('f');
    saveBtn.setToolTipText("Save button");

JTextArea logTA = new JTextArea (5, 100);
    logTA.setEditable(false);
    logTA.setBackground(Color.lightGray);
    logTA.setMargin(new Insets(5,5,5,5));
JScrollPane logScrollPane = new JScrollPane(logTA);

panel.add(openBtn);
panel.add(saveBtn);
panel.add(logTA);
panel.add(logScrollPane);

  this.setContentPane(panel);

  this.setVisible(true);
  this.setResizable(true);
  this.setSize(350,150);
  this.setTitle("Κεντρική Σελίδα");
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 }



  

 }

And the mainclass.As you can see is too small.

 public class FileDemo {
 public static void main (String[] args){
 new  FileDemoPanel();
 }
   }
toliosg
  • 46
  • 3