Problème d'overlap sur composant étendant un JTextField
Bonjour à tous,
Je souhaiterai étendre les fonctionnalités d'un JTextField en lui ajoutant un bouton lié
agissant uniquement le JTextField.
Pour ce faire j'ai créé un nouveau composant étendant la classe JTextField qui ajoute le
bouton et sa fonction.
Le nouveau composant réagit bien comme un JTextField et le bouton fonctionne.
Le problème est à l'affichage.
Le bouton créé recouvre le JTextField au lieu d'être placé après sans overlap.
J'ai essayé différents layout sans résultat.
Quelqu'un aurait il une idée de solution à ce problème d'affichage ?
Faut il redéfinir la méthode paintComponent ?
Toute idée sera la bienvenue.
Ci joint un exemple qui permet de reproduire le problème.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class TestJtfldBtApp extends JFrame
{
JPanel pan;
JTextFieldBt tfb1;
JTextFieldBt tfb2;
JTextArea affichage;
JButton showbutton;
TestJtfldBtApp()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pan = new JPanel();
pan.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 10));
// 1er JTextField avec bouton
tfb1 = new JTextFieldBt();
pan.add(tfb1);
// 2eme JTextField avec bouton
tfb2 = new JTextFieldBt();
pan.add(tfb2);
// JTextArea pour affichage resultat sur clic showbutton
affichage = new JTextArea();
affichage.setPreferredSize(new Dimension(200, 100));
pan.add(affichage);
// création boutton affichage
showbutton = new JButton("Display");
pan.add(showbutton);
// listener pour action du showbutton
showbutton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
affichage.append("\nTBF1 <"+tfb1.getText()+">");
affichage.append("\nTBF2 <"+tfb2.getText()+">");
}
});
getContentPane().add(pan);
setSize(300, 300);
setVisible(true);
}
private class JTextFieldBt extends JTextField
{
private JTextField jtf;
private JButton button;
JTextFieldBt()
{
// le constructeur par defaut créé un JTextField de 10 cars
super(null, 10);
jtf = this;
this.setLayout(new FlowLayout(FlowLayout.RIGHT, 2, 0));
// création boutton lié au JTextField
button = new JButton("Eff.");
this.add(button);
// listener pour action du boutton
button.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
// exemple action - efface le text du JTextField
jtf.setText("");
}
});
}
}
public static void main(String[] args)
{
new TestJtfldBtApp();
}
} |
Exemple de classe étendant un JTextField avec un JButton
Bonjour à tous,
Merci Ivr pour le pointeur, je ne connaissais pas.
Comme promis ci joint un exemple résultant de mes différentes recherches pour ceux que cela peut intéresser.
Cet exemple demande 3 sources :
- Main.java
- MonBorder.java
- MonJtextfieldButton.java
Main.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
public class Main
{
public static void main(String[] args)
{
JFrame mainFrame = new JFrame();
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel mainPanel = new JPanel();
mainPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 50, 10));
// ajout 1er JTextField avec bouton
final MonJtextfieldButton jtfldbt1 = new MonJtextfieldButton();
mainPanel.add(jtfldbt1);
// ajout 2ème JTextField avec bouton
final MonJtextfieldButton jtfldbt2 = new MonJtextfieldButton();
mainPanel.add(jtfldbt2);
// ajout JTextArea pour affichage du contenu des JTextField
final JTextArea affichage = new JTextArea();
affichage.setPreferredSize(new Dimension(200, 100));
affichage.setFocusable(false);
mainPanel.add(affichage);
// ajout bouton pour affichage du contenu des JTextField
JButton affichebutton = new JButton("Affiche");
mainPanel.add(affichebutton);
// listener pour action sur affichebutton
affichebutton.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
affichage.setText("");
affichage.append("\nTBF1 <"+jtfldbt1.getText()+">");
affichage.append("\nTBF2 <"+jtfldbt2.getText()+">");
}
});
mainFrame.getContentPane().add(mainPanel);
mainFrame.setSize(300, 300);
mainFrame.setVisible(true);
} |
MonBorder.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import javax.swing.JComponent;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
public class MonBorder implements Border
{
public static final int TOP = 1;
public static final int LEFT = 2;
public static final int BOTTOM = 3;
public static final int RIGHT = 4;
public static final float LEADING = 0.0f;
public static final float CENTER = 0.5f;
public static final float TRAILING = 1.0f;
private JComponent component;
private int side;
private float alignment;
private Insets borderInsets = new Insets(0, 0, 0, 0);
//private int gap = 5;
private int gap = 0;
public MonBorder(JComponent component, int side, float alignment )
{
if (side == TOP
|| side == LEFT
|| side == BOTTOM
|| side == RIGHT)
{
this.side = side;
}
else
{
String message = "Side doit être TOP, LEFT, BOTTOM, ou RIGHT";
throw new IllegalArgumentException(message);
}
this.component = component;
component.setSize( component.getPreferredSize() );
this.alignment = alignment > 1.0f ? 1.0f : alignment < 0.0f ? 0.0f : alignment;
updateAndAlign();
}
private void updateAndAlign()
{
if (side == TOP)
{
borderInsets.top = component.getPreferredSize().height + gap;
component.setAlignmentX(alignment);
component.setAlignmentY(0.0f);
}
else if (side == BOTTOM)
{
borderInsets.bottom = component.getPreferredSize().height + gap;
component.setAlignmentX(alignment);
component.setAlignmentY(1.0f);
}
else if (side == LEFT)
{
borderInsets.left = component.getPreferredSize().width + gap;
component.setAlignmentX(0.0f);
component.setAlignmentY(alignment);
}
else if (side == RIGHT)
{
borderInsets.right = component.getPreferredSize().width + gap;
component.setAlignmentX(1.0f);
component.setAlignmentY(alignment);
}
}
@Override
public Insets getBorderInsets(Component c)
{
return borderInsets;
}
@Override
public boolean isBorderOpaque()
{
return false;
}
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
{
float x2 = (width - component.getWidth()) * component.getAlignmentX() + x;
float y2 = (height - component.getHeight()) * component.getAlignmentY() + y;
component.setLocation((int)x2, (int)y2);
}
/*
* Remplace le Border existant avec un CompoundBorder contenant le
* Border original et le ComponentBorder
*/
public void installe(JComponent parent)
{
adjusteBorder(parent);
Border current = parent.getBorder();
if (current == null)
{
parent.setBorder(this);
}
else
{
CompoundBorder compound = new CompoundBorder(current, this);
parent.setBorder(compound);
}
parent.add(component);
}
/*
* Le nouveau Border doit être ajusté pour permettre au composant
* de s'insérer dans les limites du composant parent
*/
private void adjusteBorder(JComponent parent)
{
Insets parentInsets = parent.getInsets();
// ajustement de la hauteur
if (side == RIGHT || side == LEFT)
{
int parentHeight = parent.getPreferredSize().height - parentInsets.top - parentInsets.bottom;
int diff = component.getHeight() - parentHeight;
if (diff > 0)
{
if (alignment == LEADING)
{
borderInsets.bottom += diff;
}
else if (alignment == TRAILING)
{
borderInsets.top += diff;
}
else
{
int half = diff / 2;
borderInsets.top += half;
borderInsets.bottom += (diff - half);
}
}
}
// ajustement de la largeur
if (side == TOP || side == BOTTOM)
{
int parentWidth = parent.getPreferredSize().width - parentInsets.left - parentInsets.right;
int diff = component.getWidth() - parentWidth;
if (diff > 0)
{
if (alignment == LEADING)
{
borderInsets.right += diff;
}
else if (alignment == TRAILING)
{
borderInsets.left += diff;
}
else
{
int half = diff / 2;
borderInsets.left += half;
borderInsets.right += (diff - half);
}
}
}
}
} |
enfin MonJtextfieldButton.java
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
/**
* Exemple : class extendant un JTextField avec un bouton
*
*/
public class MonJtextfieldButton extends JTextField
{
private JTextField MonJtextField;
private JButton MonJButton;
/**
* Constructeur par defaut.
*/
public MonJtextfieldButton()
{
super(null, 10);
MonJtextField = this;
initComponent();
}
/**
* Constructeur avec texte
*
* @param text String
*/
public MonJtextfieldButton(String text)
{
super(text);
MonJtextField = this;
initComponent();
}
/**
* Constructeur avec nombre de colonnes.
*
*
* @param columns int
*/
public MonJtextfieldButton(int columns)
{
super(columns);
MonJtextField = this;
initComponent();
}
/**
* Constructeur avec texte et nombre de colonnes
*
* @param text
* @param columns
*/
public MonJtextfieldButton(String text,int columns)
{
super(text,columns);
MonJtextField = this;
initComponent();
}
/**
* Initialisation
*/
private void initComponent()
{
// creation du JButton lié au JTextField
MonJButton = new JButton("...");
MonJButton.setBorder( new EmptyBorder(1, 3, 1, 3) );
MonJButton.setPreferredSize( new Dimension(18, 16) );
MonJButton.setFocusable(false);
MonBorder cpnBorder =
new MonBorder(MonJButton, MonBorder.RIGHT, MonBorder.CENTER);
cpnBorder.installe(MonJtextField);
// ajout listener
MonJButton.addActionListener(new java.awt.event.ActionListener()
{
@Override
public void actionPerformed(java.awt.event.ActionEvent evt)
{
ButtonAction();
}
});
return;
}
/**
* Action executée sur clic sur le bouton
*/
private void ButtonAction()
{
// exemple action - on efface le texte du JTextField
// cette partie doit être adaptée suivant la fonction du bouton
MonJtextField.setText("");
}
} |