Hi I've got a little Swing Application with a Menu.
First two attributes containing the menues text are created, then the lookandfeel is set to windows and at last the menues are filled.
Here is the source code:
private JMenu[] Menue={new JMenu("File")};
private JMenuItem[][] MenuItemsString ={{new JMenuItem("Import"),new JMenuItem("Export")}};
...
public window(){
super ("Q3MeshConverter");
plate = new JPanel(new BorderLayout());
try{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");// set Windows lookandfeel
}
catch(Exception x){
}
menuBar = new JMenuBar();
...
setJMenuBar(menuBar);
JMenu[] Menu =Menue;
JMenuItem[][] MenuItems =MenuItemsString;
for(int m=0;m<Menu.length;m++){// loop trough the Menu(es)
menuBar.add(Menu[m]);
for(int mi=0;mi<MenuItems[m].length;mi++){// loop through the MenuItems
Menu[m].add(MenuItems[m][mi]);
MenuItems[m][mi].addActionListener(this);
}
}
...
setContentPane (plate);
}
And that's the ugly output:
Why does it looks like this?
There is no magic how a component created before the LAF change can know about it, you have to tell it :-)
SwingUtilities.updateComponentTreeUI(someComponent);
Set the look and feel in your main method:
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) { }
}
Related
need some help with ComboBoxes in Java. Looked through similar questions, found one slightly related, but not what im dealing with.
I need to load certain arrays into combo boxes depending on the items selected in the precious combo box:
think getting some procedure done at a medical center: Choose a procedure->get a list of doctors who do it, choose a doctor->get a list of available hours etc.
A single choice is working fine(whether it's "procedure->list of doctors", or "list of doctors->their working hours"), but doing more than a single one of those changes doesn't work.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
public class GUIbandymas extends JFrame {
String[] start={"Choose","Choice1", "Choice2"};
String[] Option1={"Choose","A1"};
String[] Option2={"Choose","A2","A3"};
String[] Option3={"Choose","a","b","c","d"};
String[] Option4={"Choose","1","2","3","4"};
String[] Option5={"Choose","I","II","III","IV"};
String[] pradinis={"Pasirinkite Laika"};
String[] p1={"Pasirinkite Gydytoja"};
static double kainaR;
static double kainaK;
JComboBox<String> G=new JComboBox<String>(p1);
JComboBox<String> proc;
JComboBox<String> laikas=new JComboBox<String>(pradinis);
JComboBox<String> minutes;
JButton button = new JButton ("Registuotis");
JLabel label = new JLabel("Moketi uz vizita");
JLabel suma = new JLabel();
public GUIbandymas() throws Exception {
setValueProc(start);
frame();
}
public void frame()
{
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(500,300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.add(proc);
panel.add(G);
panel.add(laikas);
panel.add(button);
button.setEnabled(false);
//panel.add(minutes);
frame.add(panel);
panel.add(label);
panel.add(suma);
proc.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e) {
if(proc.getSelectedItem().toString().equals("Choice1"))
{
setGyd(Option1);
}
if(proc.getSelectedItem().toString().equals("Choice2"))
{
setGyd(Option2);
}
}
});
G.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent a) {
if(G.getSelectedItem().toString().equals("A1"))
{
setLaikas(Option3);
}
if(G.getSelectedItem().toString().equals("A2"))
{
setLaikas(Option4);
}
if(G.getSelectedItem().toString().equals("A3"))
{
setLaikas(Option5);
}
}
});//JComboBox
}
public void setGyd(String[] s)
{
G.removeAllItems();
for(int i=0; i<s.length; i++)
{
G.addItem(s[i]);
}
}
public void setValueProc(String[] sarasas)
{
proc=new JComboBox<String>(sarasas);
}
public void setLaikas(String[] sarasas)
{
laikas.removeAllItems();
for(int i=0; i<sarasas.length; i++)
{
laikas.addItem(sarasas[i]);
}
}
}
Im in a dire need of any suggestions and possible fixes, im inclined to think that it has something to do with action listeners, since methods do work, but im at a loss since i cant determine what is it.
EDITED: the actual code should work, seems like there is no unneeded things from other files left.
NOTE: this is work with GUI, just launch it in you main() :)
While I don't really like the if-else approach you're using, it should work just fine. I agree with rrirower's suggestion that you should look at using a data model instead. Especially if you get a lot of choices, since the code turns messy quite fast.
The problem with your code is that you run into NullPointerException when rebuilding the combobox items. The reason for this is that G.actionPerformed() is called when you remove/add items. After you have removed all items (before you start adding new ones), G.getSelectedItem() will return null.
If you code a little bit more defensively, then it works as expected:
proc.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Object selectedItem = proc.getSelectedItem();
if ("Choice1".equals(selectedItem)) {
setGyd(Option1);
}
if ("Choice2".equals(selectedItem)) {
setGyd(Option2);
}
}
});
G.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent a) {
Object selectedItem = G.getSelectedItem();
if ("A1".equals(selectedItem)) {
setLaikas(Option3);
}
if ("A2".equals(selectedItem)) {
setLaikas(Option4);
}
if ("A3".equals(selectedItem)) {
setLaikas(Option5);
}
}
});//JComboBox
Instead of checking for null's, I just flipped the equals and skipped the unnecessary toString() (they are already strings, that's what you put in there).
Another thing, a pet peeve of mine, please follow the normal java code convention for all your class, field and method names. You're almost there, but Option1 etc. should start with lowercase. G should probably have a more descriptive name, as well as start with lowercase.
Finally, I didn't understand why you both create a JFrame in the constructor and extend JFrame in your class. You should choose one or the other.
You don't appear to be using a data model for the combo box. The data model controls the internal list of items. Have a look at this for more info.
I was wondering why Nimbus would be conflicting somehow with Virtual keys. Check out the sample I made below:
public class buttontest implements ActionListener {
JMenuItem close =new JMenuItem("Close");
public static void main (String[] args){
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (UnsupportedLookAndFeelException e) {
// handle exception
} catch (ClassNotFoundException e) {
// handle exception
} catch (InstantiationException e) {
// handle exception
} catch (IllegalAccessException e) {
// handle exception
}
}
public buttontest(){
JFrame test = new JFrame();
JMenuBar bar=new JMenuBar();
JMenu file=new JMenu("File");
close.setMnemonic(KeyEvent.VK_C);
file.setMnemonic(KeyEvent.VK_F);
test.setExtendedState(test.getExtendedState() | test.MAXIMIZED_BOTH); // Maximized Window or setSize(getMaximumSize())
test.setDefaultCloseOperation(1);
bar.add(file);
file.add(close);
test.setJMenuBar(bar);
test.setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==close){
System.exit(0);
}
}
}
The way its wrote, you can try to use the Virtual keys. You will see that Alt F works to open the File menu but Alt C doesnt close the application. In other way, if you comment the Nimbus code both virtual Keys will work.
I made one research regarding to this "bug" (or maybe something wrong Im doing that Im not aware) but until now I found nothing. Has someone ever passed through this?
You have to use setAccelerator() method for JMenuItem:
close.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C, InputEvent.ALT_MASK ));
From Javadoc:
JMenuItem#setAccelerator(KeyStroke)
Sets the key combination which
invokes the menu item's action listeners without navigating the menu
hierarchy. It is the UI's responsibility to install the correct
action. Note that when the keyboard accelerator is typed, it will work
whether or not the menu is currently displayed.
Additional note:
Replace LookAndFeelInfo to UIManager.LookAndFeelInfo as it's an inner class inside UIManager.
Call the constructor in the main method.
Change the parameter of setDefaultCloseOperation(1) to 3 as 3 = JFrame.EXIT_ON_CLOSE, but 1=JFrame.HIDE_ON_CLOSE which hides the frame, personally, I hate it, because close button created for closing frame, not hiding it, like Skype.
Add actionListener to close button :close.addActionListener(this);
Snippet of code is here:
int area;
int[] xcoords = new int[3];
xcoords[0] = coordsAX;
xcoords[1] = coordsBX;
xcoords[2] = coordsCX;
sortArray(xcoords);
int[] ycoords = new int[3];
ycoords[0] = coordsAY;
ycoords[1] = coordsBY;
ycoords[2] = coordsCY;
sortArray(ycoords);
//Remember, array[0] is the biggest and array[2] is the smallest!
int rectWidth = xcoords[0] - xcoords[2];
int rectHeight = ycoords[0] - ycoords[2];
area = (rectWidth * rectHeight);
System.out.println(area);
lblArea.setText("Area: " + area);
The entirety of the code is within the paint(g) method of my applet. I'm aiming to let the user be able to see the JLabel. Calculations go absolutely fine. But when I run, the applet looks like:
I've gathered that the setText line should not be in paint(g) but, in that case, where should it go in order to make it so the JLabel remains the same until a new triangle is generated (by clicking "Click Me" button)?
Please note that I'm a high schooler teaching myself Java and, as a result, my knowledge of the language looks like a hunk of Swiss Cheese. I'd appreciate explanations that don't explain too many topics that are well above the level of basic applet making. :)
Appreciate any help! Thanks!
Presumably you have an action listener attached to the "click me" button.
When the action is fired, I would update the label and the UI at that point.
You might like to have a read through How to Write an Action Listener
(I'm also kind of worried that it looks like you're using AWT instead of Swing, but I could be mistaken ;))
UPDATED example
public class TestArea {
public static void main(String[] args) {
new TestArea();
}
public TestArea() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new AreaPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class AreaPane extends JPanel {
private JLabel areaLabel;
public AreaPane() {
areaLabel = new JLabel("Area: ...");
JButton clickMe = new JButton("Click Me");
clickMe.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
areaLabel.setText("Area: " + NumberFormat.getNumberInstance().format(Math.random() * 1000));
// update UI as required
}
});
add(areaLabel);
add(clickMe);
}
}
}
Does anyone know how to control the display of the tiny little arrow that appears on submenus of JMenu?
Can I change it?
Can I disable it?
Can I move it?
Also, I notice that this arrow doesn't appear on top level JMenus only when they are submenus of other JMenu. This inconsistency annoys me since I have a mix of JMenuItem and JMenu attached to the root of my JMenuBar and so I wish it would always indicate it. Anyway to do this as well?
thanks!
Take a look at the Menu.arrowIcon UI property
(Thanks to AndrewThompson for the test code).
Doining this will effect ALL the menus created AFTER you apply the modifications.
So after you init Look and Feel and before you create any menus call UIManager.getLookAndFeelDefaults().put("Menu.arrowIcon", null);
I'd just like to say I think this is a terrible idea and would highly discourage you from doing it.
this arrow doesn't appear on top level JMenus only when they are submenus of other JMenu.
It seem (monotonously) consistent in its appearance using Metal here.
import javax.swing.*;
public class MenuArrows {
MenuArrows() {
JMenuBar mb = new JMenuBar();
JMenu root1 = new JMenu("Root Menu 1");
JMenu root2 = new JMenu("Root Menu 2");
addSubMenus(root1, 5);
addSubMenus(root2, 3);
mb.add(root1);
mb.add(root2);
JOptionPane.showMessageDialog(null, mb);
}
public void addSubMenus(JMenu parent, int number) {
for (int i=1; i<=number; i++) {
JMenu menu = new JMenu("Sub Menu " + i);
parent.add(menu);
addSubMenus(menu, number-1);
addMenuItems(menu, number);
}
}
public void addMenuItems(JMenu parent, int number) {
for(int i=1; i<=number; i++) {
parent.add(new JMenuItem("Item " + i));
}
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
new MenuArrows();
}
};
SwingUtilities.invokeLater(r);
}
}
Hey StackOverflow community, I'm having a StackOverflow problem.
I'm having difficulty adding a new tab to my GUI's JTabbedPane container, when the [+] tab is selected. So far, whenever I click the [+] tab, new tabs are appended until a StackOverflowError occurs.
A new tab is added to the JTabbedPane when the following condition is true.
if(songPanel.getSelectedIndex()==songPanel.getTabCount()-1){
...
}
I've tried to revert back to the previously selected tab to avoid tabs being added repeatedly to the JTabbedPane, to no avail. When the ChangeEvent actuator is fired, does it stay on indefinitely? I haven't come across anything useful in the SE7 API.
Relevant code (non-compilable, excerpt from larger program. May be missing brackets, only because I copy-pasted excerpts of the code, and liable to make mistakes)
#Override
public void init(){
setLayout(new GridLayout(MAIN_LAYOUT_ROWS, MAIN_LAYOUT_COLUMNS));
add(renderPanel = new JScrollPane());
add(controlPanel = new JPanel());
add(colourPanel = new JPanel());
add(songPanel = new JTabbedPane());
//songPanel options
songPanel = new JTabbedPane();
songPanel.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
songPanel.addTab("#1", new JTextArea());
songPanel.addTab("+", null, new JLabel(), "+");
Container cp = getContentPane();
cp.add(BorderLayout.SOUTH, songPanel);
//integrate songPanel changeListener
songPanel.addChangeListener(new ChangeListener(){
#Override //Method called when selected tab changes
public void stateChanged(ChangeEvent e){
try {
if(songPanel.getSelectedIndex()==songPanel.getTabCount()-1){
addTab("songPanel");
}
} catch (StackOverflowError soe){soe.printStackTrace();}
}
});
//*************************************************************************
#Override
public void start(){
}
//*************************************************************************
private void addTab(String panelName){
System.out.println("ADDING TAB");
if(panelName.equals("songPanel")){
String tabName = ("#" + Integer.toString(songPanel.getTabCount()-1));
songPanel.insertTab(tabName, null, new JTextField(), tabName, songPanel.getTabCount()-2);
}
}
}
//**************************************************************************
}
I've tried:
Setting a revert index in the addTab() method, so the newest tab is selected (still results in StackOverflowError)
Note this line:
songPanel.getSelectedIndex()==songPanel.getTabCount()-1)
Both "songPanel.getSelectedIndex()" and "songPanel.getTabCount()-1)" are always equal, so condition is always true (causing the StackOverflowError)
Error message:
java.lang.StackOverflowError
at javax.swing.text.StyleContext$SmallAttributeSet.getAttributeNames(StyleContext.java:947)
at javax.swing.text.StyleContext$SmallAttributeSet.containsAttributes(StyleContext.java:973)
at javax.swing.text.StyleContext$SmallAttributeSet.equals(StyleContext.java:852)
at java.util.WeakHashMap.eq(WeakHashMap.java:282)
at java.util.WeakHashMap.get(WeakHashMap.java:379)
at java.util.Collections$SynchronizedMap.get(Collections.java:2031)
at javax.swing.text.StyleContext.getImmutableUniqueSet(StyleContext.java:520)
at javax.swing.text.StyleContext.addAttributes(StyleContext.java:340)
at javax.swing.text.AbstractDocument$AbstractElement.addAttributes(AbstractDocument.java:1985)
at javax.swing.text.AbstractDocument$AbstractElement.<init>(AbstractDocument.java:1777)
at javax.swing.text.AbstractDocument$LeafElement.<init>(AbstractDocument.java:2502)
at javax.swing.text.AbstractDocument$BidiElement.<init>(AbstractDocument.java:2674)
at javax.swing.text.AbstractDocument.<init>(AbstractDocument.java:149)
at javax.swing.text.AbstractDocument.<init>(AbstractDocument.java:109)
at javax.swing.text.PlainDocument.<init>(PlainDocument.java:90)
at javax.swing.text.PlainDocument.<init>(PlainDocument.java:80)
at javax.swing.text.DefaultEditorKit.createDefaultDocument(DefaultEditorKit.java:130)
at javax.swing.plaf.basic.BasicTextUI.installUI(BasicTextUI.java:799)
at javax.swing.JComponent.setUI(JComponent.java:655)
at javax.swing.text.JTextComponent.setUI(JTextComponent.java:338)
at javax.swing.text.JTextComponent.updateUI(JTextComponent.java:348)
at javax.swing.text.JTextComponent.<init>(JTextComponent.java:322)
at javax.swing.JTextField.<init>(JTextField.java:231)
at javax.swing.JTextField.<init>(JTextField.java:172)
at application.Analyzer.addTab(Analyzer.java:133)
at application.Analyzer.access$100(Analyzer.java:24)
at application.Analyzer$1.stateChanged(Analyzer.java:101)
at javax.swing.JTabbedPane.fireStateChanged(JTabbedPane.java:416)
at javax.swing.JTabbedPane$ModelListener.stateChanged(JTabbedPane.java:270)
at javax.swing.DefaultSingleSelectionModel.fireStateChanged(DefaultSingleSelectionModel.java:132)
at javax.swing.DefaultSingleSelectionModel.setSelectedIndex(DefaultSingleSelectionModel.java:67)
at javax.swing.JTabbedPane.setSelectedIndexImpl(JTabbedPane.java:616)
at javax.swing.JTabbedPane.insertTab(JTabbedPane.java:735)
at application.Analyzer.addTab(Analyzer.java:133)
at application.Analyzer.access$100(Analyzer.java:24)
.
.
.
Do you have any suggestions? I know it's kind of vague, but I'm really not sure what is going wrong.
Any suggestions?
Thanks.
Tyler
StackOverflow identifies an infinite recursion. So first thing is to find that recursion. In your case these are the lines of the stacktrace that identifies that recursion:
at application.Analyzer.addTab(Analyzer.java:133) at
application.Analyzer.access$100(Analyzer.java:24) at
application.Analyzer$1.stateChanged(Analyzer.java:101) at
javax.swing.JTabbedPane.fireStateChanged(JTabbedPane.java:416) at
javax.swing.JTabbedPane$ModelListener.stateChanged(JTabbedPane.java:270)
at
javax.swing.DefaultSingleSelectionModel.fireStateChanged(DefaultSingleSelectionModel.java:132)
at
javax.swing.DefaultSingleSelectionModel.setSelectedIndex(DefaultSingleSelectionModel.java:67)
at javax.swing.JTabbedPane.setSelectedIndexImpl(JTabbedPane.java:616)
at javax.swing.JTabbedPane.insertTab(JTabbedPane.java:735) at
application.Analyzer.addTab(Analyzer.java:133)
So when you insert a tab, it automatically triggers a change of selected tab which in turns calls your ChangeEventListener which will trigger the insertion of a tab etc...
So you have two simple solutions:
Use a flag (a boolean) that is set to true before you add the new tab and that you set back to false when you are done. In your condition to test if you need to add a tab, you also check that this flag is not true.
You remove your change listener from the JTabbedPane before you insert the tab and you put it back afterwards.
In both case, use a try/finally block to make sure to return to a consistent state.
UPDATED SOLUTION
Sorry, the previous solution is not working as expected. Here my updated one:
public class TabbedPaneTest {
private final static JButton ADD_NEW_TAB_BUTTON = new JButton();
private JFrame mainFrame;
private JTabbedPane tabbedPane;
public void run() {
mainFrame = new JFrame("Test JTabbedPane");
mainFrame.setSize(300, 400);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
tabbedPane = new JTabbedPane();
tabbedPane.addTab("default new tab", new JLabel("this is a default tab"));
addNewTabButton();
tabbedPane.addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
if (tabbedPane.getSelectedComponent() == ADD_NEW_TAB_BUTTON) {
tabbedPane.addTab("new tab", new JLabel("new tab label"));
addNewTabButton();
}
}
});
mainFrame.getContentPane().add(tabbedPane);
mainFrame.setVisible(true);
}
private void addNewTabButton() {
tabbedPane.remove(ADD_NEW_TAB_BUTTON);
tabbedPane.addTab("[+]", ADD_NEW_TAB_BUTTON);
}
public static void main(String[] params) {
TabbedPaneTest test = new TabbedPaneTest();
test.run();
}
}
The problem is that the changeListener is called after the adding once again with the + tab as the selected tab, causing the creation of a new tab, and so on.
a pretty simple solution could be just to add a bool flag as Guillaume Polet stated :
songPanel.addChangeListener(new ChangeListener(){
#Override //Method called when selected tab changes
public void stateChanged(ChangeEvent e){
try {
if(songPanel.getSelectedIndex()==songPanel.getTabCount()-1 && !adding){
adding = true;
addTab("songPanel");
adding = false;
}
} catch (StackOverflowError soe){soe.printStackTrace();}
}
});
The adding flag is a class field initialized to false, and indicates whether or not you are in progress of adding a tab.
Minor changes to the addTab to get everything to work:
private void addTab(String panelName){
System.out.println("ADDING TAB");
if(panelName.equals("songPanel")){
String tabName = ("#" + Integer.toString(songPanel.getTabCount()));
int index = songPanel.getTabCount()-1;
songPanel.insertTab(tabName, null, new JTextField(), tabName, index);
songPanel.setSelectedIndex(index);
}
}
I made a little change in code, making the active tab the newly created one, and the indexes where a bit off.
Hope this helps :)