Hi all, I have read stuff on this but still dont get it. I need to draw
a box and a circle on two different tab panels.
Here is what I have so far somthing is not in the right place.
I have looked at the tutorial thats on Sun's site but didnt help.
I'm new to Java and am trying to learn but somtimes I just dont get it.
what I was trying to do was when you select another tab the oval or
square is drawn.
If you can help or point me in the right direction I would appricaiate
it.
Allen
public class Frame1 extends JFrame
{
private JTabbedPane jTabbedPane1 = new JTabbedPane();
private Graphics g;
private JTabbedPane jTabbedPane2 = new JTabbedPane();
private JTabbedPane jTabbedPane3 = new JTabbedPane();
public Frame1()
{
try
{
jbInit();
}
catch(Exception e)
{
e.printStackTrace();
}
}
private void jbInit() throws Exception
{
this.getContentPane().setLayout(null);
this.setSize(new Dimension(400, 300));
jTabbedPane1.setBounds(new Rectangle(55, 30, 290, 225));
jTabbedPane1.addTab("jTabbedPane2", jTabbedPane2);
jTabbedPane1.addTab("jTabbedPane3", jTabbedPane3);
// Register a change listener
jTabbedPane1.addChangeListener(new ChangeListener() {
// This method is called whenever the selected tab changes
public void stateChanged(ChangeEvent evt) {
JTabbedPane jTabbedPane1 = (JTabbedPane)evt.getSource();
// Get current tab
int sel = jTabbedPane1.getSelectedIndex();
if(sel == 0){
jTabbedPanel.g.drawOval(300,120,100,75);
}
g.drawRect(72,160,200,35);
}
});
Arnaud Berger - 06 Apr 2005 08:05 GMT
Hi,
A few questions :
- why do you need several JTabbedPane ?
- on which component should the oval or square be drawn ?
- what is jTabbedPanel in your example (this object doesn't exist) ?
- where is the Graphics object set ?
Some advice :
-provide a runnable example if possible
-custom painting in a component should occur in its paintComponent(Graphics
g) method
-trying to get a Graphics object from a component, storing it and drawing on
it in other places than
paint...() methods won't succeed
Regards,
Arnaud
> Hi all, I have read stuff on this but still dont get it. I need to draw
> a box and a circle on two different tab panels.
[quoted text clipped - 50 lines]
> }
> });
Johan Kütt - 06 Apr 2005 21:38 GMT
there are several ways for doing this:
====================================================
first:
---------------------
main class
.....
TabbedPane tabs = new TabbedPane();
tabs.addTab( "Circle", new Tab1Panel() );
tabbs.addTab( "Rectangle", new Tab2Panel() );
.....
---------------------
---------------------
class Tab1Panel extends JPanel {
public Tab1Panel() {}
public void paintComponent( Graphics g ) {
// draw the circle
}
}
---------------------
---------------------
class Tab2Panel extends JPanel() {
public Tab2Panel() {}
public void paintComponent( Graphics g ) {
// draw the rectangle
}
}
---------------------
=================================================
second:
JTabbedPane tabs = new JTabbedPane();
JPanel tab1 = new JPanel();
JPanel tab2 = new JPanel();
Graphics g1 = tab1.getGraphics();
Graphics g2 = tab2.getGraphics();
g1.drawCircle(...);
g2.drawRectangle(...);
tabs.addTab( "Circle", tab1 );
tabs.addTab( "Rectangle", tab2 );
=================================================
there are certaily other ways for solving this problem
johan
> Hi all, I have read stuff on this but still dont get it. I need to draw
> a box and a circle on two different tab panels.
[quoted text clipped - 7 lines]
>
> Allen