JTextField hinter JComponent verbergen

Hallo,

ich habe da ein Problem und zwar möchte ich gerne, dass das TextField von der BackgroundComp
nicht im Teil der ForegroundComp (links mit schwarzem Hintergrund) sichtbar und anklickbar ist.
Denn wenn man das Beispiel unten ausführt dann sieht man links das Textfeld durch die ForegroundComp durch…
Das Textfeld soll wie das Label in der BackgroundComp auch hinter der ForegroundComp verschwinden. Ist das irgendwie umsetzbar?

Außerdem soll das Textfeld wie bereits schon erwähnt nicht durch die ForegroundComp anklickbar und editierbar sein. (Dies soll nur durch die BackgroundComp-Seite möglich sein)

import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class FrameTest {
    
    public FrameTest() {
        JFrame frame = new JFrame();
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        ForegroundComp fgComp = new ForegroundComp(new Rectangle(0, 0, frame.getWidth()/2, frame.getHeight()));
        frame.add(fgComp);
        
        BackgroundComp bgComp = new BackgroundComp(new Rectangle(0, 0, frame.getWidth(), frame.getHeight()));
        frame.add(bgComp);
        
        frame.setVisible(true);
        frame.requestFocusInWindow();
    }
    
    public class BackgroundComp extends JComponent {
        
        public BackgroundComp(Rectangle bounds) {
            this.setBounds(bounds);
            
            JLabel label = new JLabel("Text text text text text text text text1");
            label.setBounds(100, 100, 200, 30);
            label.setForeground(Color.ORANGE);
            add(label);
            
            JTextField field = new JTextField("Text text text text text text text text2");
            field.setBounds(100, 200, 200, 30);
            field.setBorder(null);
            add(field);
        }
        
    }
    
    public class ForegroundComp extends JComponent {
        
        public ForegroundComp(Rectangle bounds) {
            this.setBounds(bounds);
            
            JLabel label = new JLabel("Text");
            label.setBounds(80, 20,50,23);
            label.setForeground(Color.WHITE);
            add(label);
        }
        
        public void paintComponent(Graphics g) {
            g.setColor(new Color(50,50,50));
            g.fillRect(0, 0, getWidth(), getHeight());
        } 
    }
    
    public static void main(String[] args) {
        new FrameTest();
    }
}```

ich würde mal behaupten, ohne das jetzt ausprobiert zu haben, das es an der reihenfolge liegt,
in der du die komponenten hinzufügst… aber ich denke ich teste das gleich mal…

*** Edit ***

ah, probier mal super.paintComponent(g) einzubauen in zeile 58… daran
dürfte es liegen…

*** Edit ***

wieso überschreibst du die überhaupt? einfach setBackground…

*** Edit ***

hmm… anklickbar ist es trotzdem… man könnte ja was basteln, dass
das event irgendwie abgebrochen wird, wenn foreground angeklickt wird…

aber wieso willst du das überhaupt so? was ist dein ziel?

*** Edit ***

äähmm… mach mal textField.setOpaque(false)…

Ok gut danke das hat mir schonmal geholfen :slight_smile: Habe aber trotzdem die weiße Box nochmal nach gemalt damit man sie wieder sieht.
Jetzt wäre nur noch das Problem, dass man nicht mehr das Textfeld im linken Feld anklicken darf.
Weiß jemand wie man das ändern kann?

import java.awt.Graphics;
import java.awt.Rectangle;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class FrameTest {
    
    public FrameTest() {
        JFrame frame = new JFrame();
        frame.setSize(400, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);

        ForegroundComp fgComp = new ForegroundComp(new Rectangle(0, 0, frame.getWidth()/2, frame.getHeight()));
        frame.add(fgComp);
        
        BackgroundComp bgComp = new BackgroundComp(new Rectangle(0, 0, frame.getWidth(), frame.getHeight()));
        frame.add(bgComp);
        
        frame.setVisible(true);
        frame.requestFocusInWindow();
    }
    
    public class BackgroundComp extends JComponent {
        
        private JTextField field;
        
        public BackgroundComp(Rectangle bounds) {
            this.setBounds(bounds);
            
            JLabel label = new JLabel("Text text text text text text text text1");
            label.setBounds(100, 100, 200, 30);
            label.setForeground(Color.ORANGE);
            add(label);
            
            field = new JTextField("Text text text text text text text text2");
            field.setBounds(100, 200, 200, 30);
            field.setBorder(null);
            field.setOpaque(false);
            add(field);
        }
        
        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.setColor(new Color(255,255,255));
            g.fillRect(field.getX(), field.getY(), field.getWidth(), field.getHeight());
        } 
    }
    
    public class ForegroundComp extends JComponent {
        
        public ForegroundComp(Rectangle bounds) {
            this.setBounds(bounds);
            
            JLabel label = new JLabel("Text");
            label.setBounds(80, 20,50,23);
            label.setForeground(Color.WHITE);
            add(label);
        }
        
        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            g.setColor(new Color(50,50,50));
            g.fillRect(0, 0, getWidth(), getHeight());
        } 
    }
    
    public static void main(String[] args) {
        new FrameTest();
    }
}```

Hab’s (noch) nicht wirklich durchgelesen, aber suchst du sowas wie eine GlassPane? How to Use Root Panes (The Java

man lernt nie aus… :rolleyes:

aber ja, genau soetwas sucht er glaube ich…

Ok das mit dem GlassPane ist wohl das was ich gesucht habe - funktioniert jetzt alles einwandfrei

Ich hätte jetzt vielleicht noch JLayeredPane mit in den Raum geworfen (was ja auch im Link erklärt wird), aber das GlassPane scheint ja den gewünschten Effekt zu bieten.