Paint and repaint

Hi I've got a simple question how to you repaint somthing that's not a Component?

When I get the string I want to paint it on screen but I need to repaint first, how?

import java.awt.*;

import java.io.*;

publicclass myList

{

public String[] name =new String[1000];

int Counter = 0;

publicvoid getFile(String str)

{

name[Counter] = str;

Counter++;

//Want repaint here

}

publicvoid paint(Graphics g)

{

name[0] ="Hej";

for(int i = 0;i < name.length; i++)

{

if(name[i] !=null)

g.drawString(name[i],10,50 + 20*i);

}

}

}

[1444 byte] By [Consideratea] at [2007-11-27 6:31:39]
# 1

Something like this, maybe?

class MyPanel extends JPanel

{

private myList imGoingToDrawThis;

public MyPanel()

{

imGoingToDrawThis = new myList();

}

public void paintComponent(Graphics g)

{

imGoingToDrawThis.paint(g);

}

}

CaptainMorgan08a at 2007-7-12 17:56:37 > top of Java-index,Java Essentials,Java Programming...
# 2
I've got the panel and the frame all I need now is to repaint the void paint() with the paintComponent in the panel. I can repaint the panel but not the list and that is what I need to do right now.
Consideratea at 2007-7-12 17:56:37 > top of Java-index,Java Essentials,Java Programming...
# 3
Call repaint? Why are you doing your own painting just for a list of strings? Is JTextArea broken?
Hippolytea at 2007-7-12 17:56:37 > top of Java-index,Java Essentials,Java Programming...
# 4

> Call repaint? Why are you doing your own painting

> just for a list of strings? Is JTextArea broken?

No I want to paint using paint because I need it for an other part of the program where I click on what I've painted. Plus with paint I can change fonts and it's just better in any way I know. So I'll stick to paint

Consideratea at 2007-7-12 17:56:37 > top of Java-index,Java Essentials,Java Programming...
# 5

In the future, Swing related questions should be posted in the Swing forum.

a) your class MyList should extend JComponent and then you override the paintComponents() method not the paint() method.

b) then you add your component to your panel

c) if you change the values in the list then you simply repaint() your MyList component.

camickra at 2007-7-12 17:56:37 > top of Java-index,Java Essentials,Java Programming...
# 6

I do not belive this is swing related since I want to get the method of repainting the AWT version of paint.

Once again I'll show some code (stripped down for you)

import java.io.*;

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.filechooser.*;

import java.net.*;

public class myPanel

{

JPanel panel;

JButton openButton;

myList ml = new myList();

public myPanel()

{

panel = new JPanel()

{

public void paintComponent(Graphics g)

{

super.paintComponent(g);

ml.paint(g);

}

};

openButton.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent e)

{

//the file variable I get from a JFileChooser

//This is where a real application would open the file.

ml.getFile(file.getName());

panel.repaint();

//I maybe want to repaint ml.paint() here

}

});

panel.add(stopButton);

panel.add(openButton);

panel.add(saveButton);

}

}

[code]

[code]

import java.awt.*;

import java.io.*;

public class myList

{

public String[] name = new String[1000];

int Counter = 0;

public void getFile(String str)

{

name[Counter] = str;

Counter++;

//Want a repaint here

}

public void paint(Graphics g)

{

for(int i = 0;i < name.length; i++)

{

if(name[i] != null)

g.drawString(name[i],10,50 + 20*i);

//Or here

}

}

}

Consideratea at 2007-7-12 17:56:37 > top of Java-index,Java Essentials,Java Programming...
# 7
I do not know why but I didn't anything and it just worked a minute ago. Thanks everyone who tried to help out with a problem that wasn't a problem.//All regardsConsiderate
Consideratea at 2007-7-12 17:56:37 > top of Java-index,Java Essentials,Java Programming...