0
I have a component that is a JPanel
containing two JTextFields
, wanted to be able to apply borders and funds only for the JTextFields
, without applying on the panel.
I treat everything on the main screen so that it is applied to everything of the type JComponent
, and also because the component can be applied to several screens.
For other components such as JTextFields
and the JTextArea
, it applies correctly.
Simplified example:
package focu;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.LineBorder;
public class TelaPrincipal extends JFrame implements FocusListener {
private JLabel label = new JLabel("Componente:");
private MeuComponente comp = new MeuComponente();
private JLabel label2 = new JLabel("JTextField:");
JTextField jt = new JTextField();
private JLabel label3 = new JLabel("JTextArea:");
JTextArea area = new JTextArea();
public static void main(String[] args) {
TelaPrincipal teste = new TelaPrincipal();
teste.setVisible(true);
}
public TelaPrincipal() {
setTitle("Teste");
add(montaTela());
//setSize(150, 300);
pack();
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private JComponent montaTela() {
JPanel painel = new JPanel();
painel.setLayout(new FlowLayout());
painel.add(label);
painel.add(comp);
comp.addFocusListener(this);
painel.add(label2);
painel.add(jt);
jt.setPreferredSize(new Dimension(100, 20));
jt.addFocusListener(this);
painel.add(label3);
painel.add(area);
area.setPreferredSize(new Dimension(100, 100));
area.addFocusListener(this);
return painel;
}
@Override
public void focusGained(FocusEvent fe) {
if (fe.getSource() instanceof JComponent) {
((JComponent) fe.getSource()).setBorder(new LineBorder(Color.RED));
((JComponent) fe.getSource()).setBackground(Color.LIGHT_GRAY);
}
}
@Override
public void focusLost(FocusEvent e) {
((JComponent) e.getSource()).setBorder(new LineBorder(Color.GRAY));
((JComponent) e.getSource()).setBackground(Color.WHITE);
}
}
class MeuComponente extends JPanel
{
public JTextField jt01 = new JTextField();
public JTextField jt02 = new JTextField();
public MeuComponente()
{
setLayout(new FlowLayout());
add(jt01);
jt01.setPreferredSize(new Dimension(70, 20));
add(jt02);
jt02.setPreferredSize(new Dimension(70, 20));
}
}
it worked perfectly, just a doubt, whatever that is (Component comp : Components) { } ? I didn’t know you could do it that way. And the method can be used in Jinternalframe as well ?
– Java
@Java Is a loop that traverses the list of components returned by the method
getComponentes()
. As every component is aJComponent
, everyone inherits this method. Hence the check if it is a typeContainer
, since we are not interested in going through components of a jtextfield or label, since it is difficult for these components to store others internally.– user28595
@Java the method
setFocusInComponents()
works with any component that is passed to it,JInternalFrame
includes in this list.– user28595
in the actual application, it is not working. The only difference is how I add the components. There I use Gridbaglayout and in the example I used Flow
– Java
@Java but the method has nothing to do with the layout. Do not forget that it should be invoked after the screen is already practically with all components inserted, otherwise it will not work at all. If you are using Nimbus as a theme, it may not work because the theme already has the focus colors set.
– user28595
So I had put in the constructor, plus, as I have a method that adds the components, removed and put at the end of this method, after they add the components, but it didn’t work.
– Java
@Java are you applying some custom theme or Nimbus? It is not every theme that accepts this type of modification. In the question code is the most standard theme, it accepts, but other themes may impose themselves and not accept.
– user28595
i am using a Lookandfeel, but it works on it, in my application is the same theme, more n works
– Java
Let’s go continue this discussion in chat.
– Java