How to repaint a portion of a component?

I am working with JLayeredPane and several overlapping components which I draw a selection rectangle around when the user clicks on a component to indicate that it is "selected". If the user selects another component I need to wipe out the selection rectangle on the previously selected component. I'm not sure what the best approach for this is.
If the initially selected component was underneath (in a lower layer than) another component, I can't simply repaint it without repainting all of the components that are above it. This seems like it could be too much painting just to remove a selection box. Can someone recommend a good approach for this problem?
Thanks,
Jonathan

Thanks for the response. The components should be on separate layers (added via JLayeredPane.add(JComponent) ) so if the painter takes the layers into account, then there must be something else wrong. Here's a quick demo program of what I'm doing:
import java.awt.*;
import java.awt.event.MouseEvent;
import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
public class LayerDemo {
     private ControlMouseInputAdapter controlMouseInputAdapter = new ControlMouseInputAdapter();
private static void createAndShowGUI() {
     LayerDemo layerDemo = new LayerDemo();
JFrame mainFrame = new JFrame();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {;}
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setSize(400, 400);
JLayeredPane layeredPane = mainFrame.getLayeredPane();
layeredPane.add(layerDemo.createColoredLabel("test1", Color.RED, new Point(20, 20)));
layeredPane.add(layerDemo.createColoredLabel("test2", Color.BLUE, new Point(40, 40)));
mainFrame.setVisible(true);
private JLabel createColoredLabel(String text, Color color,
Point origin) {
JLabel label = new JLabel();
label.addMouseListener(controlMouseInputAdapter);
label.setText(text);
label.setOpaque(true);
label.setBackground(color);
label.setBounds(origin.x, origin.y, 140, 140);
return label;
public static void main(String[] args) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {createAndShowGUI();}
private class ControlMouseInputAdapter extends MouseInputAdapter {
public void mouseEntered(MouseEvent e) {
JComponent component = (JComponent) e.getSource();
component.setBorder(BorderFactory.createLineBorder(Color.black));
public void mouseExited(MouseEvent e) {
JComponent component = (JComponent) e.getSource();
component.setBorder(null);
}

Similar Messages

  • How to repaint a panel

    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, and get back to the original size, I can see the no of textFields have increased to 6; 2 from the beginning and 4 from the first click.
    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];
              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.setText(data[i]);

    BickelT , yes, it does work! I am almost there, only a small hurdle. When I initialize my textFields with a bigger size, all the smaller sizes are painted correctly. But when the initial size (when index is 0, that is at the time of init) is small, all the subsequent sizes are fixed to that init size.
    For example, when I change my code to the following, it works fine.
    private String[] getData(int index){
              if(index == 0){
                   String[] names = { "C", "D", "E", "F" };
                   return names;
              else if (index==1) {
                   String[] names = { "A", "B" };
                   return names;
              else {
                   String[] names = { "X"};
                   return names;
         } With the code as it is (as posted originally), in the first click, a textField with space(=height) for two textFields is painted with the value C and the lower half is empty. It is refusing to draw the area bigger than the init size of 2. When I resize the applet, I can see that four textFields with the values C, D, E, F.have been painted.
    I added the codes you suggested inside the addActionListner class. Isn't that the correct place to add the codes?
    BTW, with or without p1.repaint, the result is the same.
    What could have gone wrong?
    Thank you.

  • How to email text from a text component in my applet on a the host server ?

    How to email text from a text component in my applet on a the host server, back to my email address ?
    Assuming I have Email Form on the host server.
    Help will be appreciated.

    You can do like below
    =REPLACE(Fields!Column.Value," " & Parameters!ParameterName.value & " ","<b>" & Parameters!ParameterName.value & "</b>")
    The select the expression and  right click and choose placeholder properties
    Inside that set Markup type as HTML 
    then it will highlight the passed parameter value in bold within the full xml string
    Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs

  • How to create and call a COM component in Java

    Hi All,
    can you suggest how to create and call a COM component..or if have any sample simple application can you send it to me...i will try to develop my project.
    actually i am configuring a OCR Engine using SDK which is in VB .Net when i contacted their support they told that if i have any sample COM based component Project they will help me...
    So please can you help in this.
    Thanks in advance.

    As said by my fellow posters Java Devolopment Environment (Except Microsoft implementation of JVM,this is no longer supported by MS themseleves) does not provide an built-in support to serve this cause.
    If you are looking to devolop a custom based solution the below is a good place to start with where end user is talking about Java <=> JNI <=> Native <=> COM connectivity.
    [http://bloggershetty.blogspot.com/2004/12/java-jni-com-activex-bridge-lots-of.html]
    However,if you are looking for ready made solutions ?
    Implementation any one of the solutions stated below might serve your cause.
    Open Source Solutions:
    [http://j-interop.org/]
    [http://www.danadler.com/jacob/]
    Commercial Solutions:
    [http://javacombridge.com/]
    [http://www.jnbridge.com/]
    [http://www.nevaobject.com/j2cdetails.asp?kw=java%20com%20bridge]
    [http://j-integra.intrinsyc.com/]
    Hope this might help :)
    REGARDS,
    RaHuL

  • How to access the webservice in portal component

    hai
         how to access the webservice in portal component.
         anyone knows give the solution
    Rds
    Shanthakumar

    Hai
    Please check this link.
    https://www.sdn.sap.com/irj/sdn/wiki?path=/display/vc/connectivity&
    Regards

  • How to create a hint for a component(awt),thanks

    How to create a hint for a component(awt),the component of awt has not the property.

    If you are talking about a tooltip, this is going to be hard.
    You are probably going to have to use a mouseMotionListener to get the event that the mouse is over the component and then a Timer to fire the showTooltip event if the mouse hasn't moved in some amount of time.

  • How to set a delay on Autocomplete component?

    How to set a delay on Autocomplete component so that the completion method will only be invoked when users stop typing for some time. Otherwise, there are too many unnecessary server requests.

    Hi,
    You can use shortDesc property. Something like
    <af:commandToolbarButton text="Some Button"
          id="ctb1" shortDesc="This button does something.."/>-Arun

  • How does repaint work?

    Can anybody tell me how does repaint work.

    hmm
    raheel_siddiqi = mraheel.
    Smart way of transferring Dukes (and earning more
    Dukes).
    Irrelevant question asked --> Posted in wrong forum
    --> STOOPID answer given ---> Dukes transferred.Very well pointed.... ;(
    >
    raheel bhai.. yeh baccho ka khel nahi hai :)Well Said :-)

  • How is the storage type for a component picked automatically in PO staging

    Hi
    The Process cycle is :
    Sales Order created MRP-Planned Order ---Production Order
    Problem : Transfer Requirement not created for Picking
    Now if I go to Co03--GOTOWM Pick list , For the component the storage type is notr updated automatically
    I compared with Materials for a TR was created & found that Storage type 100 was updated automatically
    Q:How is the storage type for a component picked automatically in PO staging (WM activated )

    Hi Rajesh
    Thanks for your inputs . Actually I forgot to mention that the client is not using Storage type in any of the material master data
    The problem was because the control cycle was not maintained for the combination of material /Plant & Supply Area
    Anyway thanks for your info .Appreciate your cooperation
    Arindam

  • How to embed a smartform into a component?

    Hi All,
    i would like to embed a smartform into the component to display the relevant information of complaint in WEB UI.
    In complaints when i clicked on complaint ID it is not not displaying any details? So i would like to add a smart form to the component to display all the details. Please help me how to add the smartform into the component?
    Thanks
    Maheedhar

    Hi pavan,
    I did not try this but I think you can call the smatforms function module and get the form in html from parameter JOB_OUTPUT_INFO-XMLOUTPUT-TRFRESULT.
    Put this into WD component using respective method. Compare Thread: [Add html code in web dynpro application|Add html code in web dynpro application]
    Regards,
    Clemens

  • How to add interface to customlize MXML Component when use Flex Builder 3?

    How to add interface to customlize MXML Component when use
    Flex Builder 3?

    David,
    I don't believe you can add the interface via the creation
    dialog in FlexBuilder 3. You can always manually add the
    "implements" property to your MXML Component root tag. Something
    like this: <mx:VBox implements="com.mycorp.IMyInterface">
    If you want autogeneration of the interface, then create an
    ActionScript class with that interface and then copy the generated
    functions and setter/getters into the script block of your MXML
    component.

  • How is the scarp portion of MO variance calculated?

    How is the scarp portion of Manufacturing Order variance calculated?

    First check Settings > General > Usage > see what apps is taking all the space.
    Delete those unwanted data, and sync with computer iTunes.
    If no joy, restore as new and sync back your content manually.

  • How to access system variables in Script Component in data flow task in SSIS

    Hi,
    I am new to SSIS. Can someone tell me how to access system variable in Script Component in SSIS using C# code.
    Thanks

    You can use the System.Environment.GetEnvironmentVariable(...) to read the variables. An example is here:
    http://msdn.microsoft.com/en-us/library/y6k3c7b0.aspx
    Vikash Kumar Singh || www.singhvikash.in

  • How do I delete portions of my text messages?  The entire thread is there on iOS 7.  I don't old old mesages

    How do delete old portions of messages on iOS 7. I used to be able to delete older portions

    Open your message app, go to the body of text you want to delete, hold down on it until you see copy/more - then select more - you should see some options.

  • In the Suite, how does the email portion handle spam?

    In the Suite, how does the email portion handle spam?

    Every email message you mark as spam (and also mark the ones that aren't spam that went in to your junk folder) helps it learn over time.
    If you want more (extremely) technical information about it: http://en.wikipedia.org/wiki/Bayesian_spam_filtering

Maybe you are looking for

  • How to delete fixed number of records at a time

    Hi, I have a millions of records in one table.I have to purge first 15 days data per day by deleting at time 10000 records. Appreciate any ideas from you. Thanks in Advance. regards, Suresh

  • File Upload issue in MVC

    Hi, For my MVC application, I have created a main controller. On the DO_REQUEST of the main controller, I am setting the sub controllers, and directly calling that controller using create_controller, controller_set_active, and call_controller. I incl

  • How to use HTTPS to retrieve a set of files from a distant server?

    Hi ! i am interested in some samples or links showing how is it possible to use HTTPS in a java code in a standalone application ( could be swing based or whatever) that allows an HTTPS connection to a distant server ( specefic file repository) so th

  • Email on Lion not sending, saving in drafts instead?

    I am having an interesting problem. This has happened twice now, where I swear I sent an email, I later find it in my drafts folder, and I'm not sure how it got there. This has never been a problem for me when I used MS Outlook or Gmail online, but f

  • Maven 2.0 - problems getting started

    Dear friends, I'm trying to migrate my old fashioned maven projects to the new version of maven 2.0 At first steps I'm just trying to run the console command that appear on the getting Started guide at the maven site, but it is simply not working ove