Question

I never get "paint" written to my command line window when I use Eclipse and Run->cmd to run the program. It works fine if I run System.out.print() from paintComponent in another program. Someone who can help?

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

public class GUI extends JPanel implements KeyListener, ActionListener
 {
  private static final long serialVersionUID = 1L;
  JFrame frmMain = new JFrame("Kodning");
  JTextField text = new JTextField();
  JPanel pan = new JPanel();
  static char bokstav;
  static int x=10, y=80;
  boolean convert = false;
  String s;
  Timer t = new Timer(10, this);
  public static void main(String[] args)
   {

    @SuppressWarnings("unused")
    GUI g = new GUI();

   }

  public GUI()
   {
    frmMain.setSize(600, 120);
    frmMain.setLayout(new GridLayout(2, 1));
    frmMain.addWindowListener(hornStang());
    frmMain.add(text);
    frmMain.add(pan);
    frmMain.setFocusable(true);
    frmMain.setVisible(true);
    frmMain.addKeyListener(this);
    text.addKeyListener(this);
    pan.addKeyListener(this);
    t.start();
   }
  private static WindowAdapter hornStang() 
   {
    return new WindowAdapter() 
     {
      public void windowClosing(WindowEvent e) 
       {
        System.exit(0);
       }
     };
   }
  public void keyPressed(KeyEvent e)
   {
    if(e.getKeyCode()== KeyEvent.VK_ENTER)
     {
      System.out.println("dechifrera");
      repaint();
      deshiffrera(text.getText());
     }
   }
  public void keyReleased(KeyEvent arg0){}
  public void keyTyped(KeyEvent arg0){}
  public void deshiffrera(String s) 
   {
    s = this.s;
    repaint();
   }
  @override
  public void paintComponent(Graphics g)
   {
    System.out.println("paint");
    for(int i=0;i<s.length();i++)
     {
      bokstav = s.charAt(i);
      switch (bokstav)
       {
        case 'a':nere(g); hoger(g); prick(g, 0); break;
        //en massa case
        default:break;
       }
      x=x+12;
     }
   }
  @Override
  public void actionPerformed(ActionEvent e)
   {
    repaint();
   }
 }
Was it helpful?

Solution

The component must be added to a visible window/frame/component for it's paintComponent to be called.
GUI is only added as a KeyListener but is neither added to the JFrame, nor any other visible component in the code above. There is no reason for calling paintComponent since the component is not being displayed at all.

OTHER TIPS

There are a number of issues with your code:

  1. Your GUI panel is not in the frame (shouldn't it be added instead of pan?)
  2. String s is uninitialized, which causes a NullPointerException
  3. paint should be overridden instead of paintComponents
  4. paint should not change the state of the component, because it can be called any time.
  5. etc...

You probably miss the output of "System.out.println("paint");" ?

GUI-Apps under Windows cant write to the console (they dont have a console, because it would suck if every GUI-App would also open a black window).

There are two java-interpreters under windows: "javaw.exe" which is a GUI-App and silently discards any System.out-writes. And "java.exe" which is a console-app and allows writing to the console. Try to start your program with "java.exe"

I use this with AWT (not 100% sure whether it's working in Swing too...)

Graphics g = _yourcomponent_.getGraphics();
if (g != null) {
    _yourcomponent_.paint(g);
    // below the estimated code for Swing:
    _yourcomponent_.paintComponent(g);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top