下面列出了javax.swing.AbstractButton#setText ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void setIcon(AbstractButton button, String res, String alt) {
try {
URL url = GraphsPanel.class.getClassLoader().getResource(res);
if (url == null) {
button.setText(alt);
}
else {
BufferedImage icon = ImageIO.read(url);
button.setIcon(new ImageIcon(icon));
}
}
catch (Exception e) {
button.setText(alt);
}
button.setToolTipText(alt);
}
/**
* @noinspection ALL
*/
private void $$$loadButtonText$$$(AbstractButton component, String text) {
StringBuffer result = new StringBuffer();
boolean haveMnemonic = false;
char mnemonic = '\0';
int mnemonicIndex = -1;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '&') {
i++;
if (i == text.length()) break;
if (!haveMnemonic && text.charAt(i) != '&') {
haveMnemonic = true;
mnemonic = text.charAt(i);
mnemonicIndex = result.length();
}
}
result.append(text.charAt(i));
}
component.setText(result.toString());
if (haveMnemonic) {
component.setMnemonic(mnemonic);
component.setDisplayedMnemonicIndex(mnemonicIndex);
}
}
/**
* @noinspection ALL
*/
private void $$$loadButtonText$$$(AbstractButton component, String text) {
StringBuffer result = new StringBuffer();
boolean haveMnemonic = false;
char mnemonic = '\0';
int mnemonicIndex = -1;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '&') {
i++;
if (i == text.length()) break;
if (!haveMnemonic && text.charAt(i) != '&') {
haveMnemonic = true;
mnemonic = text.charAt(i);
mnemonicIndex = result.length();
}
}
result.append(text.charAt(i));
}
component.setText(result.toString());
if (haveMnemonic) {
component.setMnemonic(mnemonic);
component.setDisplayedMnemonicIndex(mnemonicIndex);
}
}
protected SingleTargetProductDialog(AppContext appContext, String title, int buttonMask, String helpID, TargetProductSelectorModel model, boolean alwaysWriteOutput) {
super(appContext.getApplicationWindow(), title, buttonMask, helpID);
this.appContext = appContext;
targetProductSelector = new TargetProductSelector(model, alwaysWriteOutput);
String homeDirPath = SystemUtils.getUserHomeDir().getPath();
String saveDir = appContext.getPreferences().getPropertyString(SaveProductAsAction.PREFERENCES_KEY_LAST_PRODUCT_DIR, homeDirPath);
targetProductSelector.getModel().setProductDir(new File(saveDir));
if (!alwaysWriteOutput) {
targetProductSelector.getOpenInAppCheckBox().setText("Open in " + appContext.getApplicationName());
}
targetProductSelector.getModel().getValueContainer().addPropertyChangeListener(evt -> {
if (evt.getPropertyName().equals("saveToFileSelected") ||
evt.getPropertyName().equals("openInAppSelected")) {
updateRunButton();
}
});
AbstractButton button = getButton(ID_APPLY);
button.setText("Run");
button.setMnemonic('R');
updateRunButton();
}
/**
* @noinspection ALL
*/
private void $$$loadButtonText$$$(AbstractButton component, String text) {
StringBuffer result = new StringBuffer();
boolean haveMnemonic = false;
char mnemonic = '\0';
int mnemonicIndex = -1;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '&') {
i++;
if (i == text.length()) break;
if (!haveMnemonic && text.charAt(i) != '&') {
haveMnemonic = true;
mnemonic = text.charAt(i);
mnemonicIndex = result.length();
}
}
result.append(text.charAt(i));
}
component.setText(result.toString());
if (haveMnemonic) {
component.setMnemonic(mnemonic);
component.setDisplayedMnemonicIndex(mnemonicIndex);
}
}
/**
* @noinspection ALL
*/
private void $$$loadButtonText$$$(AbstractButton component, String text) {
StringBuffer result = new StringBuffer();
boolean haveMnemonic = false;
char mnemonic = '\0';
int mnemonicIndex = -1;
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) == '&') {
i++;
if (i == text.length()) break;
if (!haveMnemonic && text.charAt(i) != '&') {
haveMnemonic = true;
mnemonic = text.charAt(i);
mnemonicIndex = result.length();
}
}
result.append(text.charAt(i));
}
component.setText(result.toString());
if (haveMnemonic) {
component.setMnemonic(mnemonic);
component.setDisplayedMnemonicIndex(mnemonicIndex);
}
}
public static AbstractButton makeTexturedToolBarButton(AbstractButton button, String segmentPosition) {
if (null == segmentPosition || segmentPosition.isEmpty() || segmentPosition.equals(SEGMENT_POSITION_ONLY)) {
button.putClientProperty("JButton.buttonType", "textured");
} else {
button.putClientProperty("JButton.buttonType", "segmentedTextured");
button.putClientProperty("JButton.segmentPosition", segmentPosition);
}
button.setText(null);
button.setBorderPainted(true);
button.setPreferredSize(Constants.seaGlassButtonDimension);
return button;
}
/**
* Wrapper for AbstractButton/JLabel.setText
* @param item AbstractButton/JLabel
* @param text the text to set
*/
private static void setText(Object item, String text) {
if (item instanceof AbstractButton) {
AbstractButton b = (AbstractButton) item;
b.putClientProperty(PROP_TEXT, text);
b.setText(text);
} else {
((JLabel) item).setText(text);
}
}
/**
* Actual setter of the text & mnemonics for the AbstractButton or
* their subclasses. We must copy necessary code from org.openide.awt.Mnemonics
* because org.openide.awt module is not available yet when this code is called.
* @param item AbstractButton
* @param text new label
*/
static void setLocalizedText (AbstractButton button, String text) {
if (text == null) {
button.setText(null);
return;
}
int i = findMnemonicAmpersand(text);
if (i < 0) {
// no '&' - don't set the mnemonic
button.setText(text);
button.setMnemonic(0);
} else {
button.setText(text.substring(0, i) + text.substring(i + 1));
if (Utilities.isMac()) {
// there shall be no mnemonics on macosx.
//#55864
return;
}
char ch = text.charAt(i + 1);
// it's latin character or arabic digit,
// setting it as mnemonics
button.setMnemonic(ch);
// If it's something like "Save &As", we need to set another
// mnemonic index (at least under 1.4 or later)
// see #29676
button.setDisplayedMnemonicIndex(i);
}
}
private static void configure(AbstractButton button) {
RolloverButtonEventListener l = new RolloverButtonEventListener();
button.addMouseListener(l);
button.addItemListener(l);
if (button.getAction() != null) {
if (button.getIcon() != null) {
button.putClientProperty("hideActionText", Boolean.TRUE);
}
Object largeIcon = button.getAction().getValue("_largeIcon");
if (largeIcon instanceof Icon) {
button.setIcon((Icon) largeIcon);
}
}
Icon icon = button.getIcon();
int minWidth = BUTTON_MIN_SIZE;
int minHeight = BUTTON_MIN_SIZE;
if (icon != null) {
button.setText(null);
minWidth = Math.max(icon.getIconWidth(), BUTTON_MIN_SIZE);
minHeight = Math.max(icon.getIconHeight(), BUTTON_MIN_SIZE);
if (icon instanceof ImageIcon) {
button.setRolloverIcon(createRolloverIcon((ImageIcon) icon));
}
} else {
button.setText("[?]");
}
final int space = 3;
Dimension prefSize = new Dimension(minWidth + space, minHeight + space);
Dimension minSize = new Dimension(minWidth, minHeight);
Dimension maxSize = new Dimension(minWidth + space, minHeight + space);
button.setPreferredSize(prefSize);
button.setMaximumSize(maxSize);
button.setMinimumSize(minSize);
}
private void $$$loadButtonText$$$( AbstractButton component, String text )
{
StringBuffer result = new StringBuffer();
boolean haveMnemonic = false;
char mnemonic = '\0';
int mnemonicIndex = -1;
for( int i = 0; i < text.length(); i++ )
{
if( text.charAt( i ) == '&' )
{
i++;
if( i == text.length() )
{
break;
}
if( !haveMnemonic && text.charAt( i ) != '&' )
{
haveMnemonic = true;
mnemonic = text.charAt( i );
mnemonicIndex = result.length();
}
}
result.append( text.charAt( i ) );
}
component.setText( result.toString() );
if( haveMnemonic )
{
component.setMnemonic( mnemonic );
component.setDisplayedMnemonicIndex( mnemonicIndex );
}
}
/**
*
* @param precludedNames keys that will not be considered for filters
* @param elements elements that will be considered for filters
* @param maxFactor controls the threshold for filter creation
* @param buttonSupplier provides toolkit controls for the filters
* @param paintFunction provides a way to individually color the control buttons
*/
private AttributeFilters(Collection<String> precludedNames, Set<? extends Attributed> elements,
double maxFactor,
Supplier<AbstractButton> buttonSupplier, Function<String, Paint> paintFunction) {
// count up the unique attribute values (skipping the 'precluded names' we know we don't want)
for (Attributed element : elements) {
Map<String, String> attributeMap = new HashMap<>(element.getAttributeMap());
for (Map.Entry<String, String> entry : attributeMap.entrySet()) {
if (!precludedNames.contains(entry.getKey())) {
multiset.add(entry.getValue());
}
}
}
if (maxFactor == 0) {
maxFactor = .01;
}
double threshold = Math.max(2, elements.size() * maxFactor);
// accept the values with cardinality above the max of 2 and maxFactor times the of the number elements.
multiset.removeIf(s -> multiset.count(s) < threshold);
// create a button for every element that was retained
multiset.elementSet();
for (String key : multiset.elementSet()) {
AbstractButton button = buttonSupplier.get();
button.setForeground((Color) paintFunction.apply(key));
button.setText(key);
button.addItemListener(item -> {
if (item.getStateChange() == ItemEvent.SELECTED) {
selectedTexts.add(button.getText());
fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
this.selectedTexts, ItemEvent.SELECTED));
}
else if (item.getStateChange() == ItemEvent.DESELECTED) {
selectedTexts.remove(button.getText());
fireItemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
this.selectedTexts, ItemEvent.DESELECTED));
}
});
buttons.add(button);
}
}
/**
* Allocate and dress an instance of the provided class, then
* register the action in the UI structure (menus and buttons)
* according to the action descriptor parameters.
*
* @param action the provided action class
* @return the registered and decorated instance of the action class
*/
@SuppressWarnings("unchecked")
private ApplicationAction registerAction (ActionDescriptor desc)
{
///logger.info("registerAction. " + desc);
ApplicationAction action = null;
try {
// Retrieve proper class instance
Class<?> classe = classLoader.loadClass(desc.className);
Object instance = null;
// Reuse existing instance through a 'getInstance()' method if any
try {
Method getInstance = classe.getDeclaredMethod(
"getInstance",
(Class[]) null);
if (Modifier.isStatic(getInstance.getModifiers())) {
instance = getInstance.invoke(null);
}
} catch (NoSuchMethodException ignored) {
}
if (instance == null) {
// Fall back to allocate a new class instance
///logger.warn("instantiating instance of " + classe);
instance = classe.newInstance();
}
// Retrieve the action instance
action = getActionInstance(instance, desc.methodName);
if (action != null) {
// Insertion of a button on Tool Bar?
if (desc.buttonClassName != null) {
Class<? extends AbstractButton> buttonClass =
(Class<? extends AbstractButton>) classLoader.
loadClass(desc.buttonClassName);
AbstractButton button = buttonClass.newInstance();
button.setAction(action);
toolBar.add(button);
button.setBorder(UIUtil.getToolBorder());
button.setText("");
}
} else {
logger.error("Unknown action {} in class {}",
desc.methodName, desc.className);
}
} catch (ClassNotFoundException | SecurityException |
IllegalAccessException | IllegalArgumentException |
InvocationTargetException | InstantiationException ex) {
logger.warn("Error while registering " + desc, ex);
}
return action;
}
@Override
protected JPanel createVariablesPanel() {
JPanel variablesBorderPanel = new JPanel();
BoxLayout layout = new BoxLayout(variablesBorderPanel, BoxLayout.PAGE_AXIS);
variablesBorderPanel.setLayout(layout);
variablesBorderPanel.setBorder(BorderFactory.createTitledBorder(Bundle.CTL_Panel_SysVar_Border_TitleText()));
AbstractButton addVariableButton = ToolButtonFactory.createButton(UIUtils.loadImageIcon(Bundle.Icon_Add()), false);
addVariableButton.setText(Bundle.CTL_Button_Add_Variable_Text());
addVariableButton.setAlignmentX(Component.LEFT_ALIGNMENT);
addVariableButton.setMaximumSize(new Dimension(150, controlHeight));
AbstractButton addDependentVariableButton = ToolButtonFactory.createButton(UIUtils.loadImageIcon(Bundle.Icon_Add()), false);
addDependentVariableButton.setText(Bundle.CTL_Button_Add_PDVariable_Text());
addDependentVariableButton.setAlignmentX(Component.LEFT_ALIGNMENT);
addDependentVariableButton.setMaximumSize(new Dimension(250, controlHeight));
JPanel buttonsPannel = new JPanel(new SpringLayout());
buttonsPannel.add(addVariableButton);
buttonsPannel.add(addDependentVariableButton);
SpringUtilities.makeCompactGrid(buttonsPannel, 1, 2, 0, 0, 0, 0);
buttonsPannel.setAlignmentX(Component.LEFT_ALIGNMENT);
variablesBorderPanel.add(buttonsPannel);
varTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
varTable.setRowHeight(20);
JScrollPane scrollPane = new JScrollPane(varTable);
scrollPane.setAlignmentX(Component.LEFT_ALIGNMENT);
variablesBorderPanel.add(scrollPane);
variablesBorderPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
Dimension variablesPanelDimension = new Dimension((formWidth - 3 * DEFAULT_PADDING) / 2 - 2 * DEFAULT_PADDING, 130);
variablesBorderPanel.setMinimumSize(variablesPanelDimension);
variablesBorderPanel.setMaximumSize(variablesPanelDimension);
variablesBorderPanel.setPreferredSize(variablesPanelDimension);
addVariableButton.addActionListener(e -> {
newOperatorDescriptor.getVariables().add(new SystemVariable("key", ""));
varTable.revalidate();
});
addDependentVariableButton.addActionListener(e -> {
newOperatorDescriptor.getVariables().add(new SystemDependentVariable("key", ""));
varTable.revalidate();
});
return variablesBorderPanel;
}
/**
* Allocate and dress an instance of the provided class, then register the action in
* the UI structure (menus and buttons) according to the action descriptor parameters.
*
* @param action the provided action class
* @return the registered and decorated instance of the action class
*/
@SuppressWarnings("unchecked")
private ApplicationAction registerAction (ActionDescriptor desc)
{
///logger.info("registerAction. " + desc);
ApplicationAction action = null;
try {
// Retrieve proper class instance
Class<?> classe = classLoader.loadClass(desc.className);
Object instance = null;
// Reuse existing instance through a 'getInstance()' method if any
try {
Method getInstance = classe.getDeclaredMethod("getInstance", (Class[]) null);
if (Modifier.isStatic(getInstance.getModifiers())) {
instance = getInstance.invoke(null);
}
} catch (NoSuchMethodException ignored) {
}
if (instance == null) {
// Fall back to allocate a new class instance
instance = classe.newInstance();
}
// Retrieve the action instance
action = getActionInstance(instance, desc.methodName);
if (action != null) {
// Insertion of a button on Tool Bar?
if (desc.buttonClassName != null) {
Class buttonClass = classLoader.loadClass(desc.buttonClassName);
AbstractButton button = (AbstractButton) buttonClass.newInstance();
button.setAction(action);
toolBar.add(button);
button.setBorder(UIUtil.getToolBorder());
button.setText("");
}
} else {
logger.error("Unknown action {} in class {}", desc.methodName, desc.className);
}
} catch (ClassNotFoundException |
IllegalAccessException |
IllegalArgumentException |
InstantiationException |
SecurityException |
InvocationTargetException ex) {
logger.warn("Error while registering " + desc, ex);
}
return action;
}
private static void setText(AbstractButton b, String text, boolean mnemonics) {
if (mnemonics) Mnemonics.setLocalizedText(b, text);
else b.setText(text.replace("&", "")); // NOI18N
}
/**
* Localise the given AbstractButton, setting the text and optionally
* mnemonic Note that AbstractButton includes menus and menu items.
*
* @param button
* The button to localise
* @param key
* The key to look up in resource bundle
* @param defaultString
* default String to use if key not found
* @param setMnemonic
* whether or not to set the mnemonic. According to Sun's
* guidelines, default/cancel buttons should not have mnemonics
* but instead should use Return/Escape
*/
public static void localiseButton(AbstractButton button, String key, String defaultString, boolean setMnemonic) {
AnnotatedString as = new AnnotatedString(L10N.getLocalString(key, defaultString));
button.setText(as.toString());
int mnemonic;
if (setMnemonic && (mnemonic = as.getMnemonic()) != KeyEvent.VK_UNDEFINED) {
button.setMnemonic(mnemonic);
button.setDisplayedMnemonicIndex(as.getMnemonicIndex());
}
}
/**
* Updates/sets the text of an abstract button based on the key of its text.
*
* <p>The button's text will be taken from the {@link Language} specified by the <code>textKey</code>.<br>
* If the text of the button designates a mnemonic, it will be set properly.</p>
*
* @param button abstract button whose text to be updated
* @param textKey key of the button's text
*/
public static void updateButtonText( final AbstractButton button, final String textKey, Object... arguments ) {
final Pair< String, Character > textAndMnemonic = Language.getTextAndMnemonic( textKey, arguments );
button.setText( textAndMnemonic.value1 );
if ( textAndMnemonic.value2 != null )
button.setMnemonic( textAndMnemonic.value2 );
}
/**
* Shortcut function to translate a button type component.
* @param btn Button type component.
* @param key Key of the button text string.
*/
public static void tButton(AbstractButton btn, String key) {
btn.setText(t(key));
btn.setMnemonic(mnemonic(key));
}
/**
* Shortcut function to translate a button type component.
* @param btn Button type component.
* @param key Key of the button text string.
* @param args Text string arguments values.
*/
public static void tButton(AbstractButton btn, String key, Object[] args) {
btn.setText(t(key, args));
btn.setMnemonic(mnemonic(key));
}