We have built a Point of Sale system and now we require to implement it to Touch screens? Do we need to change any code in turn to allow this to work.
And we are using the Keyboard to enter values - let's say quantity - Is there a java way of popping up a key board (like android) when I focus on a JTextField?
Here is a simple example on how to implement a pop-up keyboard:
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
#SuppressWarnings("serial")
public class MainFrame extends JFrame
{
private JTextField txt;
private PopUpKeyboard keyboard;
public MainFrame()
{
super("pop-up keyboard");
setDefaultCloseOperation(EXIT_ON_CLOSE);
txt = new JTextField(20);
keyboard = new PopUpKeyboard(txt);
txt.addMouseListener(new MouseAdapter()
{
#Override
public void mouseClicked(MouseEvent e)
{
Point p = txt.getLocationOnScreen();
p.y += 30;
keyboard.setLocation(p);
keyboard.setVisible(true);
}
});
setLayout(new FlowLayout());
add(txt);
pack();
setLocationByPlatform(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
new MainFrame().setVisible(true);
}
});
}
private class PopUpKeyboard extends JDialog implements ActionListener
{
private JTextField txt;
public PopUpKeyboard(JTextField txt)
{
this.txt = txt;
setLayout(new GridLayout(3, 3));
for(int i = 1; i <= 9; i++) createButton(Integer.toString(i));
pack();
}
private void createButton(String label)
{
JButton btn = new JButton(label);
btn.addActionListener(this);
btn.setFocusPainted(false);
btn.setPreferredSize(new Dimension(100, 100));
Font font = btn.getFont();
float size = font.getSize() + 15.0f;
btn.setFont(font.deriveFont(size));
add(btn);
}
#Override
public void actionPerformed(ActionEvent e)
{
String actionCommand = e.getActionCommand();
txt.setText(txt.getText() + actionCommand);
}
}
}
If you don't need multi-touch, the normal mouse drivers for use with most touch screen controllers will just have the touch-screen emulate a normal mouse where a finger touching the screen is emulated as a mouse click.
As for a virtual keyboard, there are crummy ones built into Windows and MacOSX but it would probably be best to build one into the application if you can.
If you need multi touch or have issues with specific touch screen controllers, there are a few options.
Your best bet in swing, at least on windows, seems to be this project: http://www.michaelmcguffin.com/code/JWinPointer/
JavaFX appears to have touch support, Intel has a tutorial: https://software.intel.com/en-us/articles/using-javafx-to-implement-multi-touch-with-java-on-windows-8-desktop. You might be able to get this working with swing somehow as there are methods to host Swing in JavaFX and JavaFX in Swing, you might look for other answers to accomplish interop between both.
There was project MT4J, but it seems to be defunct. It doesn't seem to work with Swing or JavaFX.
You should be able to provide your own virtual keyboard through the use of something like a JWindow and the KeyboardFocusManager
We implemented a custom Look-and-feel for our Swing application with touch support to make everything just look bigger (all buttons, checkboxes, ..., even JTree instances) so that it is easy to modify them using a finger.
Such a solution might save you the work to convert all your UI's to a touch-friendly UI.
You should change your user experience and interaction design, thus change the code. Check out Windows UX guidelines for touch - https://msdn.microsoft.com/en-us/library/windows/desktop/dn742468.aspx
Related
Hi !
I'd like to know how i can change the default focused background and foreground for a JComboBox.
Since i made it extends a DefaultListCellRenderer, which is modifying both foreground and background for each of its items, i don't like that it doesn't respect them when it gains focus.
I've tried UIMnager.put("ComboBox.background", new ColorUIResource(my_color)); and some other keys (every single one i could find, actually) as first thing SwingUtilities does, without any success.
I didn't make any change in UIManager inside this program, neither before nor after, so the look and feel is the default one (Metal), as it's provide at first by Java 13.
Is there a way to do so ?
Edit:
here is a sample to see what i mean :
package tests;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridLayout;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
final class ComboBoxTest implements Runnable {
public static void main(String[]arguments_) {
SwingUtilities.invokeLater(new ComboBoxTest());
}
#Override
public void run() {
JFrame frame_ = new JFrame("JComboBox Test");
JPanel panel_ = new JPanel(new GridLayout(3, 1));
JComboBox<String>
useless_box = new JComboBox<>()
, tested_box = new JComboBox<>(new String[] {
"<html>⛈</html>", "<html>☠</html>"
});
tested_box.setBackground(Color.ORANGE);
tested_box.setForeground(Color.RED.darker().darker());
tested_box.setFont(tested_box.getFont().deriveFont(48.f));
tested_box.setRenderer(new ComboRenderer());
panel_.add(new JPanel());
panel_.add(useless_box);
panel_.add(tested_box);
frame_.add(panel_);
frame_.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame_.setLocationRelativeTo(null);
frame_.pack();
frame_.setVisible(true);
}
private class ComboRenderer extends DefaultListCellRenderer {
/**
*
*/
private static final long serialVersionUID = -1329981614961331328L;
#Override
public Component getListCellRendererComponent (
JList<? extends Object>list_, Object value_
, int index_, boolean is_selected, boolean cell_has_focus
) {
super.getListCellRendererComponent (
list_, value_, index_, is_selected, cell_has_focus
);
setHorizontalAlignment(DefaultListCellRenderer.CENTER);
switch(index_) {
case -1:
setBackground(Color.ORANGE);
break;
default:
if(is_selected == true) {
setBackground(Color.ORANGE.darker());
} else {
setBackground(Color.ORANGE);
}
break;
}
setForeground(Color.RED.darker().darker());
return this;
}
}
}
The first box (at the middle) and the JPanel (at the top) are here to see what happens to the second one when it gains the focus or loses it, transfering the focus or not completely (by clicking on the JPanel instead of the JComboBox itself or an other selectable JComponent).
i don't like that it doesn't respect them when it gains focus.
If you are saying you don't like the blue selection background color when the combo box gains focus then you can use:
UIManager.put("ComboBox.selectionBackground", new ColorUIResource(Color.ORANGE));
This may work on some LAF's. But it will apply for all combo boxes in the application.
If you need it only for a single combo box then you would need to write a custom ComboBoxUI. I have no idea what methods you would need to override, but you can search the source code for "selectionBackground" to see how that property is used.
I've made a JFrame with Diferent JButtons and i'd like to get an image from another class. Any ideas? Or how draw on the same class but on the action performed?
Because it doesnt let me to do any drawings...my complier always gives me error messages
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.*;
public class red extends JFrame {
public JButton b;
public JButton b1;
public JButton b2;
public JButton b3;
public JButton b4;
public static Image p;
public static Graphics g;
public red() throws IOException {
gui1 x = new gui1();
setTitle(" ");
setSize(1200,700);
setLayout(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
b= new JButton("click");
b1= new JButton();
b.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent e0){
b1.setBounds(0, 0, 200, 200);
b.show(false);
add(x);
}
});
b.setBounds(0, 0, 100, 100);
add(b1);
add(b);
setVisible(true);
}
public static void main(String[] args) throws IOException {
red k = new red();
}
}
import java.awt.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;
public class gui1 extends Canvas {
public static Image p;
public void paint(Graphics g){
g.drawImage(p, 700, 200, 100, 100, this);
}
{
try {
p= ImageIO.read(new File("Lighthouse.jpg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Phew! I see A LOT of errors in your code (even after I corrected the compilation errors):
You're not following the Java naming conventions:
Class names should be nouns, in mixed case with the first letter of each internal word capitalized
while red is a noun it should be more descriptive and be capitalized. The same goes for gui1
You're extending JFrame which in plain english would say: red is a JFrame, you should really avoid this and create your GUI based on JPanels instead... see Java Swing using extends JFrame vs callint it inside of class
You're setting size (a REAAAAAAALLY big one window for the JButton sizes you're using), instead use pack()
You're using null-layout, while pixel-perfect GUIs might seem like the easiest way to create complex GUIs for Swing newbies, the more you use them the more problems related to this you'll find in the future, they are hard to maintain and cause random problems, they don't resize, etc. Please read Null layout is evil and Why is it frowned upon to use a null layout in Swing? for more information about why you should avoid its use and why you should change your GUI to work with Layout Managers along with Empty Borders for extra spacing between components.
You're making use of a deprecated method JFrame#show() you should be using JFrame#setVisible(...) instead.
Related to point #4, you shouldn't be calling setBounds(...) method, but let that calculations to the layout managers.
You're not placing your program on the Event Dispatch Thread (EDT), Swing is not thread safe, you can fix this by changing your main() method as follows:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
//Your constructor here
}
});
}
You're mixing AWT and Swing components, instead of using AWT's Canvas use Swing's JPanel which has more functionality and support.
Images will become embedded resources once they're packaged in a JAR file, so it's wise to start treating them as if they already were, not as external files as shown in the embedded-resource tag.
Once you change from Canvas to JPanel you should override its paintComponent(...) method and not paint(...) and call it's super.paintComponent(g) method as the first line, also don't forget to add the #Overrides annotation. See the tutorial on Swing custom painting.
You're abusing the use of static keyword, see how does the static keyword works?
After seeing all the above errors I recommend you to go back and Learn the basics of the language before starting with a graphical environment which will only add more difficulty to your learning.
From what I understand you want to draw an image on a button click, if that's the case then you can wrap your image in a JLabel and add that JLabel to a JPanel which then is added to a parent JPanel which is later added to the JFrame:
As you can see in the GIF above, the icon is displayed after user presses the button.
Obviously this can be improved for the GUI to be more "attractive" with combinations of layout managers and empty borders as stated before.
This was done with the following code:
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class ImageDrawingFromOneClassToAnother {
private JFrame frame;
private JPanel pane;
private JPanel leftPane;
private JPanel rightPane;
private ImageIcon icon;
private JButton button;
private JLabel label;
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new ImageDrawingFromOneClassToAnother().createAndShowGui();
}
});
}
public void createAndShowGui() {
frame = new JFrame(getClass().getSimpleName());
icon = new ImageIcon(this.getClass().getResource("king.png")); //Read images as if they were already embedded resources
button = new JButton("Draw image");
label = new JLabel(""); //Create an empty label
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
label.setIcon(icon); //On button click, we set the icon for the empty label
}
});
pane = new JPanel() {
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 200); //Set a size for the main panel
}
};
pane.setLayout(new GridLayout(1, 2)); //The main panel
leftPane = new JPanel(); //The button panel
leftPane.setLayout(new BoxLayout(leftPane, BoxLayout.PAGE_AXIS));
leftPane.add(button);
rightPane = new JPanel(); //The panel where the image will be drawn
rightPane.add(label);
//We add both (button and image) panels to the main panel
pane.add(leftPane);
pane.add(rightPane);
frame.add(pane); //Add the main panel to the frame
frame.pack(); //Calculate its preferred size
frame.setVisible(true); //Set it to be visible
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I am using JWindow in my project to display a UI that is undecorated and also doesn't appear in the task bar. But, the JWindow always seems to be on top of all other windows. I tried setting the setAlwaysOnTop to false, but it didn't seem to help.
Here's the code that can reproduce the problem :
package test;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JWindow;
public class Test extends JWindow implements ActionListener {
public Test() {
setSize(300, 300);
setLocationRelativeTo(null);
setAlwaysOnTop(false);
JButton myButton = new JButton("Click Here");
myButton.addActionListener(this);
getContentPane().add(myButton);
setVisible(true);
}
public static void main(String[] args) {
new Test();
}
#Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("Click Here"))
JOptionPane.showMessageDialog(this, "This dialog box appears behind the JWindow!");
}
}
My OS is Linux and I'm using the Oracle JDK 6. Also, while I was testing my app on Windows, I was using JDialog for the UI and it was working fine. But, in Linux JDialog seems to appear in the task bar.
Any help as to how to solve this?
After you set the visibility of the window to True, you send it to the back like this:
setVisible(true);
toBack();
If, later, you want to bring it to the top of the stacking order, you simply call:
toFront();
More details here:
http://docs.oracle.com/javase/6/docs/api/java/awt/Window.html#toBack()
http://docs.oracle.com/javase/6/docs/api/java/awt/Window.html#toFront()
I am trying to create a fullscreen window that cover the whole screen using Java. This window must also have some transparency (about 30%-50% transparent). When saying whole screen, I do mean it cover everything (including the dock/taskbar/menubar in OSX/Linux/Windows), and when I say with transparancy, I mean a real-time transparancy and not just a hacked screenshot. Here is what I am aware-of/tried:
Using Java Fullscreen API: while it creates a true fullscreen, you cannot have some transparency with it (only opaque color). One hack is to take a screenshot of the whole desktop and set it as background for the window, but this mean it is not real-time transparency.
Setting window size to match screen dimension: while it fills the whole screen, in certain OSes (e.g. Mac OS X) the window will be rendered behind the dock/menubar, and not above it. However, transparency do work here.
Using setWindowOpacity API: it work in the second case, but not in the first (Fullscreen API)
Using setBackground with alpha: it work like the setWindowOpacity, but only in certain OSes. But also doesn't work with Fullscreen API.
Use JFrame/JWindow/JDialog/Frame/Window: tried every window model I could, without any luck
So I am asking if this is possible through a another hack that I am not aware of, then I would be happy to hear about.
The goal is to overlay a semi-transparent fullscreen over the desktop.
is possible only with visible TaskBar e.i.
.
GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
otherwise you got and exception
.
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException:
The effects for full-screen windows are not supported.
or by using brutte_force to DirectX freezed my PC twicw, only power_off to save PC's GPU
import com.sun.awt.AWTUtilities;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class JFrameOpacityExample {
private JFrame myFrame = new JFrame("Test Frame");
private boolean opacity = true;
private boolean resize = true;
private JButton button = new JButton("Opacity");
private JButton button1 = new JButton("Resize");
public JFrameOpacityExample() {
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Object src = evt.getSource();
if (opacity) {
AWTUtilities.setWindowOpacity(myFrame, 0.50f);
opacity = false;
} else {
AWTUtilities.setWindowOpacity(myFrame, 1.0f);
opacity = true;
}
}
});
button1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Object src = evt.getSource();
if (resize) {
Rectangle dim = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
int h = dim.height;
int w = dim.width;
myFrame.setBounds(00, 00, w, h);
resize = false;
} else {
myFrame.setBounds(100, 100, 400, 400);
resize = true;
}
}
});
JPanel panel = new JPanel();
panel.add(button);
panel.add(button1);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myFrame.add(panel);
myFrame.setSize(400, 400);
myFrame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrameOpacityExample jFrameOpacityExample = new JFrameOpacityExample();
}
});
}
}
Hey guys I'm creating a game similar to farmville in java and I'm just wondering how would I implement the interactive objects/buttons that the user would usually click to interact with the game client.
I do not want to use the swing library (generic windows looky likey objects), I would like to import custom images for my buttons and assign button like properties to those images which would be used for the GUI.
Any advice? Any pointers? I can't seem to find that information through youtube or some other java gaming sites as they're only showing simple example using swing.
Any help would be deeply appreciated thanks!
Regards
Gareth
Do you really not want to use Swing, or do you just not want the default look and feel of a JButton and other swing controls? What does " (generic windows looky likey objects), " mean?
There are many sources out there that describe customizing buttons to include images on top of them:
Creating a custom button in Java
JButton and other controls have all the events and methods associated with adding click listeners, etc. You probably don't want to create your own control. We do not have enough information to go off of, for example what does "interactive objects" mean?
If you simply want to add an icon to a JButton, use the constructor that takes an Icon.
You can use JButton, just override the paint function. and draw what ever you want there. It takes a while until you get it at the first time how this works. I recommend you to read a little about the event-dispatching thread (here is java's explanation)
And here is some code that I wrote so you have a simple reference.
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class Test extends JButton implements ActionListener{
private static final long serialVersionUID = 1L;
Image img;
/** constuctor **/
public Test(String tImg, JFrame parent){
this.img = new ImageIcon(tImg).getImage();
this.addActionListener(this);
}
/*********** this is the function you want to learn ***********/
#Override
public void paint(Graphics g){
g.drawImage(this.img, 0, 0, null);
}
#Override
public void actionPerformed(ActionEvent arg0) {
// TODO do some stuff when its clicked
JOptionPane.showMessageDialog(null, "you clicked the button");
}
public static void main(String[] args) {
JFrame f = new JFrame();
Test t = new Test("pics.gif", f);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(1, 1));
f.add(t);
f.setSize(400,600);
f.setVisible(true);
}
}