I have a JMenuBar with the standard items and shortcuts. But I noticed that the shortcut description is left-aligned, which looks ugly. Is there a way to right-align it?
PS: "Umschalt" means shift. Is there a way to force it to say shift instead of Umschalt?
[UPDATE: Locale.setDefault(Locale.ENGLISH); fixes the problem, but a solution to only affect specific components would be better.. ]
PSPS: With UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); I have set the look and feel to be OS default. But now I would like to make some small adjustments to the look on top of the standard OS Look. For example I would like to make the JMenuBar black. The interwebs told me to use UIManager.put("tMenuBar.background", Color.BLACK); but it doesn't seem to do anything..
[UPDATE: It seems like this is not possible with Windows Look and feel :/]
Here the code:
private JMenuBar tMenuBar;
private JMenu mbEdit;
private JMenuItem mCut, mCopy, mPaste, mDo, mUndo;
tMenuBar = new JMenuBar();
mbEdit = new JMenu("Edit");
tMenuBar.add(mbEdit);
// EDIT
mUndo = new JMenuItem("Undo");
mDo = new JMenuItem("Redo");
mCut = new JMenuItem("Cut");
mCut.setIcon(iCut);
mCopy = new JMenuItem("Copy");
mCopy.setIcon(iCopy);
mPaste = new JMenuItem("Paste");
mPaste.setIcon(iPaste);
mbEdit.add(mUndo);
mbEdit.add(mDo);
mbEdit.addSeparator();
mbEdit.add(mCut);
mbEdit.add(mCopy);
mbEdit.add(mPaste);
// Undo
mUndo.setAccelerator(KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
// Redo
mDo.setAccelerator(KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_Z, ((Toolkit.getDefaultToolkit().getMenuShortcutKeyMask() | java.awt.event.InputEvent.SHIFT_MASK))));
// Cut
mCut.setAccelerator(KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_X, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
// Copy
mCopy.setAccelerator(KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_C, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
// Paste
mPaste.setAccelerator(KeyStroke.getKeyStroke(
java.awt.event.KeyEvent.VK_V, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
Already tried:
applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
I would like to make the JMenuBar black.
It should be (with the "t")
UIManager.put("MenuBar.background", Color.BLACK);
You need to set the UIManager properties "before" you create the component.
Also, the property may not be supported on all LAF's. Check out UIManager Defaults for more information and a list of the properties support by LAF.
Take a look at this post regarding the german words. I do realize that this was probably a comment rather than an answer but I'm still unable to do those due to the lack of reputation and I want to help.
Related
I'm trying to resize an existing PDF button. I want to amend the label from "Print" to "Print Amended".
PushbuttonField button = form.getNewPushbuttonFromField("HoldButton");
Rectangle box = button.getBox();
box.setRight(box.getRight() + 72); // Increase width by 1"
button.setBox(box);
button.setText("Print Amended");
form.replacePushbuttonField("HoldButton", button.getField());
The above code successfully changes the label, but not the size. The end result is a button with no change in width, and the label "Print Amended" squished together.
Is it possible to resize an existing button in iText?
I tried your example and I was surprised that I could reproduce your problem.
I looked into the iText code and I see that it is explicitly forbidden to change the /T value. This makes sense: if you want to replace an existing button, you don't want to change its name.
However, for some reason we also explicitly forbid changing the /Rect value. See the code of the AcroFields class:
for (Object element : button.getKeys()) {
PdfName key = (PdfName)element;
if (key.equals(PdfName.T) || key.equals(PdfName.RECT))
continue;
if (key.equals(PdfName.FF))
values.put(key, button.get(key));
else
widgets.put(key, button.get(key));
merged.put(key, button.get(key));
markUsed(values);
markUsed(widgets);
}
I am not sure why we made this decision when we wrote this code. If I remove || key.equals(PdfName.RECT), then your code works as expected.
As we deliberately excluded changing the dimensions of the button, I am in doubt if this is a bug or if we intentionally added that code there. Reading your requirement, I am inclined to remove || key.equals(PdfName.RECT) from the official source code.
PS: I know that this doesn't answer your question, but it does explain why your code doesn't work in spite of the fact that it looks perfectly OK. As I explained: I'm really surprised that it doesn't work, because I'm responsible for the iText code...
PS 2: I've changed the code in the official trunk.
Try something like:
newButton1 = new JButton("Print Amended") {
{
setSize(150, 75);
setMaximumSize(getSize());
}
};
or:
Try to use setMaximumSize() method
button.setMaximumSize(new Dimension(100,100));
I want to set an ImageIcon on a JButton but I don't want to set it via this way:
JButton btnTest = new JButton(new ImageIcon("IMAGE PATH"));
Is there a way I could do something like below, rather then setting it in the constructor?
btnTest.setImageIcon();
btnTest.setText(new ImageIcon().something....)
Use JButton.setIcon(Icon).
btnTest.setIcon(new ImageIcon("IMAGE PATH"));
There are also extra methods to set custom icons when the button is rolled over, pressed, selected, disabled, or different combinations of those states.
The Java API is a great source for this type of information. Bookmark it and refer to it often!
ImageIcon icon = new ImageIcon("path_to_icon");
btnTest.setIcon(icon);
Currently I have a very basic file viewer working as follows :
- in JOptionPane I browse for files, and set some variables to display (colors, line connecting etc)
- previous windows loads a frame with drawn points
alt text http://img190.imageshack.us/img190/4443/104bu.jpg
Code :
http://paste.pocoo.org/show/220066/
Now I'd like to throw it into one window, with JMenu for selecting files and changing display parameters. How to get started ? Should I rewrite everything to JDialog ?
alt text http://img684.imageshack.us/img684/5264/lab10db.jpg
If you want the JOPtionPane as a child of the main JFrame, then add it as a child. Of course it will then cover your dots. Hence you will have to not draw your dots directly in the content pane of the main JFrame, but rather in a new JPanel that you have also added to the JFRame's content pane. Let me know if I've understood the question whatsoever.
Here's some code for how I see the setup (I'm leaving the layout problem out of this, partly because it depends on what you want to see):
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(new Dimension(400,400));
frame.getContentPane().add(new JOptionPane());
JPanel canvasForDots = new JPanel();
frame.getContentPane().add(canvasForDots);
You might also like to look at How to Use Tool Bars and How to Use Menus. ImageApp is a typical implementation that associates menu items with the corresponding Action instances.
private class ClearAction extends AbstractAction {…}
private class ImageOpenAction extends AbstractAction {}
private Action openAction = new ImageOpenAction("Open");
private Action clearAction = new ClearAction("Clear");
…
JMenu menu = new JMenu("File");
menu.add(new JMenuItem(openAction));
menu.add(new JMenuItem(clearAction));
This related example adds the file chooser directly to the main frame. Here's a more elaborate example of connecting lines and shapes using the same principles.
So i have made a simple program with a basic menu at the top of the frame, Now i just need to put actions behind each JMenuItem. Im struggling to work the code out though, Here is what i thought would work:
JMenu file_Menu = new JMenu("File");
JMenuItem fileExit = new JMenuItem("Exit Program");
file_Menu.add(fileExit);
fileExit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
JFrame hello = new JFrame("POPUP");
hello.setSize(100,75);
hello.setDefaultCloseOperation(hello.EXIT_ON_CLOSE);
hello.setVisible(true);
}
});
main_Menu.add(file_Menu);
This doesn't seem to work though, I thought that this code would create a small popup window when the menu item is clicked.
Can any spot the bug because i cant seem to.
Suggestion: Instead of adding a separate ActionListener, just use AbstractAction:
JMenuItem fileExit = new JMenuItem(new AbstractAction("Exit Program") {
public void actionPerformed(ActionEvent ae) {
JFrame hello = new JFrame("POPUP");
hello.setSize(100,75);
hello.setDefaultCloseOperation(hello.EXIT_ON_CLOSE);
hello.setVisible(true);
}
});
I'd also suggest, instead of setting EXIT_ON_CLOSE on the popup menu, you set it on the main frame of your application, and have the action simply call theMainFrame.dispose().
You got it working, but you have another problem.
Don't do this:
hello.setDefaultCloseOperation(hello.EXIT_ON_CLOSE);
When you close the pop-up frame, your entire JVM terminates. Consult JFrame.setDefaultCloseOperation javadocs for a more appropriate value.
Give an instance of Action (extend from AbstractAction) to JMenuItem
Based on the code you posted it looks like it should work, but we can't see the entire context of how the menu item is being used.
Did you debug your code (with a System.out.println) to see if the ActionListener is being invoked?
If you need more help post your SSCCE that demonstrates the problem.
Fixed it.
Forgot to add the actionPerformed method.
I would like to assign a mnemonic to a JMenu using resource bundles (or the ResourceMap). So for example, the code without resource file would be...
JMenu fileMenu = new JMenu();
fileMenu.setText("File"); // this would be read from a resource file
fileMenu.setMnemonic('F'); // but the docs say this is obsolete
fileMenu.setMnemonic(KeyEvent.VK_F);
So how do I put the KeyEvent.VK_F in a resource file?
For a JMenuItem I can do it with actions, but this is JMenu.
Java's javax.swing.KeyStroke class bridges the gap:
JMenu fileMenu = new JMenu();
String mnemonic = // string from localization
fileMenu.setMnemonic(KeyStroke.getKeyStroke(mnemonic).getKeyCode());
Accelerators are not supported for JMenus, only for JMenuItems (which makes sense, since these invoke an action without using the menu at all).
Inside the resource file use the accelerator
add.Action.accelerator = control A
You could do it in a similar way, and treat "FileMenu" as a (fake) action?