Hello all I am trying to make a new software that it gives me modulo of any number I gave. What I want is I put a JCheckBox on my gui and when it's checked the window should be
setAlwaysOnTop(true);
and when deselected
setAlwaysOnTop(false);
Some of my code is
boolean top = false;
Check = new JCheckBox("Always on top");
Check.setLocation(140, 105);
Check.setSize(150, 20);
Check.setSelected(top);
Check.addItemListener(new CheckBoxListener());
add(Check);
setAlwaysOnTop(top);
private class CheckBoxListener implements ItemListener{
public void itemStateChanged(ItemEvent e){
if(e.getSource()==Check){
if(Check.isSelected()){
top = true;
}else{
top = false;
}
}
}
}
setAlwaysOnTop does not observe further state changes to your boolean top. It takes the value of top when it is passed.
In your listener, write:
if(e.getSource() == Check) {
setAlwaysOnTop(Check.isSelected());
}
Change:
public void itemStateChanged(ItemEvent e){
if(e.getSource()==Check){
if(Check.isSelected()){
top = true;
}else{
top = false;
}
}
}
to
public void itemStateChanged(ItemEvent e){
if(e.getSource()==Check){
setAlwaysOnTop(Check.isSelected());
}
}
}
(Also, look into the various LayoutManagers in Java; don't make a GUI with fixed positioning.)
Related
I have a part of my code:
while(_running){
// render screens
if( _input.escape) {
if( isPaused ) {
pauseDialog.setVisible(false);
remuse(); // set isPaused = false, _running = true and render screens
}
else {
pause(); // set isPaused = true and _running = false and render screens
pauseDialog.setVisible(true);
}
}
}
and _input like this
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
}
#Override
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
}
and pauseDialog
public class PauseDialog extends JDialog{
JButton b1,b2;
public PauseDialog() {
setLayout(new GridLayout(2, 1,8,8));
setSize(new Dimension(85, 180));
setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
b1 = new JButton("resume");
b2 = new JButton("exit");
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("resume");
}
});
b2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("exit");
}
});
add(b1); add(b2);
}
but if i setvisible(true) for pauseDialog when escape key is released, variable _input.escape is always set true value, so dialog appears and disappears and appears again ... like an infinite loop. what should i do next? thanks.
Set "_input.escape" to False, after Escape is pressed?
Or does "_input.escape" need to be True for pausing to happen?
If so, that implies something's wrong, 'cause you shouldn't need to hold down Escape to keep the game paused?
It just seems you need a good logic system so that the dialog isn't being triggered more than once.
Maybe check if it's already triggered and don't trigger it again?
My question states it all.
Here is my code:
fullscreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
MainMenu.isFullscreen = true;
}
});
Where fullscreen is a JButton.
And then in my Screen class:
if(mm.isFullscreen) {
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
repaint();
}
With mm being a deceleration of MainMenu. When mm.isFullscreen is false the Screen is its normal size.
I think I read somewhere that ActionListeners can't change the value of something outside the ActionListener...?
edit:
I have fixed the isue thanks for the help but this is new code:
In the screen class:
public void setFullscreen() {
GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(this);
repaint();
}
In the mainMenu class:
fullscreen.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(isFullscreen) {
isFullscreen = false;
}
if(!isFullscreen) {
isFullscreen = true;
screen.setFullScreen();
}
});
When setting isFullscreen = false; you are not changing the state of full screen window!
You may use GraphicsDevice#setFullScreenWindow(null); to set it back to windowed mode.
I am looking to be able to verify that a JTextField contains only 'numbers' and no (+ , -) before sending the content of this JTextField in a Sql query (to avoid a SqlException).
I want :
if I enter a letter, a JLabel is displayed and the color of JTextField changes in red.
label_errer.setVisible (true);
if I delete the letter, the JLabel will disappear and the color of JTextField is normal.
label_errer.setVisible (false);
the following code works if I click "enter":
textField_app = new JTextField(3);
textField_app.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JTextField source = (JTextField) e.getSource();
String textFieldContent = source.getText();
Color bgColor = Color.RED;
boolean isNumeric=false;
try {
Integer.parseInt(textFieldContent);
isNumeric = true;
bgColor = Color.WHITE;
label_errA.setVisible(false);
} catch (Exception e2) {
// ---> isNumeric=false
}
source.setBackground(bgColor);
if(isNumeric==false){
label_errA.setEnabled(true);
label_errA.setVisible(true);
}
}
});
this solution works, but is there another?
textField_app = new JTextField(3);
KeyListener keyListener = new KeyListener() {
public void keyPressed(KeyEvent keyEvent) {
printIt("Pressed", keyEvent);
}
public void keyReleased(KeyEvent keyEvent) {
printIt("Released", keyEvent);
}
public void keyTyped(KeyEvent keyEvent) {
printIt("Typed", keyEvent);
}
private void printIt(String title, KeyEvent keyEvent) {
int keyCode = keyEvent.getKeyCode();
String keyText = KeyEvent.getKeyText(keyCode).toString();
if(keyCode==(getKeyBinding(keyText))){
textField_app.setBackground(new Color(220, 20, 60));
label_errA.setEnabled(true);
label_errA.setVisible(true);
}
else {
Color bgColor =Color.WHITE;
textField_app.setBackground(bgColor);
label_errA.setEnabled(false);
label_errA.setVisible(false);
}
}
};
public int getKeyBinding(String k){
if(k.equals("A")){
return KeyEvent.VK_A;
} else if(k.equals("B")){
return KeyEvent.VK_B;
} else if(k.equals("C")){
return KeyEvent.VK_C;
} else if(k.equals("D")){
return KeyEvent.VK_D;
} else if(k.equals("E")){
return KeyEvent.VK_E;
} else if(k.equals("F")){
return KeyEvent.VK_F;
} else if(k.equals("G")){
return KeyEvent.VK_G;
} else if(k.equals("H")){
return KeyEvent.VK_H;
} else if(k.equals("I")){
return KeyEvent.VK_I;
} else if(k.equals("J")){
return KeyEvent.VK_J;
} else if(k.equals("K")){
return KeyEvent.VK_K;
} else if(k.equals("L")){
return KeyEvent.VK_L;
} else if(k.equals("M")){
return KeyEvent.VK_M;
} else if(k.equals("N")){
return KeyEvent.VK_N;
} else if(k.equals("O")){
return KeyEvent.VK_O;
} else if(k.equals("P")){
return KeyEvent.VK_P;
} else if(k.equals("Q")){
return KeyEvent.VK_Q;
} else if(k.equals("R")){
return KeyEvent.VK_R;
} else if(k.equals("S")){
return KeyEvent.VK_S;
} else if(k.equals("T")){
return KeyEvent.VK_T;
} else if(k.equals("U")){
return KeyEvent.VK_U;
} else if(k.equals("V")){
return KeyEvent.VK_V;
} else if(k.equals("W")){
return KeyEvent.VK_W;
} else if(k.equals("X")){
return KeyEvent.VK_X;
} else if(k.equals("Y")){
return KeyEvent.VK_Y;
} else if(k.equals("Z")){
return KeyEvent.VK_Z;
}
else{
return 0;
}
}
You are looking for a JFormattedTextField. You have all the information here to get yourself started.
What you should probably do to detect every key press is to use a DocumentListener. But since your goal is validation, take a look at using a DocumentFilter instead. It's a little more complicated, but it's a cleaner way to do it, and you won't get any concurrent modification exceptions.
You can create a DocumentFilter, and then every key press will need to pass the filter's inspection (a rough way of putting it, but fairly accurate). If the filter deems it OK, it puts it through. You can also add any actions of your own, such as turning the field red, as you mentioned.
I think you are looking for the java.awt.event.KeyListener instead of ActionListener. Use the KeyTyped() function.
I wrote a library that I can give you a link to if you want, which gives you Textfields that can only take numbers and nothing else.
Yes, you can look at the source too, no problem.
I could really use a tri-stated checkbox in Java. It sounds like a simple thing, but I've only seen really ugly implementations [note: link now broken].
Three radio buttons just take up too much real estate and will probably be confusing for the users in my case. It's basically for a search dialog. I need true, false or "don't care" options. Is there a different technique that people use?
Use a drop-down.
I found a way to make a tri-state checkbox by simply adding a listener:
public class TriStateActionListener implements ActionListener{
final protected Icon icon;
public TriStateActionListener(Icon icon){
this.icon=icon;
}
public static Boolean getState(javax.swing.JCheckBox cb){
if (cb.getIcon()==null) return null;
if (cb.isSelected()) return true;
else return false;
}
public void actionPerformed(ActionEvent e) {
javax.swing.JCheckBox cb=(javax.swing.JCheckBox)e.getSource();
if (!cb.isSelected()){
cb.setIcon(icon);
}
else if (cb.getIcon()!=null){
cb.setIcon(null);
cb.setSelected(false);
}
}
}
Then in the application code, it's just a single line:
jCheckBox1.addActionListener(new TriStateActionListener(getResourceMap().getIcon("TriStateIcon")));
After all the feedback, I'm thinking a drop-down may be a better choice. But, I wanted to share my code here for everyone else.
In this implementation the three state can be only set via programmatically. To be Look and Feel portable it use images, that must be placed inside the the same java package.
public class TristateCheckBox extends JCheckBox {
private static final long serialVersionUID = 1L;
private boolean halfState;
private static Icon selected = new javax.swing.ImageIcon(TristateCheckBox.class.getResource("selected.png"));
private static Icon unselected = new javax.swing.ImageIcon(TristateCheckBox.class.getResource("unselected.png"));
private static Icon halfselected = new javax.swing.ImageIcon(TristateCheckBox.class.getResource("halfselected.png"));
#Override
public void paint(Graphics g) {
if (isSelected()) {
halfState = false;
}
setIcon(halfState ? halfselected : isSelected() ? selected : unselected);
super.paint(g);
}
public boolean isHalfSelected() {
return halfState;
}
public void setHalfSelected(boolean halfState) {
this.halfState = halfState;
if (halfState) {
setSelected(false);
repaint();
}
}
}
Sample frame:
public class NewJFrame19 extends javax.swing.JFrame {
private final TristateCheckBox myCheckBox;
public NewJFrame19() {
myCheckBox = new TristateCheckBox();
myCheckBox.setText("123123");
add(myCheckBox);
jButton1 = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
getContentPane().setLayout(new java.awt.FlowLayout());
jButton1.setText("jButton1");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
getContentPane().add(jButton1);
pack();
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
myCheckBox.setHalfSelected(true);
}
public static void main(String args[]) {
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Windows".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(NewJFrame19.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(NewJFrame19.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(NewJFrame19.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(NewJFrame19.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame19().setVisible(true);
}
});
}
private javax.swing.JButton jButton1;
}
JIDE have open sourced some very nice functionality in their Common Layer, one of which happens to be a tristate checkbox
I would suggest that you go run the webstart demo to see if it meets your needs
That "ugly implementations" is an old link. One of the suggestions on that page was updated a couple of years ago. I haven't tested the old implementation, so I don't know if the new one is any better or worse.
TristateCheckBox Revisited
Tristate check-boxes are standard UI idiom for Treeviews with partially checked children nodes. They are widely used in layer management in complex hierarchial views such as Google Earth.
I dont know why anyone would give the solutions with additional icon png files while java api gives great funcionality for overriding paintIcon(..) method.
The best lightweight solution to remember CheckBox state is IMO ClientProperty attribute.
/*
* Tri-state checkbox example
* #s1w_
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class TCheckBox extends JCheckBox implements Icon, ActionListener {
final static boolean MIDasSELECTED = true; //consider mid-state as selected ?
public TCheckBox() { this(""); }
public TCheckBox(String text) {
super(text);
putClientProperty("SelectionState", 0);
setIcon(this);
addActionListener(this);
}
public TCheckBox(String text, int sel) {
/* tri-state checkbox has 3 selection states:
* 0 unselected
* 1 mid-state selection
* 2 fully selected
*/
super(text, sel > 1 ? true : false);
switch (sel) {
case 2: setSelected(true);
case 1:
case 0:
putClientProperty("SelectionState", sel);
break;
default:
throw new IllegalArgumentException();
}
addActionListener(this);
setIcon(this);
}
#Override
public boolean isSelected() {
if (MIDasSELECTED && (getSelectionState() > 0)) return true;
else return super.isSelected();
}
public int getSelectionState() {
return (getClientProperty("SelectionState") != null ? (int)getClientProperty("SelectionState") :
super.isSelected() ? 2 :
0);
}
public void setSelectionState(int sel) {
switch (sel) {
case 2: setSelected(true);
break;
case 1:
case 0: setSelected(false);
break;
default: throw new IllegalArgumentException();
}
putClientProperty("SelectionState", sel);
}
final static Icon icon = UIManager.getIcon("CheckBox.icon");
#Override
public void paintIcon(Component c, Graphics g, int x, int y) {
icon.paintIcon(c, g, x, y);
if (getSelectionState() != 1) return;
int w = getIconWidth();
int h = getIconHeight();
g.setColor(c.isEnabled() ? new Color(51, 51, 51) : new Color(122, 138, 153));
g.fillRect(x+4, y+4, w-8, h-8);
if (!c.isEnabled()) return;
g.setColor(new Color(81, 81, 81));
g.drawRect(x+4, y+4, w-9, h-9);
}
#Override
public int getIconWidth() {
return icon.getIconWidth();
}
#Override
public int getIconHeight() {
return icon.getIconHeight();
}
public void actionPerformed(ActionEvent e) {
TCheckBox tcb = (TCheckBox)e.getSource();
if (tcb.getSelectionState() == 0)
tcb.setSelected(false);
tcb.putClientProperty("SelectionState", tcb.getSelectionState() == 2 ? 0 :
tcb.getSelectionState() + 1);
// test
System.out.println(">>>>IS SELECTED: "+tcb.isSelected());
System.out.println(">>>>IN MID STATE: "+(tcb.getSelectionState()==1));
}
}
usage: TCheckBox tcb = new TCheckBox("My CheckBox");
I'd just use the one you posted.
As long as your complexity is in another class (that works) and it acts just like any other control, who cares? (That seems to be the assumption behind all of swing, most swing classes seem to be about this complicated.)
Change the UI. Tristate check-box is unusual and can really confuse users. The drop down is good option but for more then one occurrence within dialog it will also bring a lot of confusion to user.
I needed a JButton with an attached dropdown style menu. So I took a JPopupMenu and attached it to the JButton in the way you can see in the code below. What it needs to do is this:
show the popup when clicked
hide it if clicked a second time
hide it if an item is selected in the popup
hide it if the user clicks somewhere else in the screen
These 4 things work, but because of the boolean flag I'm using, if the user clicks somewhere else or selects an item, I have to click twice on the button before it shows up again. That's why I tried to add a FocusListener (which is absolutely not responding) to fix that and set the flag false in these cases.
EDIT: Last attempt in an answer post...
Here are the listeners: (It's in a class extending JButton, so the second listener is on the JButton.)
// Show popup on left click.
menu.addFocusListener(new FocusListener() {
#Override
public void focusLost(FocusEvent e) {
System.out.println("LOST FOCUS");
isShowingPopup = false;
}
#Override
public void focusGained(FocusEvent e) {
System.out.println("GAINED FOCUS");
}
});
addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("isShowingPopup: " + isShowingPopup);
if (isShowingPopup) {
isShowingPopup = false;
} else {
Component c = (Component) e.getSource();
menu.show(c, -1, c.getHeight());
isShowingPopup = true;
}
}
});
I've been fighting with this for way too long now. If someone can give me a clue about what's wrong with this, it would be great!
Thanks!
Code:
public class Button extends JButton {
// Icon.
private static final ImageIcon ARROW_SOUTH = new ImageIcon("ArrowSouth.png");
// Unit popup menu.
private final JPopupMenu menu;
// Is the popup showing or not?
private boolean isShowingPopup = false;
public Button(int height) {
super(ARROW_SOUTH);
menu = new JPopupMenu(); // menu is populated somewhere else
// FocusListener on the JPopupMenu
menu.addFocusListener(new FocusListener() {
#Override
public void focusLost(FocusEvent e) {
System.out.println("LOST FOCUS");
isShowingPopup = false;
}
#Override
public void focusGained(FocusEvent e) {
System.out.println("GAINED FOCUS");
}
});
// ComponentListener on the JPopupMenu
menu.addComponentListener(new ComponentListener() {
#Override
public void componentShown(ComponentEvent e) {
System.out.println("SHOWN");
}
#Override
public void componentResized(ComponentEvent e) {
System.out.println("RESIZED");
}
#Override
public void componentMoved(ComponentEvent e) {
System.out.println("MOVED");
}
#Override
public void componentHidden(ComponentEvent e) {
System.out.println("HIDDEN");
}
});
// ActionListener on the JButton
addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("isShowingPopup: " + isShowingPopup);
if (isShowingPopup) {
menu.requestFocus();
isShowingPopup = false;
} else {
Component c = (Component) e.getSource();
menu.show(c, -1, c.getHeight());
isShowingPopup = true;
}
}
});
// Skip when navigating with TAB.
setFocusable(true); // Was false first and should be false in the end.
menu.setFocusable(true);
}
}
Here's a variant of Amber Shah's "big hack" suggestion I just made. Without the isShowingPopup flag...
It's not bulletproof, but it works quite well until someone comes in with an incredibly slow click to close the popup (or a very fast second click to reopen it...).
public class Button extends JButton {
// Icon.
private static final ImageIcon ARROW_SOUTH = new ImageIcon("ArrowSouth.png");
// Popup menu.
private final JPopupMenu menu;
// Last time the popup closed.
private long timeLastShown = 0;
public Button(int height) {
super(ARROW_SOUTH);
menu = new JPopupMenu(); // Populated somewhere else.
// Show and hide popup on left click.
menu.addPopupMenuListener(new PopupMenuListener() {
#Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent arg0) {
timeLastShown = System.currentTimeMillis();
}
#Override public void popupMenuWillBecomeVisible(PopupMenuEvent arg0) {}
#Override public void popupMenuCanceled(PopupMenuEvent arg0) {}
});
addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if ((System.currentTimeMillis() - timeLastShown) > 300) {
Component c = (Component) e.getSource();
menu.show(c, -1, c.getHeight());
}
}
});
// Skip when navigating with TAB.
setFocusable(false);
}
}
As I said in comments, that's not the most elegant solution, but it's horribly simple and it works in 98% of the cases.
Open to suggestions!
Here is another approach which is not too bad of a hack, if not elegant, and which, as far as I could tell, works. First, at the very top, I added a second boolean called showPopup.
The FocusListener has to be as follows:
menu.addFocusListener(new FocusListener() {
#Override
public void focusLost(FocusEvent e) {
System.out.println("LOST FOCUS");
isShowingPopup = false;
}
#Override
public void focusGained(FocusEvent e) {
System.out.println("GAINED FOCUS");
isShowingPopup = true;
}
});
The isShowingPopup boolean does not get changed anywhere else--if it gains focus, it assumes it's shown and if it loses focus, it assumes it isn't.
Next, the ActionListener on the button is different:
addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("isShowingPopup: " + isShowingPopup);
if (showPopup) {
Component c = (Component) e.getSource();
menu.show(c, -1, c.getHeight());
menu.requestFocus();
} else {
showPopup = true;
}
}
});
Now comes the really new bit. It's a MouseListener on the button:
addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
System.out.println("ispopup?: " + isShowingPopup);
if (isShowingPopup) {
showPopup = false;
}
}
#Override
public void mouseReleased(MouseEvent e) {
showPopup = true;
}
});
Basically, mousePressed gets called before the menu loses focus, so isShowingPopup reflects whether the popup was shown before the button is pressed. Then, if the menu was there, we just set showPopup to false, so that the actionPerformed method does not show the menu once it gets called (after the mouse is let go).
This behaved as expected in every case but one: if the menu was showing and the user pressed the mouse on the button but released it outside of it, actionPerformed was never called. This meant that showPopup remained false and the menu was not shown the next time the button was pressed. To fix this, the mouseReleased method resets showPopup. The mouseReleased method gets called after actionPerformed, as far as I can tell.
I played around with the resulting button for a bit, doing all the things I could think of to the button, and it worked as expected. However, I am not 100% sure that the events will always happen in the same order.
Ultimately, I think this is, at least, worth trying.
You could use the JPopupMenu.isVisible() instead of your Boolean variable to check the current state of the popup menu.
Have you tried adding a ComponentListener to the JPopupMenu, so that you know when it's been shown and hidden (and update your isShowingPopup flag accordingly)? I'm not sure listening for focus changes is necessarily the right approach.
What you need is a PopupMenuListener:
menu.addPopupMenuListener(new PopupMenuListener() {
#Override
public void popupMenuWillBecomeVisible(PopupMenuEvent arg0) {
}
#Override
public void popupMenuWillBecomeInvisible(PopupMenuEvent arg0) {
System.out.println("MENU INVIS");
isShowingPopup = false;
}
#Override
public void popupMenuCanceled(PopupMenuEvent arg0) {
System.out.println("MENU CANCELLED");
isShowingPopup = false;
}
});
I inserted this into your code and verified that it works.
Well, I can't be sure without seeing all of your code, but is it possible that the popup never actually gets focus at all? I've had problems with things' not getting focus properly in Swing before, so it could be the culprit. Try calling setFocusable(true) on the menu and then calling requestFocus() when you make the menu appear.
I tried the Answer of Tikhon Jelvis (introducing a smart combination of focusListener and mouseListener). It does not work for me on Linux (Java7/gtk). :-(
Reading http://docs.oracle.com/javase/7/docs/api/javax/swing/JComponent.html#requestFocus%28%29 there is written "Note that the use of this method is discouraged because its behavior is platform dependent."
It may be that the order of listener calls changed with Java7 or it changes with GTK vs Windows. I would not recommend this solution if you want to be platform independent.
BTW: I created a new account on stackoverflow to give this hint. It seems I am not allowed to comment to his answer (because of reputation). But it seems I have a button to edit it. This stackoverflow is a very funny thing. :-)