i got the following java code
Icon icoR = new ImageIcon("src/resources/Republica.png");
Icon icoI = new ImageIcon("src/resources/Imperio.png");
JButton botRep = new JButton("Jedi", icoR);
JButton botImp = new JButton("Sith", icoI);
Object[] options = {botRep, botImp};
//Object[] options = {"Jedi","Sith"};
int i = JOptionPane.showOptionDialog(
null,
"Question","Title",
JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE,
null,options,options[0]);
System.out.println(i);
and im trying to retrieve the selected value from the optionDialog with icon buttons. Problem is, that althought dialog and buttons themselves are properly shown, whenever the buttons are clicked.. nothign happens, and the println statement isnt executed... until i click on the X to close the dialog, and regardless of what i have clicked before.. an 1 is printed as result. Oddly enough, if i change the options array to the regular (text only) one that appears commented in the code, the dialog behaves as one would expect returning the 0-based index of the chosen option. Any hints on what could be wrong here?
thanks in advance
It works if you just pass the Icons, the option pane will create the buttons for you:
Object[] options = {icoR, icoI};
So I guess if you pass the buttons it assumes that you will handle the closing of the option pane yourself.
As a work around you can use the Compound Icon class to create an Icon containing text and your image.
You might also want to use the Text Icon which you can also find from the above link. When you create the TextIcon you will probably need to use:
setFont( UIManager.getFont("Button.font") );
to set the font of the TextIcon to be the same as the button.
Related
My whole application is right to left for Persian language. I need to show the alerts in right to left.
How could I change the properties of alert dialogue for this purpose?
You should be able to change the nodeOrientation on the dialogPane of the Dialog:
Alert alert = ...
alert.getDialogPane().setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);
As fabian has suggested below, you could do this simply with
alert.getDialogPane().setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);
But if someone came in searching for some manual way of doing this (though I don't see why anyone would want to do this), then please find the details below-
I have no knowledge of how the languages Right-To-Left work. But you can TextFlow does provide the option to align text right to left. So this might work, but I am not sure!
Also, the following code snippet does not edit the text in the alert box itself. But it just adds additional TextFlow to the dialog.
Alert alert = new Alert(AlertType.CONFIRMATION);
alert.setTitle("Some title");
Text text = new Text("some right-to-left text here");
TextFlow textFlow = new TextFlow();
textFlow.setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);
textFlow.getChildren().addAll(text);
alert.getDialogPane().setContent(textFlow);
alert.show();
This is how the dialog box looks, after I added some Google-translated 'Hello World' in Persian. Just did this, so people looking up would directly know if this works or not, rather than having to try it out.
The header text does seem to get in the way. So you can remove it by using-
alert.setHeaderText(null);
EDIT:
Since the buttons are still in English (or Left-Right), here is something you can use, considering you've done the entire application Right-Left, follow same procedure for making the Button texts Right-Left. I wouldn't put time into it, as it is something you already have.
ButtonType okBtn; //make sure this is of type ButtonBar.ButtonData.OK_DONE
ButtonType cancelBtn; //make sure this is of type ButtonBar.ButtonData.CANCEL_CLOSE
Alert alert = new Alert(AlertType.CONFIRMATION,"",okBtn,cancelBtn);
alert.getDialogPane().setContent(textFlow);
alert.setTitle("Some title");
alert.setHeaderText(null);
Optional<ButtonType> result = alert.showAndWait();
You can perform the task if OK button is clicked by processing the result -
Check if result.isPresent() is true and check if result.get() == okBtn
The result of this code (ignore the button texts) -
If this works, then please do let me know. And if it doesn't let me know 'how' its not looking right.
Use:
alert.getDialogPane().getScene().setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);
instead of:
alert.getDialogPane().setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);
to make the title bar direction RTL as well.
I am using a bare bones JFileChooser on Redhat 6 with the code:
JFileChooser testFileChooser = new JFileChooser("Test");
testFileChooser.showOpenDialog(this);
The JFileChooser works as expected. In the file selection area, if I right click, I get a dialog with three rows, one with an arrow. There is no text describing what the rows does. The first row switches between list and detail views. The second row appears to do nothing and the third row creates a new folder. When I googled about this problem there appears to be no way to access or inspect this right-click popup dialog of the JFileChooser. I do not understand how it could break in the first place like this. I need ideas for how to fix or workaround this problem please.
I've found a fix/workaround!
JFileChooser uses a FilePane internally, which uses the JFileChooser's getComponentPopupMenu as it's context menu. However, because that returns null, it creates its own.
The text on the labels are gotten from the UIManager, with UIManager.getString calls. For some reason, the UIManager is returning empty strings.
So, we just have to set the strings in the UIManager manually.
If you make these calls before creating the file chooser, that'll fix it.
UIManager.put("FileChooser.detailsViewActionLabelText", "Details");
UIManager.put("FileChooser.listViewActionLabelText", "List");
UIManager.put("FileChooser.viewMenuLabelText", "View");
UIManager.put("FileChooser.refreshActionLabelText", "Refresh"));
UIManager.put("FileChooser.newFolderActionLabelText", "New Folder");
I realize this is 4 years late, but I hope this helps.
Basically, I need an image/icon to show up whenI hover over a swing component
//this is what i normally use to apply image to components
Icon icon = new ImageIcon(getClass().getResource("icon.png"));
label = new JLabel(icon);
what I'd like to do:
//use an imported icon on a tool tip text and also add some texts
label.setToolTipText(icon + "some random text");
Tool tips support HTML, so display that image as you might display it in HTML. E.G. as seen in this answer.
Background:
I am doing some UI work in an eclipse environment where I fill a combo control with some values. The string values have an different length and often a length greater than the combo width and the width of the parent composite.
Problem:
When I open the dropdown list, the width of the list is greater than the width of the parent composite and the user could not see the complete value of the list entry.
I have tried to use the "setTextLimit" option, but without success. For me it is perfectly adequate if I could set the width of the dropdon list with a constant value.
Code example:
this.mComponentName = new Combo (lComponentComposite, SWT.BORDER);
this.mComponentName.setTextLimit(COMBO_TEXT_LIMIT);
GridData componentNameGridData = new GridData();
componentNameGridData.widthHint = 166;
this.mComponentName.setLayoutData(componentNameGridData);
this.mComponentName.addSelectionListener(this.mComboSelectionAdapter);
this.mComponentName.addKeyListener(this.mComboKeyAdapter);
Greetings
dirk
While creating a combobox specify Horizontal scroll also
this.mComponentName = new Combo (lComponentComposite, SWT.BORDER|SWT.H_SCROLL);
This will not let the text overflow
That is really a good question. After digging through developer forums and even the source code, I lean towards saying it is not possible.
I solved the problem temporarily by switching to a CCombo, but I do not like the solution as I believe one of SWT's strength is in using native widgets, and the CCombo does not look as good (at least on Win7 and OS X).
When you select a drop-down combo box or drop-down list box to size it, only the right and left sizing handles are active. Use these handles to set the width of the box as it is displayed initially.
Choose the drop-down arrow at the right of the combo box.
The outline of the control changes to show the size of the combo box with the drop-down area extended.
Use the bottom sizing handle to change the initial size of the drop-down area.
Choose the drop-down arrow again to close the drop-down portion of the combo box.
Have you tried passing some other value as your second parameter in your Combo initialization (first line) ? Something other than SWT.BORDER?
I've tried finding out if it is possible to display strings in multiple rows (as a single combo box item), but with no success. This would reduce the width of your items. Try adding \n to a couple of strings that you add to the combo box, and see if it will work. If it works, you can automate this process later by parsing through the string, and checking whether the space character count gets to high and adding \n after every fifth or sixth blank character. I think codejammer's suggestion is the best one, but I can't upvote yet.
Partially solved the problem adding to item names some spaces (have set the number of spaces by trial and error). Now combo is wider and all texts are visible, however horizontal scroll is visible, too.
String p = " ";
combo.add("Long.... item name 1"+p);
combo.add("Long item name ..... 2"+p);
...
Another soultion is resize combo when it gain focus and restore size after lost focus. Example below.
final Point p = combo.getSize();
combo.addFocusListener(new FocusListener() {
#Override
public void focusLost(FocusEvent e) {
((Control) e.getSource()).setSize(p);
}
#Override
public void focusGained(FocusEvent e) {
((Control) e.getSource()).setSize(new Point(400, p.y));
}
});
combo.setPreferredSize(new Dimension(FIX_WIDTH, FIX_HEIGHT));
Try this
JComboBox CB = new JComboBox();
CB.setPreferredSize(new Dimension(int, int));
p.add(CB);// which p is a JPanel
I need to have a button whose text is underlined and the only way I could find to do this in Java was to use and tags, but when I do this, it causes the button to take up as much room as is left in the JToolBar even though the text is short and it should only take up a small amount of space. Here is how I create the Button.
String buttonText = new String("<html><u>Lesson Plans</u></html>");
JButton lessonButton = new JButton(buttonText);
toolBar.add(lessonButton);
If I remove the tags then it takes up the right amount of space but if I have them in there is takes up the entire toolBar. Anyone know what's going on?
You might be able to fix the problem by using:
button.setMaximumSize( button.getPreferredSize() );
Otherwise you should be able to just change the font to use an underlined font. Darryl's Visual Font Designer shows how to add attributes to a font.
You can overwrite the paintComponent method of your JButton, and write on it with any style and font.
You forgot the closing "" and wrote "" instead... This may be the reason for your problems.