I set a dismissdelay '0' for a button tooltip. In windows I can't see the tooltip but In linux I am able to see the tooltip. In the java source code of javax.swing.ToolTipManager I did not find any platform dependent info.
Anyone have any idea?
public class TooltipInSwing extends JFrame {
public TooltipInSwing() {
super("TooltipInSwing");
setSize(400, 300);
getContentPane().setLayout(new FlowLayout());
JButton b1 = new JButton("Simple tooltip 1");
b1.setToolTipText("simple tool tip without a dismiss delay" + ToolTipManager.sharedInstance().getInitialDelay());
// set a new dismiss delay to a really big value, default is 4 sec.
ToolTipManager.sharedInstance().setDismissDelay(0);
getContentPane().add(b1);
WindowListener wndCloser = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
addWindowListener(wndCloser);
setVisible(true);
}
public static void main(String args[]) {
new TooltipInSwing();
}
}
Related
I have two JFrame based windows: SeatLayout and BillSummary. I need to get the seatnumber from the SeatLayout frame and display it in BillSummary but the variable scope is limited to the first frame.
How can I do this?
Using multiple JFrame is a bad practice and it should be avoided.
Reason being, it will add more problems in the future and it will be nightmare to maintain.
To answer your question , how to pass variable from your parent(JFrame) to a child(JDialog) .This can achive by using JDialog.
I am going to run through an example.
lets say, your BillSummary.java is ....
//BillSummary Class
public class billSummary {
JFrame frame;
billSummary(JFrame frame) {
this.frame = frame;
}
public void launchbillSummary(int seatNumber) {
// Create a dialog that suits your ui , you can use JPanel as your layout container
JDialog dialog = new JDialog(frame, "Bill Summary", true);
dialog.setLayout(new BorderLayout());
dialog.setSize(100, 100);
dialog.add(new JLabel(Integer.toString(seatNumber)), BorderLayout.CENTER);
dialog.setVisible(true);
}
}
Your seatLayout.java
public class seatLayout {
seatLayout(){
//Lets say you have seleted seat number 10
int defaultSeatNumber = 10;
//Lets say you have a button and when it is clicked , you pass the data to billsummary page
JButton enter = new JButton("Enter");
//Your seatLayout GUI
JFrame frame = new JFrame("seat layout");
frame.setSize(300,300);
frame.add(enter);
enter.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent arg0) {
//Do your passing of data/ price of calculation here
//You pass the data that to your custom dialog -> Bill summary
new billSummary(frame).launchbillSummary(defaultSeatNumber);
}
});
frame.setVisible(true);
}
public static void main(String[] args){
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new seatLayout();
}
});
}
}
I hope this help and answers your question. Good luck :)
I am trying to create an object when the user presses the button.So far, I've come up with the implementation bellow, but it does not seem to work.I haven't been dealing with Swing and Java UI at all so I am guessing it might be an amateur mistake.
The object I am trying to create is from another type called DebitCard.
private JFrame frame;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
GenerateCard window = new GenerateCard();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public GenerateCard() {
}
{
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JButton btnNewButton = new JButton("Generate card");
btnNewButton.setBounds(112, 213, 216, 41);
frame.getContentPane().add(btnNewButton);
}
private class buttonEvent implements ActionListener {
public void actionPerformed(ActionEvent e) {
String command = e.getActionCommand();
if (command.equals("Generate card")) {
DebitCard a = new DebitCard();
}
}
}
Based on your available code, you seem to have forgotten to register buttonEvent with btnNewButton
btnNewButton.addActionListener(new buttonEvent());
You might want to take a closer look at:
How to Use Buttons, Check Boxes, and Radio Buttons
How to Write an Action Listener
Laying Out Components Within a Container
Code Conventions for the Java TM Programming Language (this will make your code easier to read and it easier for you to read others)
In my program, I have this MAIN window and a HELP window. The HELP window (when opened) is to always stay on top whether it's in focus or not. The issue however is, when I try to requestFocusInWindow() for a component in the MAIN window through an action listener that gets fired from the HELP window, it just won't let me do it.
What is the proper way of accomplishing this?
TY :)
Edit:
As requested, here's a short example of what I'm trying to accomplish. Essentially I need the button inside the HELP window to trigger focus to the TextField inside the Main window.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Main {
public static void initGUI() {
mainFrame = new JFrame("Main");
helpFrame = new JFrame("Help");
mainFrame.setPreferredSize(new Dimension(500, 200));
helpFrame.setPreferredSize(new Dimension(500, 200));
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
helpFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setLayout(new FlowLayout());
helpFrame.setLayout(new FlowLayout());
mainTextView = new JTextField("", 20);
mainButton = new JButton("Open Help");
helpButton = new JButton("Request Focus");
mainButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(mainButton)) {
helpFrame.pack();
helpFrame.setVisible(true);
}
}
});
helpButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(helpButton))
System.out.println("Focus requested:" + mainTextView.requestFocusInWindow());
}
});
helpFrame.add(helpButton);
mainFrame.add(mainTextView);
mainFrame.add(mainButton);
mainFrame.pack();
mainFrame.setVisible(true);
}
public static void main(String[] args) {
initGUI();
}
static JFrame mainFrame, helpFrame;
static JTextField mainTextView;
static JButton mainButton, helpButton;
}
So turns out the fix was rather trivial. If requestFocus() is used instead of requestFocusInWindow(), it seems to work just fine.
Kinda feel stupid for how much time I spent on this :P
helpButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(helpButton))
mainTextView.requestFocus();
}
});
Can someone tell me how to set my X button in main application window visible to false and how to set Alt + F4 function not available or just disable it?
Update
I added it in:
public ZalumView(SingleFrameApplication app) {
super(app);
initComponents();
mainFrame = this.getFrame();
mainFrame.setTitle("Zalum - zarzadzanie zasobami ludzkimi");
mainFrame.pack();
mainFrame.setResizable(false);
mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
"A frame may have its native decorations (i.e. Frame and Titlebar) turned off with setUndecorated."—Frame
Addendum: You can send a WINDOW_CLOSING event and bind that Action to your desired Keystroke, as shown here.
For setting X button to invisible is very much described by #trashgod and For disabling your ALT + F4 thing, you can simply write frameObject.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
or you can addWindowListener(...) to your JFrame like this :
Code snippet to show what i am saying :
import java.awt.event.*;
import javax.swing.*;
public class FrameTest
{
private WindowAdapter windowAction;
private JFrame frame;
public FrameTest()
{
frame = new JFrame("FRAME TEST");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationByPlatform(true);
windowAction = new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
}
};
frame.addWindowListener(windowAction);
frame.setSize(100, 100);
frame.setVisible(true);
}
public static void main(String... args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new FrameTest();
}
});
}
}
I made an application with some widgets and at command line I want change their look and feel:
java -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel LookAndFeelAppl
but after invoked that command only a widget into the class constructor change its L&F but other that I created into a separate methods don't!!! Also tha JFrame itself not change.
public class LookAndFeelAppl extends JFrame
{
private JLabel label;
private JButton button;
public LookAndFeelAppl()
{
super("Look And Feel Demo");
setLayout(null);
final UIManager.LookAndFeelInfo plaf[] = UIManager.getInstalledLookAndFeels();
JLabel lable_laf = new JLabel("Choose L&F:");
final JComboBox cb = new JComboBox();
createOtherGUI();
cb.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
int ix = cb.getSelectedIndex();
try
{
UIManager.setLookAndFeel(plaf[ix].getClassName());
SwingUtilities.updateComponentTreeUI(LookAndFeelAppl.this);
}
catch (Exception ex)
{
}
}
});
// HERE IS THE PROBLEM!!!! THIS LOOP GOES BEFORE
// THE ITEMLISTENER BECAUSE WHEN I ADD ITEMS AN ITEMEVENT
// IS RAISED THAT SET AGAIN THE L&F WITH setLookAndFeel!!!
for (int i = 0, n = plaf.length; i < n; i++)
{
cb.addItem(plaf[i].getName());
}
//--------------------------------------------------------
add(lable_laf);
add(cb);
lable_laf.setBounds(10, 10, 150, 25);
cb.setBounds(10, 35, 150, 25);
}
public void createOtherGUI()
{
button = new JButton("BUTTON!!!!");
add(button);
button.setBounds(300, 45, 150, 35);
}
public static void main(String args[])
{
LookAndFeelAppl window = new LookAndFeelAppl();
window.setSize(1000, 700);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setVisible(true);
window.setLocationRelativeTo(null);
}
}
I ran this in Eclipse (latest version) using JDK 1.6.0_13 on windows. In the run configuration (VM arguments) of Eclipse I used -Dswing.defaultlaf=com.sun.java.swing.plaf.windows.WindowsLookAndFeel
public class Test extends JFrame {
public Test() {
super("Test L&F");
JComboBox box = new JComboBox(new String[] {"One", "Two", "Three"});
getContentPane().add(box, BorderLayout.SOUTH);
createControls();
setSize(300, 300);
setVisible(true);
}
private void createControls() {
JComboBox box = new JComboBox(new String[] {"One", "Two", "Three"});
getContentPane().add(box, BorderLayout.NORTH);
}
public static void main(String[] args) {
new Test();
}
}
Sorry, formatting is weird but you get the idea...
Few things to try and questions:
I see you are setting the L&F for windows, so I'm assuming you are running this on a windows box? The reason I ask is I'm not sure what the JDK is supporting as far as cross-platform L&Fs, just something to think about.
Did you try other L&Fs with the same result? (e.g. Nimbus)
This may or may not do anything but where do you have main(...)? Try putting it in the JFrame and in a separate class that constructs the JFrame.
Worst comes to worse you could always pass the L&F classname in as an arg to main(...) (or a props file) then use the UIManager to set it BEFORE any swing components are created.
Good luck,
Dave