• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question about a simple GUI application

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 508
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
which Layout are you using? do you add one single JTextField twice?
 
James Tan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
to Chantal Ackermann:
I use the default layout for JFrame and MyPanel.
I add JTextField only once. I post code.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you make a new Buffered Image, paint the contents of the old one onto it before you get rid of the old image...
 
James Tan
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see.
Is there the only way to do this job?
Thank you.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic