aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Question about a simple GUI application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Question about a simple GUI application" Watch "Question about a simple GUI application" New topic
Author

Question about a simple GUI application

James Tan
Greenhorn

Joined: Apr 23, 2002
Posts: 13
I build a simple GUI, a JFrame, which contain a JTextField on the top, and a MyPanel(extends from JPanel) below the textFiled, and a JLabel on the bottom.
I use a Timer to repaint the MyPanel to draw a line in random position each second, but when I run the application, there are two textfield appear, one on the top of the frame, the other on the top of panel, and the last one does not always appears the whole content. when I do some input in the first text field, the same content display in the second.
I don'y know why.


James Tan<br />SUN Certificated Programmer
Rene Liebmann
Ranch Hand

Joined: May 02, 2002
Posts: 196
When your GUI is so simple, then you should post the code. Then it is possible to help you. I'm shure, you made a mistake. Where do you draw your line? Usually it is better to take an own class, which overwrites the function paintComponent.
Chantal Ackermann
Ranch Hand

Joined: Sep 28, 2000
Posts: 508
which Layout are you using? do you add one single JTextField twice?
James Tan
Greenhorn

Joined: Apr 23, 2002
Posts: 13
Here is my code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SimuScreenSaver extends JFrame
{
public int maxPaintLimit=100;
private JLabel aLabel;
private ScreenSaverPanel aPanel;

class TextFieldAction implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();

try{
int i = new Integer(s).intValue();
maxPaintLimit = i;
aLabel.setText(String.valueOf(maxPaintLimit));
}catch(NumberFormatException ex){
aLabel.setText("Invalid Input");
}
}
}

public SimuScreenSaver()
{
super("Screen Saver Simulator");

Container c = getContentPane();

JTextField atextField = new JTextField("100");
atextField.addActionListener( new TextFieldAction() );
c.add(atextField,BorderLayout.NORTH);


aLabel = new JLabel("Label");
c.add(aLabel,BorderLayout.SOUTH);

ScreenSaverPanel aPanel = new ScreenSaverPanel(this);
c.add(aPanel);


setSize(800,600);
//setBackground(Color.black);
show();
}


public static void main(String [] args)
{
SimuScreenSaver app = new SimuScreenSaver();
app.addWindowListener(
new WindowAdapter() {
public void windowClosing( WindowEvent e )
{
System.exit(0);
}
}
);
}
}
class ScreenSaverPanel extends JPanel implements ActionListener
{
private SimuScreenSaver aFrame;
private int paintTimes;

ScreenSaverPanel(SimuScreenSaver aFrame)
{
this.aFrame = aFrame;
Timer timer = new Timer(500,this);
timer.start();
}


public void actionPerformed(ActionEvent e)
{
repaint();
}

public void paint(Graphics g)
{
int startX,startY,endX,endY;

if( ++paintTimes > aFrame.maxPaintLimit )
{
paintTimes = 0;
//g.setColor(Color.black);
g.fillRect(0,0,1024,768);
return;
}
g.setColor(new Color((int)(256*Math.random()),
(int)(256*Math.random()),
(int)(256*Math.random()))
);
startX = (int)(1024*Math.random());
startY = (int)(768*Math.random());
endX = (int)(1024*Math.random());
endY = (int)(768*Math.random());
g.drawLine(startX,startY,endX,endY);
}
}
James Tan
Greenhorn

Joined: Apr 23, 2002
Posts: 13
to Chantal Ackermann:
I use the default layout for JFrame and MyPanel.
I add JTextField only once. I post code.
Brian Duff
Greenhorn

Joined: Aug 16, 2001
Posts: 22
Hi,
This is happening because your ScreenSaverPanel is opaque (JPanel is opaque by default). Part of the contract of the paint() method of any opaque JComponent is that it must fully paint the area covered by the component. See the javadoc for JComponent.isOpaque() for details.
The reason you are seeing two text fields is because of repaint glitches due to the fact that paint() is not honoring this contract.
You can solve it by either calling setOpaque(false) in ScreenSaverPanel's constructor, or by adding the following line of code at the start of your paint() method:
g.clearRect( 0, 0, getWidth(), getHeight() );
Brian
James Tan
Greenhorn

Joined: Apr 23, 2002
Posts: 13
Thank you, Brian
But it brings another flaw, only one line can be diaplayed. My original idea is to display all the lines until the number get to the limit.
I use Panel instead, and overriding update method,
only call paint in update, it seems well. But how to deal with swing in such requirment?
Thanks again.
Brian Duff
Greenhorn

Joined: Aug 16, 2001
Posts: 22
Try painting the lines to an off screen buffer, then blitting the buffer to the panel in the paint() method.
You can use the BufferedImage class to do this. Let me know if you need more info.
Brian
James Tan
Greenhorn

Joined: Apr 23, 2002
Posts: 13
Thanks Brian and others helping me.
I create a BufferedImage with the size of ScreenSaverPanel, do all the drawing on this BufferedImage, it works well. However, if I change the size of the frame, BufferedImage do not change its size unless I create a new one which will lose all the drawings.
If someone here have any good idea, please share with me. I am a greenbron.
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

If you make a new Buffered Image, paint the contents of the old one onto it before you get rid of the old image...


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
James Tan
Greenhorn

Joined: Apr 23, 2002
Posts: 13
I see.
Is there the only way to do this job?
Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about a simple GUI application
 
Similar Threads
Scrolling within a JTabbedPane
simple JTabbedPane question
Classes
adding and removing Swing Component
Using custom panel in NetBeans GUI builder