my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Can I use repaint() on a JFrame? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Can I use repaint() on a JFrame?" Watch "Can I use repaint() on a JFrame?" New topic
Author

Can I use repaint() on a JFrame?

Arun Martin
Ranch Hand

Joined: Dec 01, 2000
Posts: 64
Hi,
I have got a main class which is JFrame and other sub class JPanel. Could u please tell me how to repaint() the entire JPanel once a component(JButton)present in the panel is pressed?
with warm regards,
Arun.
Javaoops
Ranch Hand

Joined: Jun 21, 2001
Posts: 57
JPanel.repaint() method in Actionlistener (actionPerformed) or MouseListener method (mouse clicked) added to JButton, it repaints the panel. This method causes a call to this component's update method.

Arun Martin
Ranch Hand

Joined: Dec 01, 2000
Posts: 64
Hi Rajan,
Thanks for the suggestion. I tried out but it didn't serve my purpose. I will tell u what I want.
I have a class MainClass which has got 2 JTabbedPanes which are 2 JPanels ie P1,P2.
In P1,I get some user values thru some JTextFields and insert into the database. Then next Panel P2 is used for modifiying the P1's values.
In P2,the values are retreived from the database and put in a JComboBox which is used for further manipulation.
The problem is that whenever I insert values into P1,the new values don't get immediately updated in P2. I have to quit the application and run the application once again. This defeats the very purpose of my app. I want that P2 should reflect my new values immediately when they are inserted into database thru P1.
The outline pgm code
-------------------
public class MainClass extends JFrame implements ChangeListener
{
JTabbed tp;
MainClass()
{
// Declare 2 Tabbed Panes.
tp.add(p1,new p1());
tp.add(p2,new p2());
// add Listener
// add tabbedpane to the container.
}

// Listener method
{
if (selected index = 0)
new p1();
if (selected index = 1)
new p2();
}
public static void main()
{
}

} // end of MainClass
class p1 extends JPanel implements ActionListener
{
JTextField tf1;
JTextField tf2;

JButton submit;

p1()
{
// declare the textfields,button and add to the
content pane
// add Action Listener
}

actionperformed()
{
// get the text field value
// insert into the database
}
} // end of class p1
class p2 extends JPanel implements ActionListener
{
JComboBox jcbox tf;
JTextField tf1;
JButton submit;

p2()
{
// get the values from the database
// put into the combobox.
// the pblm ie the latest values are not being displayed
}

actionperformed()
{
// get the values from the combo box for processing
}
} // end of class p2
with warm regards,
Arun.

Gregg Bolinger
GenRocket Founder
Ranch Hand

Joined: Jul 11, 2001
Posts: 15299
    
    6

Are you trying to repaint() a specific pane? Or the entire JTabbedPane? repaint() only the JPanel. Also, if that doesn't work, you might try p1.remove(Component) and then adding it back. That worked for a similar situation for me. I know that will probably add quite more code but just giving some suggestions.


GenRocket - Experts at Building Test Data
Javaoops
Ranch Hand

Joined: Jun 21, 2001
Posts: 57
Hi Arun,
your problem is similar to that of static. At the first time when u run the application it queries the values from the database and stores in JCombobox.
Ur problem will not be solved by repainting the component.
Add ChangeListener the JTabbedpane, so that you can query from the Database and update the JComboBox in order to be dynamic when ever you select the Panel2 tab.

Arun Martin
Ranch Hand

