hi i have an application with 5 jtextpanes which is an array
i would like to know how to be able to draw a blue circle
in the 1st jtextpane tp[0]
in the 2nd jtextpane i would like to draw a red circle... tp[1]....
i have a code that can help us to resolve this problem
but right now with that, im painting all the same shapes
in all the tabs
ty for the help
|
Code:
|
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Color;
import java.*;
import javax.*;
import java.awt.Graphics;
public class Mains extends JFrame
{
private JTabbedPane jtp = new JTabbedPane();
//private Transpa tp[] = new Transpa[5];
private JTextPane tp[] = new JTextPane[5];
private JTextPane textPane;
private int i = 0;
private int index = 0;
public static void main( String[] argv )
{
Mains m = new Mains();
}
public Mains()
{
super("Editor Text Center");
createDocument();
JMenuBar menuBar = new JMenuBar();
setJMenuBar (menuBar);
JMenu file = new JMenu ("File");
JMenuItem newItemFile = new JMenuItem ("New");
newItemFile.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
createDocument();
}
});
JMenuItem ItemFile = new JMenuItem ("test");
ItemFile.addActionListener (new ActionListener()
{
public void actionPerformed (ActionEvent e)
{
int index = jtp.getSelectedIndex();
JTextPane jTPane = tp[index];
System.out.println(jTPane.getText());
}
});
file.add(newItemFile);
file.add(ItemFile);
menuBar.add(file);
setSize(990,735);
pack();
setVisible(true);
}
public void createDocument()
{
tp[i] = new JTextPane();
JTextPane t = tp[i];
t.setText("a"+(i+1));
JScrollPane editorScroll = new JScrollPane(tp[i]);
jtp.add("doc"+(i+1), editorScroll);
getContentPane().add(jtp,BorderLayout.CENTER);
i++;
}
} |
|
Code:
|
import javax.swing.JTabbedPane;
import javax.swing.JTextPane;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
public class Transpa extends JTextPane
{
JTabbedPane jtp;
public Transpa(JTabbedPane jtp)
{
this.jtp = jtp;
//this.setOpaque(false);
this.setBackground(Color.WHITE);
}
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Random r = new Random();
Color c = new Color(r.nextInt(256), r.nextInt(256), r.nextInt(256));
g.setColor(c);
//g.fillRect(0,0,getWidth(),getHeight());
g.fillRect(0,0,50,50);
g.drawOval(50,50,30,40);
super.repaint();
}
} |
some code or advice would be very appreciate it
do i have to do a for to check all the tabs and inside the for do if (tp[i]==... ) ? in the paint component ?