I'm trying to position a JDialog relative to a JButton in an internal JPanel- which I can do using dialog.setLocationRelativeTo(button), but the dialog then covers the button. I've been trying, quite unsuccessfully, to then move the dialog slightly out of the way.
I've found a few different approaches that seemed promising, to then only produce exactly the same result, from just using setLocation(x,y) after setting the relative location (which seem to override each other), to getting the location of the button from the screen.
I'm warey of being spoonfed this sort of stuff, and having asked a few questions over the last few days, but does anyone have any hints at all, like where I should be looking in the API? Should I be looking at converting the coordinates relative to the component to a screen coordinate? That's my next best guess...but I'm not going to lie, it's definitely confusing me.
You don't need to convert yourself, thanks to Component.getLocationOnScreen():
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
final JFrame f = new JFrame("test");
final JButton b = new JButton("Hello");
f.getContentPane().setLayout(new BorderLayout());
f.getContentPane().add(b, BorderLayout.NORTH);
f.setSize(300, 200);
f.setVisible(true);
b.addActionListener((e) -> {
JDialog dialog = new JDialog(f);
dialog.getContentPane().add(new JLabel(new Date().toString()));
dialog.pack();
Point point = b.getLocationOnScreen();
//dialog.setLocationRelativeTo(b); // Shows over button, as doc says
dialog.setLocation(new Point(point.x, point.y + b.getHeight()));
dialog.setVisible(true);
});
});
}
Works for me...
Doc for Window.setLocationRelativeTo(...) says:
If the component is not null and is shown on the screen, then the
window is located in such a way that the center of the window
coincides with the center of the component.
So the behavior you get is normal.
(Hope I have correctly understood your issue)
Related
We have a project for university which is a program to hold handouts and feedback for courseworks done.
What we've thought of is breaking the whole thing down into smaller pieces, for example:
You have a coursework which requires to write a program and a report on results etc.
So the user will create a new coursework by selecting the "code" and "report" options, since that's what is required. And then we need to create the respective tabs in the program so the user can input what is needed.
I have created all necessary forms and windows, It's just I'm not sure how to move on forward.
a) where should I put my code? should I have it on the "create" event?
b) how do I do this whole custom population thing?
Obviously, I'm not asking for the entire thing in code. I'm not even sure what to read and what to search for.
Following are some screenshots of the ui to help explain what I mean.
New project window
How the main window should be after creating a new projet. Notice the various tabs.
A form for report feedback
On your "Create" button click check for the checkbox.isSelected() and use the method below as:
if(reportCheckbox.isSelected()){
addonScreen(new reportFrame(),"Report Submission");
addonScreen(new reportFeedbackFrame(),"Report Feedback");
}
Use a desktop pane as a container...add your tabbed pane to it
public static JTabbedPane tabbedPane = new JTabbedPane();
jDesktopPane1.add(tabbedPane);
Use this method to add tabs to the layout at runtime
public static void addOnScreen(JInternalFrame inFrame, String title) {
//border for the internal frame
javax.swing.plaf.InternalFrameUI ifu = inFrame.getUI();
((javax.swing.plaf.basic.BasicInternalFrameUI) ifu).setNorthPane(null);
Border b1 = new LineBorder(new Color(114, 139, 173), 3, true) {
};
tabbedPane.setBounds(0, 0, jDesktopPane1.getWidth(), jDesktopPane1.getHeight());
inFrame.setLocation(0, 0);
inFrame.setSize(jDesktopPane1.getWidth(), jDesktopPane1.getHeight());
inFrame.setBorder(b1);
JPanel jp = new JPanel();
jp.setLayout(new GridLayout());
jp.setOpaque(true);
jp.add(inFrame);
tabbedPane.addTab(title, jp);
tabbedPane.setSelectedComponent(jp);
inFrame.requestFocusInWindow();
inFrame.setVisible(true);
tabbedPane.setVisible(true);
}
I am a newbie, I have a question. I am trying to use Eclipse to write a Java windows application, in which I will have a main window, which will contain several things, like a dashboard sort of thing, and it will have buttons, for example to add a record to a database, and this button when pressed, will open a new relevant window on top.
I tried to start, I wrote this code in Java, and for some reason, the button is in the size of the frame...full screen ! How do I fix it ?
Can you suggest me better ideas for a design than what I specified ?
Thank you
public class MainClass {
public static void main(String[] args) {
// TODO Auto-generated method stub
JFrame jfrm = new JFrame("Frame1");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
jfrm.setSize(screenSize.width, screenSize.height);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel jlab = new JLabel("Hello");
jfrm.add(jlab);
JButton button = new JButton("Button");
button.setSize(new Dimension(50, 50));
button.setLocation(500, 350);
jfrm.getContentPane().add(button);
jfrm.setVisible(true);
}
Add a layout to the JFrame first. For example FlowLayout:
jfrm.setLayout(new FlowLayout());
"I tried to start, I wrote this code in Java, and for some reason, the button is in the size of the frame...full screen ! How do I fix it ?"
The reason the button stretches is because JFrame has a default BorderLayout that does not respect the preferred sizes of child components.
The solution is to set the Layout Manager to layout that does respect preferred sizes. The image blow from this example shows the most common Layout Managers and show visually which one respect the preferred size of child components.
Also, the BorderLayout is also the reason your JLabel does not show. By default, every component that is added to a BorderLayout without a position specified e.g. BorderLayout.SOUTH, will automatically be placed in the BorderLayout.CENTER position. Each position may only have one component. So when you add the JLabel it goes to the CENTER, but when you add the JButton, it also goes the CENTER, kicking out the JLabel.
If you've never encountered Layout Managers, this is probably all confusing to you. You should take the time to go over How to Layout Components Within a Container
You need to use some form of layoutmanager, you can use this information: http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
You must use Layout manager.
Use this code..
public static void main(String[] args)
{
JFrame jfrm = new JFrame("Frame1");
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
jfrm.setSize(screenSize.width, screenSize.height);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel jlab = new JLabel("Hello");
jfrm.add(jlab);
JButton button = new JButton("Button");
button.setSize(new Dimension(50, 50));
button.setLocation(500, 350);
jfrm.add(button);
jfrm.setVisible(true);
jfrm.setLayout(new FlowLayout());
jfrm.pack();
you forgot to add pack(). so use this code to get Jlabel and JButtion side by side.
Thanks...
I have a JFrame that contains two nested JSplitPanes. I want them to be set to an exact proportion on startup.
I can not use setDividerLocation(int) as I don't know the frame's size yet (I maximize it on startup). So, I use the proportional version, setDividerLocation(double).
Code:
// ...
JSplitPane left = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
JSplitPane right = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
right.setResizeWeight(1); // So that I can move the dividers independently
left.setLeftComponent(scrollPane1);
right.setLeftComponent(scrollPane2);
right.setRightComponent(scrollPane3);
left.setRightComponent(right);
add(left, BorderLayout.CENTER);
add(statusLabel, BorderLayout.PAGE_END);
setVisible(true);
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
left.setDividerLocation(0.3);
right.setDividerLocation(0.7);
// ...
Now, sometimes this works perfectly fine, but sometimes it doesn't change the dividers. I believe this is because the frame is not loaded on screen. From the setDividerLocation(double) docs:
If the split pane is not correctly realized and on screen, this method will have no effect (new divider location will become (current size * proportionalLocation) which is 0).
Is there a way to wait until the frame is "on screen"? This is probably about a few milliseconds, but it still breaks the layout on startup. I would not like to use Thread.sleep with a fixed value but some way that works with Swing.
EDIT: I tried the hack Behe suggested. It did not work, so it might not be about the timing.
EDIT 2: I debugged some more. It appears that this is caused by my resize weight being set to 1. However this is required by my layout.
I found a way. I added a ComponentListener to the inner JSplitPane that notifies me when it is resized by the frame maximizing. When that happens I can then safely set the resize weight.
final JSplitPane left = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
final JSplitPane right = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
left.setDividerLocation(0.3);
right.setDividerLocation(0.3);
left.setLeftComponent(scrollPane1);
right.setLeftComponent(scrollPane2);
right.setRightComponent(scrollPane3);
left.setRightComponent(right);
add(left, BorderLayout.CENTER);
add(status, BorderLayout.PAGE_END);
setVisible(true);
right.addComponentListener(new ComponentAdapter() {
#Override
public void componentResized(ComponentEvent e) {
right.setResizeWeight(1);
}
});
setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
I have a class PanelTrial extends JPanel & implements GroupLayout. In it, I have a JTabbedPane namely jTabbedPane on left & another JPanel namely rightPanel on right. In rightPanel, I load 2 panels (namely compoPanel, btnsPanel) alternatively during runtime.
My Issue : Width of compoPanel, btnsPanel is different (and I want it to be different). Initially compoPanel (that is bigger in W) is loaded in the rightPanel. I am looking for is, when I load btnsPanel in rightPanel, I want the jTabbedPane's size to increase and occupy all free space. I update the PreferredSize of jTabbedPane & rightPanel - and their sizes also change. BUT location of rightPanel doesn't move to extreme right - this makes it in the middle of jTabbedPane.
Here is the code that I use :
orgTabDimen = new Dimension(350, 600);
newTabDimen = new Dimension(500, 600);
orgRghtDimen = new Dimension(280, 574);
newRghtDimen = new Dimension(50, 574);
private void updateRightPanel(boolean showBtnPanel) {
rightPanel.removeAll();
GroupLayout layout = (GroupLayout) rightPanel.getLayout();
if (showBtnPanel) {
// SHOW BTNSpANEL
layout.setHorizontalGroup(layout.createSequentialGroup()
.addComponent(btnPanel));
layout.setVerticalGroup(layout.createParallelGroup(
Alignment.TRAILING).addComponent(btnPanel));
// Set respective dimesions
rightPanel.setPreferredSize(newRghtDimen);
this.jTabbedPane1.setPreferredSize(newTabDimen);
} else {
// SHOW COMPOpANEL
layout.setHorizontalGroup(layout.createSequentialGroup()
.addComponent(compoPanel));
layout.setVerticalGroup(layout.createParallelGroup(
Alignment.TRAILING).addComponent(compoPanel));
rightPanel.setPreferredSize(orgRghtDimen);
this.jTabbedPane1.setPreferredSize(orgTabDimen);
}
jPanel1.validate();
this.validate();
}
Can anyone help me solve this issue - am stuck up here. Can't figure out a way where the btnsPanel shows up on extreme right. I even tried with calling invalidate(), but that also didn't help me.
Any help is highly appreciative.
Thanks
Simply stated, I am trying to make a game I am working on full-screen.
I have the following code I am trying to use:
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gs = ge.getDefaultScreenDevice();
if(!gs.isFullScreenSupported()) {
System.out.println("full-screen not supported");
}
Frame frame = new Frame(gs.getDefaultConfiguration());
Window win = new Window(frame);
try {
// Enter full-screen mode
gs.setFullScreenWindow(win);
win.validate();
}
Problem with this is that I am working within a class that extends JPanel, and while I have a variable of type Frame, I have none of type Window within the class.
My understanding of JPanel is that it is a Window of sorts, but I cannot pass 'this' into gs.setFullScreenWindow(Window win)... How should I go about doing this?
Is there any easy way of calling that, or a similar method, using a JPanel?
Is there a way I can get something of type Window from my JPanel?
-
EDIT: The following method changes the state of JFrame and is called every 10ms:
public void paintScreen()
{
Graphics g;
try{
g = this.getGraphics(); //get Panel's graphic context
if(g == null)
{
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(frame.getExtendedState()|JFrame.MAXIMIZED_BOTH);
frame.add(this);
frame.pack();
frame.setResizable(false);
frame.setTitle("Game Window");
frame.setVisible(true);
}
if((g != null) && (dbImage != null))
{
g.drawImage(dbImage, 0, 0, null);
}
Toolkit.getDefaultToolkit().sync(); //sync the display on some systems
g.dispose();
}
catch (Exception e)
{
if(blockError)
{
blockError = false;
}
else
{
System.out.println("Graphics context error: " + e);
}
}
}
I anticipate that there may be a few redundancies or unnecessary calls after the if(g==null) statement (all the frame.somethingOrOther()s), any cleanup advice would be appreciated...
Also, the block error is what it seems. I am ignoring an error. The error only occurs once, and this works fine when setup to ignore the first instance of the error... For anyone interested I can post additional info there if anyone wants to see if that block can be removed, but i'm not concerned... I might look into it later.
Have you made any progress on this problem? It might be helpful if you could update your question with your expected behavior and what the code is actually doing? As was already pointed out, JFrame is a subclass of Window, so if you have a JFrame, you don't need a Window.
For what it's worth, I have a Java app which works in fullscreen mode. Although the screen is not repainted as often as yours, it is repainted regularly. I do the following to enter fullscreen:
// pseudo-code; not compilable
JPanel container = new JPanel();
container.setOpaque( true ); // make sure the container will be visible
JFrame frame = new JFrame();
frame.getContentPane().add(container); // add the container to the frame
frame. ... //other initialization stuff, like default close operation, maximize, etc
if ( fullScreenModeIsSupported )
frame.setUndecorated( true ); // remove window decorations from the frame
gs.setFullScreenWindow( frame );
frame.validate();
Then whenever I need to update the screen, I just plug a new JPanel into the container JPanel:
// pseudo-code; not compilable
container.removeAll(); // clean out the container
container.add( jPanelWithNewDisplay ); // add the new display components to the container
container.validate(); // update and redisplay
container.repaint();
Can't claim that it's technically perfect, but it works well for me. If the pseudo-code examples don't cut it, I can spend some time putting together a compilable example.
JPanel is not a subclass of Window. JFrame is.
So you could try:
JFrame yourFrame = new JFrame();
yourFrame.add(yourPanel);
appyYourFullScreenCodeFor( yourFrame );
That should work.
I think I got what you need.
Set the frame undecorated, so it
comes without any title bar and
stuff.
Add your panel to the frame., so it
looks like only your panel is shown.
Maximize your frame. So now it
should look like there's only your
panel taking the full screen without
and window stuff.
frame.setUndecorated(true);
frame.add(panel); //now maximize your
frame.
Note: Its important to note that the undecorated API can only be called when your frame is undisplayable, so if its already show, then first you need to do setVisible(false).
EDIT1: If all you want is to get the window containing your panel, then you can do this:
Window win = SwingUtilities.getAncestorOfClass(Window.class, myPanel);
Once you get the window instance you can pass it wherever you want.
EDIT2: Also the Frame class extends Window so you can directly do gs.setFullScreen(frame). You dont need to create a new window for that frame.
My understanding of JPanel is that it
is a Window of sorts
Why would you think that? Did you read the API? Does JPanel extend from Window?
You can try using the SwingUtilities class. It has a method that returns the Window for a given component.