The Artima Developer Community
Sponsored Link

Java Answers Forum
How to repaint a panel

2 replies on 1 page. Most recent reply: Dec 17, 2004 3:43 AM by mausam

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Tienshan

Posts: 7
Nickname: tienshan
Registered: Oct, 2004

How to repaint a panel Posted: Oct 20, 2004 9:03 AM
Reply to this message Reply
Advertisement

Hi All!


I am trying to display a few textFields in an AWT applet. The number of textFields I need to set is variable. I want to repaint a portion of an applet (the panel in which these textFields are placed) when a button is clicked.


In the code below, I added a textField array in panel 1(p1), initialized with record size (no_of_rec), which I know at the beginning (no_of_rec = 2 at INDEX = 0).


When I click "Next Item" button, I want to show 4 textFields (no_of_rec = 4 at INDEX = 1), with the values C, D, E, F.


One more click (INDEX = 2) should show one textField only, with the value X.


The codes for the textFields are in a method(addDataPanel), so that I can call it when I click "Next Item" button. A system.out check shows that 4 textFields are created when INDEX = 1, and so on, but with calling repaint() inside the button's actionPerformed method, I am unable to update the panel (p1) dynamically.


When I minimize the window with [-] button, or resize by dragging the applet with mouse, I can see four textFields with the items A, B, E, F. It should have been C, D, E, F. My second click does not change anything.


Could someone please help?


Thank you.

Tienshan


Here is my code:


import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class test1020 extends Applet{
int NO_OF_REC = 0;
int INDEX = 0;
TextField[] txtDataRowDisplay;
String[] data;
Panel p1;
GridBagConstraints c1;
Color pnlBgColor = new Color(144, 196, 222);

public void init() {
data = getData(INDEX);
NO_OF_REC = data.length;

//main Panel
Panel pMain = new Panel();
pMain.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
pMain.setBackground(pnlBgColor);

//p0
Panel p0 = new Panel();
p0.setLayout(new GridBagLayout());
GridBagConstraints c0 = new GridBagConstraints();
c0.fill = GridBagConstraints.HORIZONTAL;
p0.setBackground(pnlBgColor);

//p1
p1 = new Panel();
p1.setLayout(new GridBagLayout());
c1 = new GridBagConstraints();


Button btnNext = new Button("Next Item");
c0.insets = new Insets(0, 0, 10, 0);
c0.gridx = 3;
c0.gridy = 0;
p0.add(btnNext, c0);

btnNext.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e){
INDEX++;
data = getData(INDEX);
NO_OF_REC = data.length;
addDataPanel(p1, c1);
display(data);
p1.repaint();

}
});

//call a method to add data row(s)
addDataPanel(p1, c1);

//add p0 and p1 to pMain;
c.gridx = 0;
c.gridy = 0;
pMain.add(p0, c);

c.gridx = 0;
c.gridy = 1;
pMain.add(p1, c);

add(pMain);
this.setBackground(pnlBgColor);
display(data);

}//init

private String[] getData(int index){

if(index == 0){
String[] names = { "A", "B" };
return names;
}
else if (index==1) {
String[] names = { "C", "D", "E", "F" };
return names;
}

else {
String[] names = { "X" };
return names;
}
}


private void addDataPanel(Panel p1, GridBagConstraints c1){
txtDataRowDisplay = new TextField[NO_OF_REC];

System.out.println("txtDataRowDisplay.length: " + txtDataRowDisplay.length);

//initialize fields
for(int i = 0; i < NO_OF_REC; i++){
txtDataRowDisplay[i] = new TextField(4);
}

int cnt = 0;
for(int m = 0; m < txtDataRowDisplay.length; m++ ){
c1.gridx = 0;
c1.gridy = m + cnt;
p1.add(txtDataRowDisplay[m], c1);
cnt++;
}
}

private void display(String[] data){
for(int i = 0; i < data.length; i++){
txtDataRowDisplay[i].setText(data[i]);
}
}

}


Tienshan

Posts: 7
Nickname: tienshan
Registered: Oct, 2004

Re: How to repaint a panel Posted: Oct 20, 2004 9:08 AM
Reply to this message Reply
I am sorry about the formatting. Probably shouldn't have put
tags. Here is another try.

I am trying to display a few textFields in an AWT applet. The number of textFields I need to set is variable. I want to repaint a portion of an applet (the panel in which these textFields are placed) when a button is clicked.

In the code below, I added a textField array in panel 1(p1), initialized with record size (no_of_rec), which I know at the beginning (no_of_rec = 2 at INDEX = 0).

When I click "Next Item" button, I want to show 4 textFields (no_of_rec = 4 at INDEX = 1), with the values C, D, E, F.

One more click (INDEX = 2) should show one textField only, with the value X.

The codes for the textFields are in a method(addDataPanel), so that I can call it when I click "Next Item" button. A system.out check shows that 4 textFields are created when INDEX = 1, and so on, but with calling repaint() inside the button's actionPerformed method, I am unable to update the panel (p1) dynamically.

When I minimize the window with [-] button, or resize by dragging the applet with mouse, I can see four textFields with the items A, B, E, F. It should have been C, D, E, F. My second click does not change anything.

Could someone please help?

Thank you.

Tienshan

Here is my code:

mausam

Posts: 243
Nickname: mausam
Registered: Sep, 2003

Re: How to repaint a panel Posted: Dec 17, 2004 3:43 AM
Reply to this message Reply
any one of these methods may help you

validate

public void validate() Ensures that this component has a valid layout. This method is primarily intended to operate on instances of Container.

Since: JDK1.0

See Also:invalidate(), doLayout(), LayoutManager, Container.validate()

Flat View: This topic has 2 replies on 1 page
Topic: java combobox Previous Topic   Next Topic Topic: changing text color in JTextArea

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use