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);
}
}
}
Something like this, maybe?
class MyPanel extends JPanel
{
private myList imGoingToDrawThis;
public MyPanel()
{
imGoingToDrawThis = new myList();
}
public void paintComponent(Graphics g)
{
imGoingToDrawThis.paint(g);
}
}
> 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
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.
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
}
}
}