i am working with swing gui.
I have a simple Jframe with a tabbed pane with two tabs. When i click a non selected Tab, and don't move the mouse, nothing happens. i.e. the hidden tab does not appear. But, if i click the unselected tab, then mouse over to a button(without clicking button) the tab repaints.
I also see this behavior with toggle buttons too. I toggle the text of a button based on the state but it doesn't show/repaint until i move focus to another button.
Am i supposed to be calling a repaint for the whole Jframe after every event? or am i not processing events properly?
Related
I am designing a game for a project in Java Swing using a JForm. I know that you have to use the code
jPanel1.setVisible(true);
I used this for all panels, even in the init() method in the source.
I am using the "Layered Panel" option for every panel I add. I placed the image on the first panel:
There are radio buttons in my second panel. I want the user to click on the option while looking at the picture, kind of like clicking on an icon (radio button) on desktop (background image).
Like this:
When I try to look at my design preview, it just shows me Panel2 (only radio buttons), not 1.
Is it possible to fix this (make two panels visible at once), or should I just make a button to switch between the panels?
I'm getting some trouble with a toolbar I made. In this I have 4 JButtons and below a JTable, which is set on focus when my windows shows in order to see the JButtons unfilled.
This is how I set each JButton:
btn = new JButton("New");
btn.setFocusPainted(false);
btn.setBackground(SystemColor.window);
btn.setVerticalTextPosition(SwingConstants.BOTTOM);
btn.setHorizontalTextPosition(SwingConstants.CENTER);
btn.addActionListener(new ActionListener() {
...
});
btn.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
toolBar_General.add(btn);
btn.setIcon(new ImageIcon(MainWindow.class.getResource("/icons/file11.png")));
Example of my toolbar at start: http://www.tiikoni.com/tis/view/?id=0ef950b
But after clicking on a JButton, it remains filled after itsactionListener does his work.
Example of my toolbar after clicking a JButton: http://www.tiikoni.com/tis/view/?id=9e88ea4
My question is: how can I mantain my buttons contentAreaFilled property true (so the user can tab between components in my window and see which control he has selected) and unfocus my button after the user click?
The result I would like to have is that of the first photo.
I think the call you are looking for is:
btn.setRequestFocusEnabled(false);
this will stop your buttons from taking over the focus when the mouse clicks them, achieving the goal of not having the keyboard focus painted for mouse users. However it still lets keyboard-centric users "tab over" to them.
If one of your buttons gets the initial focus, however, then the keyboard focus will still render there. In that case, you probably want to select some other sensible component to place the initial keyboard focus onto by calling:
otherComponent.requestFocusInWindow();
Are you talking about the input Focus? (the dotted rectangle a component shows when it has the focus)
You can explicitly disable a component getting the focus by calling setFocusable(false) on the component (e.g. every button). It does not prevent you from clicking on the buttons, but it does prevent you from cycling the focus with the tab-key to it.
I am extending the JButton class and using an ImageIcon as my button background. I have also added a mouseListener so I can change the image of the button when the mouse hovers over the button or presses the button. This all works fine but the problem is that when the button is clicked a JOptionPane is displayed asking the user "Are you sure" while the option pane is displayed the buttons MouseClicked, mouseExited or mouseReleased methods are not executed so it looks like the button is stuck pressed until the use deals with the option dialog. Removing the dialog is not an option. Is there a way to get the mouse events executed before or during the dialog displaying?
I have also added a mouseListener so I can change the image of the button when the mouse hovers over the button or presses the button.
Read the AbstractButton API. There are methods that allow you to specify the Icon for these situations. For examples setPressedIcon().
it looks like the button is stuck pressed until the use deals with the option dialog.
Not sure if the ButtonModel state has been changed before the option pane is displayed or not. Anyway you can try using:
button.paintImmediately(...);
before the option pane is displayed. If the ButtonModel state has been updated the button should be repainted in its normal state. Note, this method should rarely be used since it can affect painting performance.
So I have a UI that I made with Swing using Java. It is very complex and I want to save the user some time by making the UI simpler.
Imagine you have a page with many CheckBox. Once you select whatever CheckBox you desire, the next button makes a JFrame appear with the same tabs as you selected inside a Tabbed Pane.
Here is a small sample of my code which should give an idea of what I want the button to do:
btnNewButton_7.addActionListener(new ActionListener(){
#SuppressWarnings("static-access")
public void actionPerformed(ActionEvent e){
Admin.set.frame.setVisible(false);
Admin.adv.setVisible(true);
Admin.adv.jp2.setVisible(chckbxNewCheckBox_42.isSelected());
Admin.adv.jp3.setVisible(chckbxNewCheckBox_43.isSelected());
Admin.adv.jp4.setVisible(chckbxNewCheckBox_44.isSelected());
}
});
So the next Button should set the JFrame (set.frame) invisible. Then, it should open adv (a new frame). So far, everything is working. Now, my tabe are JPanel inside adv. I would love to make them disappear whenever the CheckBox 42,43 and 44 are selected or unselected, should I add an onChangeListener?
I have a Java Applet that loads a record of a board game from a URL and displays the configuration using a series of panels laid out in a grid and nested in a JPanel. This is a small component of an overall applet with text fields and such for inputs and outputs. When a button is pressed, I want the board to show the next configuration from the record. I figured out a way to do this, but when the button is pressed, the same configuration is shown, but when I scroll up or down, or resize the window, then the configuration changes. Is there a way to "refresh" just the board JPanel when the button is pressed? Currently, when the button is pressed, the new configuration panel is created, the old configuration is removed, and the new configuration is added to the display.
Thank you very much!
If nothing else, you could try hiding and then immediately showing the panel again. That should force it to redraw.