When I run my application in Linux with Metal look and feel, the user can't be identitied whether the button is pressed or not. it look like button is not pressed though the buttton is in press mode. also the right hand side close button icon is not same as windows.
how can i resolve this two issues?
EDIT
I did following way.
try {
m_FrameBackground = new Color(Integer.parseInt((String)
Client.getClient().getProperty("BackgroundColor"),16));
m_TaskBackground = new Color(Integer.parseInt((String)
Client.getClient().getProperty("TaskBackground"),16));
} catch (Exception e) {
m_FrameBackground = new Color(Integer.parseInt("dfd3be",16));
m_TaskBackground = new Color(Integer.parseInt("dfd3be",16));
}
m_FuelTheme.secondary3 = new ColorUIResource(m_FrameBackground);
m_FuelTheme.secondary2 = new ColorUIResource(m_FrameBackground.darker());
m_FuelTheme.secondary1 = new ColorUIResource(m_FrameBackground.darker().darker());
MetalLookAndFeel.setCurrentTheme(m_FuelTheme);
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
JDialog.setDefaultLookAndFeelDecorated(true);
Related
I've been working on a background java program for a while and its almost ready to be released so I thought I should probably add a way to exit the program. I have this function:
private static void setupSysTray() {
if (!SystemTray.isSupported()) {
System.out.println("SystemTray is not supported");
return;
}
try {
final PopupMenu popupMenu = new PopupMenu();
final TrayIcon trayIcon = new TrayIcon(ImageIO.read(new File(workingDirectory +
fileSeparator + "tray.png")), "Multi");
final SystemTray tray = SystemTray.getSystemTray();
MenuItem exitItem = new MenuItem("Exit");
exitItem.addActionListener(e -> {
System.out.println("Something happened!");
System.exit(0);
});
popupMenu.add(exitItem);
trayIcon.setPopupMenu(popupMenu);
tray.add(trayIcon);
} catch (IOException | AWTException e) {
e.printStackTrace();
}
}
I presumed this would handle it, I pieced it together from various stack overflow posts and the offical documentation. The result is that the tray icon appears, with the correct image and tooltip. When I right click it I see the menu item for "exit" show up. But that's where it breaks, the menu item doesn't have any hover coloring (leading me to believe input at all with it is broken) and clicking on the item turns up no results. Have I made some silly mistake like ordering the adding of items wrong? What's going on here?
As it turns out I was making use of a Global Mouse Hook in an old part of the code that I had all but forgotten about. The hook is from this repository and the fix was changing the hook's raw input setting from true to false.
// True instead of false seems to block JavaFX events
private static GlobalMouseHook mouseHook = new GlobalMouseHook(false);
In my program, I'm using javahelp with helpbroker: all the swing components of the window are automatically generated and positioned.
I would like custom the helpbroker to add a button bar at the bottom of the window like in the image.
What is the easiest way to do it ?
Thanks
The only way to add a help button is to Embed the javahelp in a JFrame:
public class vmHelp {
public static void main(String args[]) {
JHelp helpViewer = null;
String title = "";
try {
// Get the classloader of this class.
ClassLoader cl = vmHelp.class.getClassLoader();
// Use the findHelpSet method of HelpSet to create a URL referencing the helpset file.
// Note that in this example the location of the helpset is implied as being in the same
// directory as the program by specifying "jhelpset.hs" without any directory prefix,
// this should be adjusted to suit the implementation.
String lHelpSetFile = "APP.hs";
URL url = HelpSet.findHelpSet(cl, lHelpSetFile);
if (url == null) {
System.err.println("URL is null, maybe the help set file is wrong: " + lHelpSetFile + ". Look at vmHelp.java");
return;
}
// Create a new JHelp object with a new HelpSet.
HelpSet h = new HelpSet(cl, url);
title = h.getTitle();
helpViewer = new JHelp(h);
// Set the initial entry point in the table of contents.
helpViewer.setCurrentID("top");
} catch (Exception e) {
System.err.println(e.getMessage());
}
// Create a new frame.
JFrame frame = new JFrame();
// Set it's size.
frame.setSize(1000, 800);
// Add the created helpViewer to it.
frame.getContentPane().add(helpViewer);
// Set a default close operation.
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setTitle(title);
// Make the frame visible.
frame.setVisible(true);
}
}
After we can customize the JFrame as we want, it is easy to add a south panel with a close button.
To make all the help buttons listen our javahelp:
pButton.addActionListener(helpAction(pHelpId));
with helpAction that displays our JFrame
Think also to handle keyboard shortcuts, like the helpbroker:
pComponent.registerKeyboardAction(helpAction(pHelpId), KeyStroke.getKeyStroke(KeyEvent.VK_HELP, 0),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
pComponent.registerKeyboardAction(helpAction(pHelpId), KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
To open the help at the wanted section:
helpViewer.setCurrentID("top");
"top" corresponds to tag in the .jhm file
I have jbutton1 which has a text "Connect" when the user clicks the jbutton1 the application runs and the text of jbutton1 changes from "Connect" to "Disconnect". Now again when the user clicks jbutton1 and the text is "Disconnect" so the application should close.
The code i have given here does not checks anything it automatically closes the application which i don't want. I want to check the text of the button if it is connect i should do a different process and if it is disconnect it shouls close my application when the button is clicked.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if(jComboBox2.getSelectedItem()==null)
{
System.out.println("Select one name");
}
else
{
try {
Runtime r = Runtime.getRuntime();
p = Runtime.getRuntime().exec("C:\\Program Files");
AS a4sObj = new AS(new String[]{jComboBox2.getSelectedItem().toString()});
} catch (IOException ex) {
Logger.getLogger(serialportselection.class.getName()).log(Level.SEVERE, null, ex);
}
jButton1.setText("Disconnect"); //This sets connect button to disconnect
if(jButton1.getText()== "Disconnect") // I wanted to change this line of code
{
System.exit(0);
}
Just move setText to the end:
if(jButton1.getText()== "Disconnect") // I wanted to change this line of code
{
System.exit(0);
}
jButton1.setText("Disconnect"); //This sets connect button to disconnect
This is wrong
if(jButton1.getText()== "Disconnect") , take a look at this great answer to check the reason and learn how to compare Strings in java
You should do:
if("Disconnect".equalsIgnoreCase(jButton1.getText()))
instead
I coded a j2me application using LWUIT. It works fine on emulator as well as a symbian device. But when i tried to run it on a nokia s40 device,it showed up a "nothing to display" message. I tried displaying a splash screen, as prescribed in some forums. Still,the app never gets past the splash screen.
EDIT 1
Display.init(this);
Resources r = Resources.open("/theme.res");
UIManager.getInstance().setThemeProps(r.getTheme(r.getThemeResourceNames()[0]));
Dialog splash = new Dialog("Splash Screen");
splash.setAutoDispose(true);
splash.setTimeout(5000);
splash.show();
RecordStore rs = null;
byte[] buffer = null;
rs = RecordStore.openRecordStore("xxxxxx", true);
if (rs.getNumRecords() > 0) {
buffer = rs.getRecord(rs.getNumRecords());
num = new String(buffer, 0, buffer.length);
rs.closeRecordStore();
offer(num); // a method which displays main form
} else {
rs.closeRecordStore();
registration("xxxxx"); //another method which displays the secondary form
}
In this snippet,a blank screen is displayed on the device after the dialog/splash screen.
The form gets displayed when i remove the codes managing the RecordStore.
How do i fix this mess ?
EDIT 2
Code for registration()
Form f = new Form();
f.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
Image img = Image.createImage("logo.png");
f.addComponent(new Label(img));
Label lbl = new Label(msg);
f.addComponent(lbl);
f.addComponent(new Label("xxxxx"));
final TextArea number = new TextArea(1, 10, TextArea.NUMERIC);
f.addComponent(number);
Button btn = new Button("Register");
btn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//perform rms related activities and move onto offer()
}
});
f.addComponent(btn);
Button help = new Button("Help?");
help.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
//display a help dialog
}
});
f.addComponent(help);
f.addCommandListener(this);
f.show();
Change the splash.show() to splash.showModeless()
Regardless your code is incorrect since it assumes show() will display the dialog immediately which is not how most GUI frameworks work. Your method needs to complete and return control to LWUIT in order for the dialog to show. However, you read the RMS and then the code to show your form is unclear, when do you expect it to actually occur.
You need to show the dialog without a timeout (I would use a form for the splash screen there is no reason to use a dialog), then open a thread (new Thread(...)) to do whatever you want and then when the thread completes show your form.
From this blog, The Nothing to display issue is standard Nokia S40 behavior for delayed calls to setCurrent() and the normal recommendation is to show a splash screen early on to avoid this prompt.
Also look this same related discussion.
Edit:
Form splashscreen = new Form();
splashscreen.getStyle().setBgImage(imageName);
splashscreen.show()
Display.getInstance().callSerially(new Runnable() {
public void run() {
try {
Thread.sleep(5000L);
// do RMS related things here.
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
});
is there any way to save an image from a java applet
Been asked before:
Java applet - saving an image in a png format
or
Save an Image to a File in a Applet?
Please first search your question or at least check out the related questions as you type your question in. It's very helpful for finding other users with common issues.
Just try this notepad example which helps u to load images and save it also,
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Scanner;
import java.io.*;
public class Notepad extends JFrame implements ActionListener {
private TextArea textArea = new TextArea("", 0,0, TextArea.SCROLLBARS_VERTICAL_ONLY);
private MenuBar menuBar = new MenuBar(); // first, create a MenuBar item
private Menu file = new Menu(); // our File menu
// what's going in File? let's see...
private MenuItem openFile = new MenuItem(); // an open option
private MenuItem saveFile = new MenuItem(); // a save option
private MenuItem close = new MenuItem(); // and a close option!
public Notepad() {
this.setSize(500, 300); // set the initial size of the window
this.setTitle("Java Notepad Tutorial"); // set the title of the window
setDefaultCloseOperation(EXIT_ON_CLOSE); // set the default close operation (exit when it gets closed)
this.textArea.setFont(new Font("Century Gothic", Font.BOLD, 12)); // set a default font for the TextArea
// this is why we didn't have to worry about the size of the TextArea!
this.getContentPane().setLayout(new BorderLayout()); // the BorderLayout bit makes it fill it automatically
this.getContentPane().add(textArea);
// add our menu bar into the GUI
this.setMenuBar(this.menuBar);
this.menuBar.add(this.file); // we'll configure this later
// first off, the design of the menuBar itself. Pretty simple, all we need to do
// is add a couple of menus, which will be populated later on
this.file.setLabel("File");
// now it's time to work with the menu. I'm only going to add a basic File menu
// but you could add more!
// now we can start working on the content of the menu~ this
gets a little repetitive,
// so please bare with me!
// time for the repetitive stuff. let's add the "Open" option
this.openFile.setLabel("Open"); // set the label of the menu item
this.openFile.addActionListener(this);
// add an action listener (so we know when it's been clicked
this.openFile.setShortcut(new MenuShortcut(KeyEvent.VK_O, false));
// set a keyboard shortcut
this.file.add(this.openFile); // add it to the "File" menu
// and the save...
this.saveFile.setLabel("Save");
this.saveFile.addActionListener(this);
this.saveFile.setShortcut(new MenuShortcut(KeyEvent.VK_S, false));
this.file.add(this.saveFile);
// and finally, the close option
this.close.setLabel("Close");
// along with our "CTRL+F4" shortcut to close the window, we also have
// the default closer, as stated at the beginning of this tutorial.
// this means that we actually have TWO shortcuts to close:
// 1) the default close operation (example, Alt+F4 on Windows)
// 2) CTRL+F4, which we are about to define now:
(this one will appear in the label)
this.close.setShortcut(new MenuShortcut(KeyEvent.VK_F4, false));
this.close.addActionListener(this);
this.file.add(this.close);
}
public void actionPerformed (ActionEvent e) {
// if the source of the event was our "close" option
if (e.getSource() == this.close)
this.dispose(); // dispose all resources and close the application
// if the source was the "open" option
else if (e.getSource() == this.openFile) {
JFileChooser open = new JFileChooser();
// open up a file chooser (a dialog for the user to browse files to open)
int option = open.showOpenDialog(this);
// get the option that the user selected (approve or cancel)
// NOTE: because we are OPENing a file, we call showOpenDialog~
// if the user clicked OK, we have "APPROVE_OPTION"
// so we want to open the file
if (option == JFileChooser.APPROVE_OPTION) {
this.textArea.setText("");
// clear the TextArea before applying the file contents
try {
// create a scanner to read the file (getSelectedFile().getPath() will get the path to the file)
Scanner scan = new Scanner(new FileReader(open.getSelectedFile().getPath()));
while (scan.hasNext()) // while there's still something to read
this.textArea.append(scan.nextLine() + "\n"); // append the line to the TextArea
} catch (Exception ex) { // catch any exceptions, and...
// ...write to the debug console
System.out.println(ex.getMessage());
}
}
}
// and lastly, if the source of the event was the "save" option
else if (e.getSource() == this.saveFile) {
JFileChooser save = new JFileChooser(); // again, open a file chooser
int option = save.showSaveDialog(this); // similar to the open file, only this time we call
// showSaveDialog instead of showOpenDialog
// if the user clicked OK (and not cancel)
if (option == JFileChooser.APPROVE_OPTION) {
try {
// create a buffered writer to write to a file
BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
out.write(this.textArea.getText()); // write the contents of the TextArea to the file
out.close(); // close the file stream
} catch (Exception ex) { // again, catch any exceptions and...
// ...write to the debug console
System.out.println(ex.getMessage());
}
}
}
}
// the main method, for actually creating our notepad and setting it to visible.
public static void main(String args[]) {
Notepad app = new Notepad();
app.setVisible(true);
}
}