JOptionPane.showConfirmDialog goes out of screen when the message is large - java

JOptionPane.showConfirmDialog(this,
message,
"title",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
The message can be 10 lines and message can be 500 lines. It changes dynamically. I want to implement a scroll bar if the message exceeds the screen height.
So I tried:
JTextArea textArea = new JTextArea (message);
JscrollPane scrollPane = new JScrollPane(textArea,JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
JOptionPane.showConfirmDialog(this,
scrollPane,
"title",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE);
This will open a dialog and a scroll bar in windows and it works fine, but in mac os the dialog goes out of screen.
Can any one help me?

You can set the preferred size for your scrollPane before showing the dialog to limit its size:
scrollPane.setPreferredSize(new Dimension(400, 200));
The dimension to use can be based on the screen size that you can retrieve like this:
Toolkit.getDefaultToolkit().getScreenSize()
Note that I have not tested this on other platforms than Mac OS X.

Related

How to show SystemTray Message when Frame is not iconified

Is there a way to display a TrayIcon Message
tray.displayMessage("Warning", "Array index out of range", TrayIcon.MessageType.INFO);
when a JFrame is not iconified?
MadProgrammer brought me the idea. I had the code
if(e.getNewState()==NORMAL){
tray.remove(trayIcon);
setVisible(true);
}
so the TrayIcon was removed when i tried to display a message.

How to put two components at the same place

I am currently building a game and I don't know how to make a main menu. I don't know how to put the text on the GIF background. Now the thing is when I try to put them on I try this code:
private void initializeDisplay() {
playBtn.setBorderPainted(false);
playBtn.setFocusPainted(false);
playBtn.setContentAreaFilled(false);
playBtn.setText("PLAY");
playBtn.setFont(new Font("Poiret One", Font.TRUETYPE_FONT, 27));
playBtn.setForeground(Color.WHITE);
mainPanel.add(playBtn);
mainPanel.add(bgImg);
mainPanel.setLayout(new GridBagLayout());
bgImg.setLocation(new Point(200, 200));
mainPanel.setOpaque(true);
mainPanel.addKeyListener(new Keyboard());
mainPanel.addMouseListener(new Mouse());
mainPanel.setFocusable(true);
}
Also when I do that I use a GridBagLayout for the layout type but it seems that I'm not get the result I want, the problem is the button not the image in the background because that's appearing but the button isn't appearing.
You could also add an image to a jlabel's icon field, and then add the label to your panel's content pane.
This post explains it more:
How to set a background picture in JPanel

JFace MessageDialog is not showing the body(dialog area) by default

I am creating a JFace MessageDialog
MessageDialog md = new MessageDialog(null, "Title", null,
"Body message", SWT.YES | SWT.NO, new String[] { Messages.yes, Messages.no }, 0);
On one Windows 7 machine when this dialog is opened, it doesn't show its body/dialog area until user presses the Enter key and only title bar is displayed without body. Moreover its position is left bottom corner of the screen. While on pressing the Enter key it opens the dialog in a proper manner and user can see the "Body message" and Yes No buttons on it.
Is it related to some windows or java setting on that machine?

Making a JDialog always on top of application

I'm making a program that requires this jdialog box to always be focused and on top, ie: make the ding sound when i click on the parent window. here's what i have so far:
JDialog dialog = new JDialog();
// dialog.setAlwaysOnTop(true);
dialog.setSize(400, 220);
dialog.setLocationRelativeTo(relativeTo); //relativeTo is the name of parent frame
dialog.setVisible(true);
// dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
// dialog.setModal(true);
(the commented things are those i have tried unsuccessfully...)
how would i make this dialog box ontop of the parent window? any help would be great! thanks
You need to set the dialog's owner and make the dialog modal:
JDialog dialog = new JDialog(parentFrame, true); // owner, modal

How to show scroll bar in the text area in eclipse

I am learning Java.I just created an application (liked desktop one in c#) by adding some labels,a text view and a button.
Its fun learning this new thing,but i soon encountered an issue ,when u tried to add vertical scrolling to the text view i added on the UI.
I also tried adding vertical scroll to the text area but still my text area is not displaying the scroll bar.
The part of the code created when i added the controls from the panel to the UI is as below:
thisLayout.setVerticalGroup(thisLayout.createSequentialGroup()
.addContainerGap(17, 17)
.addComponent(getJtxtArea(), GroupLayout.PREFERRED_SIZE, 158, GroupLayout.PREFERRED_SIZE)
The code for the function getJtxtArea() is as below:
private JTextArea getJtxtArea() {
if(jtxtArea == null) {
jtxtArea = new JTextArea();
jtxtArea.setBackground(new java.awt.Color(255,255,255));
jtxtArea.setFont(new java.awt.Font("Segoe UI",3,14));
jtxtArea.setWrapStyleWord(true);
jtxtArea.setLineWrap(true);
JScrollPane scroll = new JScrollPane(jtxtArea);
scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
}
return jtxtArea;
}
Can anyone tell me why i am not getting the scroll bar on the text view.Thanks in advance.
Note:I am using Eclipse Helios as the IDE and using Jigloo plugin in eclipse for the GUI.
Add the component scroll instead of the jtxtArea. In addition to that, you might also want to resize your JScrollPane.

Categories

Resources