Joined: Dec 01, 2000
Posts: 64
Hi Rajan,
Thanks for pointing out that repaint() was not required. I am still confused by ur reply. If u don't mind,please could u make it more lighter in regard to my code. I am enclosing the almost similiar version of my code.
Code:
----
public class AdminTabbedPan extends JFrame implements ChangeListener
{
Container container;
JTabbedPane tp;

public AdminTabbedPan()
{

container= this.getContentPane();
container.setLayout(new FlowLayout());
//container.setLayout(new GridLayout(1,1));
tp = new JTabbedPane();
tp.addChangeListener(this);
tp.addTab("Add New User",new AddUser());
tp.setBackgroundAt(0,Color.white);
tp.addTab("Add New MgmtRep QCRep",new
ModifyUser());
tp.setBackgroundAt(1,Color.white);
container.add(tp);
}
public void stateChanged(ChangeEvent ce)
{
JTabbedPane tbpane;
tp.repaint();
tbpane = (JTabbedPane) ce.getSource();
int selectedIndex = tbpane.getSelectedIndex();
if (selectedIndex == 0)
{
new AddUser();
}
if (selectedIndex == 1)
{
new ModifyUser();
}
}
public static void main(String[] a)
{
AdminTabbedPan ob = new AdminTabbedPan();
}
} // end of class AdminTabbedPan
class AddUser extends JPanel implements ActionListener
{
JPanel MainPanel = new JPanel(new GridLayout
(13,2,6,10));
JButton submit;

public AddUser()
{
// add everything to MainPanel
// then add MainPanel to JPanel
}
public void actionPerformed(ActionEvent evt)
{
Object e = evt.getSource();
if (e==submit)
{
// get the user values and insert it into the
database
}
}
} // end of AddUser
class ModifyUser extends JPanel implements ActionListener
{
JPanel MainPanel = new JPanel(new GridLayout
(13,2,6,10));
JButton submit;
JComboBox combobox;

public ModifyUser()
{
// add everything to MainPanel
// then add MainPanel to JPanel
}
public void actionPerformed(ActionEvent evt)
{
Object e = evt.getSource();
if (e==combobox)
{
// get the user values and insert it into
the database
}

if (e==submit)
{
// get the user values and insert it into
the database
}
}
} // end of class ModifyUser
with warm regards,
Arun.




Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Where are you adding the DB values to the JComboBox. What he said is you need to make sure that when you update the database you clear and reload the JComboBox. It sounds like you only do it one time.
Javaoops
Ranch Hand

Joined: Jun 21, 2001
Posts: 57
Try out the sample

here is the sample dynamic generation of data to the combobox data items.
At first time combobox will have one item(1), later at every visit to the tab, combobox item get increased(i.e, 2,3,4,5,6).
Accodingly you can modify your code, such that dynamically it queries the database and update the combo item.
keep in mind that each time you have to clear items in the combobox and then add the items which is quried from the database else you will have duplicates.

Arun Martin
Ranch Hand

Joined: Dec 01, 2000
Posts: 64
Hi Rajan,
Thanks a lot for the extra effort. I tried out but I am enclosing the exact replica of the code. This code is based on the previous sample code. Correct me if i have got the concepts wrong.
Code:
----
public class AdminTabbedPan extends JFrame implements ChangeListener
{
Container container;
JTabbedPane tp;
int count = 0;
public AdminTabbedPan()
{
container= this.getContentPane();
tp = new JTabbedPane();
JPanel panel1 = new JPanel();
JButton button1 = new JButton("button1 panel1");
panel1.add(button1);
JButton button2 = new JButton("button2 panel1");
panel1.add(button2);
JPanel panel2 = new JPanel();
JButton button21 = new JButton("button1 panel2");
panel2.add(button21);
combo = new JComboBox();
String string = ""+ ++count;
tp.addChangeListener(this);
tp.addTab("Add New User",panel1);
tp.setBackgroundAt(0,Color.white);
tp.addTab("Add New MgmtRep QCRep",panel2);
tp.setBackgroundAt(1,Color.white);
container.add(tp);
pack();
setSize(400,400);
}
public void stateChanged(ChangeEvent ce)
{
JTabbedPane tbpane;
tp.repaint();
tbpane = (JTabbedPane) ce.getSource();
int selectedIndex = tbpane.getSelectedIndex();
if (selectedIndex == 0)
{
// new AddUser();
System.out.println("Selected tab is 0");
}
if (selectedIndex == 1)
{
`// IN panel2
// I create the JCombo And display the values // here from the database
class panel2 extends JPanel implements ActionListener
{
JComboBox SelectUserCombo;
ResultSet res;
Connection con;
Statement stm;
public panel2()
{
TitlePanel.add(title);
SelectUserCombo = new JComboBox();
SelectUserCombo.addItemListener(this);
SelectUserCombo.removeAllItems();

try
{
// GET THE DATABASE VALUES
while(res.next())
{
SelectUserCombo.insertItemAt
(res.getString(1),i);
i++;
}
}
catch(Exception ex)
{

}
// add SelectUserCombo to the panel
}// end of constructor panel2()
public void actionPerformed(ActionEvent evt)
{
Object e = evt.getSource();
if (e==SelectUserCombo)
{
}
} // end of actionPerformed

}// end of class panel2
}// end of selectedIndex=2
}// end of state Changed
public static void main(String[] a)
{
AdminTabbedPan ob = new AdminTabbedPan();
ob.show();
}
}// end of class AdminTabbedPan
with warm regards,
Arun.
 
Consider Paul's rocket mass heater.
 
subject: Can I use repaint() on a JFrame?