When I call pack() on a JInternalFrame it is not packed correctly, mostly too tight.
Edit:
Here is a minimal example that shows the behavior described above.
It also seems to depend on the used lookandfeel (here: Nimbus).
import javax.swing.*;
public class JInternalFrameTester {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
System.err.println("Failed setting NimbusLookAndFeel");
}
JFrame frame = new JFrame();
JDesktopPane desktop = new JDesktopPane();
desktop.setOpaque(true);
frame.setContentPane(desktop);
frame.setSize(250, 250);
frame.setVisible(true);
JInternalFrame iframe = new JInternalFrame("Internal Frame");
JTextField textfield = new JTextField("Any text here");
iframe.add(textfield);
iframe.setVisible(true);
/* XXX If placed here, it crashes the layouts */
iframe.pack();
desktop.add(iframe);
/* XXX If placed here, the layout is right */
//iframe.pack();
}
}
You have to call pack() after adding the JInternalFrame to the respective JDesktopPane.
Related
I have i situation when my Tabbedpane have 3 tabs and then I choose someone of them, it sets in first position and move whole tabs.
I couldn't find in Internet anything like that, everyone has fixed positions of their tabs. How to fix tabs?
//EDITED
import javax.swing.*;
import java.awt.*;
public class Main {
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (UnsupportedLookAndFeelException | ClassNotFoundException | InstantiationException |
IllegalAccessException e) {
throw new RuntimeException(e);
}
JFrame frame = new JFrame("Somest");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
JPanel UpperPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.RIGHT, JTabbedPane.WRAP_TAB_LAYOUT);
tabbedPane.addTab("Calculation", new JLabel());
tabbedPane.addTab("Store", new JLabel());
tabbedPane.addTab("Settings", new JLabel());
UpperPanel.add(tabbedPane);
frame.add(UpperPanel);
}
}
Find out that try {...} "enable" this problem. Used try {...} to change JChooser from standard to more-or-less Windows chooser view. What I have to do?
The reason is that this is implemented differently in various Look and Feel implementations (See here, section 6.2.1):
[...] selecting a tab that is not in the frontmost row or column moves that row or column to the front. This does not occur in a JTabbedPane using the default Metal L&F as can be seen in the TabbedPaneDemo example above. However, this does occur when using the Windows, Motif, and Basic L&Fs. This feature was purposefully disabled in the Metal L&F [...]
The responsible method is shouldRotateTabRuns(...) and returns true or false in various TabbedUI implementations (see Metal L&F vs. Basic L&F).
To prevent tab rotation, you could overwrite like this:
public class TabExample extends JFrame {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
TabExample app = new TabExample();
app.setVisible(true);
}
});
}
private TabExample() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 500);
setLocationRelativeTo(null);
setVisible(true);
JPanel UpperPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.RIGHT, JTabbedPane.WRAP_TAB_LAYOUT);
// Check if TabbedPaneUI is instance of WindowsTabbedPaneUI
// You may add checks for other L&F
if (tabbedPane.getUI() instanceof com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI) {
tabbedPane.setUI(new MyWindowsTabbedPaneUI());
}
tabbedPane.addTab("Calculation", new JLabel());
tabbedPane.addTab("Store", new JLabel());
tabbedPane.addTab("Settings", new JLabel());
UpperPanel.add(tabbedPane);
add(tabbedPane, BorderLayout.NORTH);
}
// Class that overwrites WindowsTabbedPaneUI and returns false
class MyWindowsTabbedPaneUI extends com.sun.java.swing.plaf.windows.WindowsTabbedPaneUI{
#Override
protected boolean shouldRotateTabRuns(int tabPlacement) {
return false;
}
}
}
Everything seems right, but the menubar doesn't show up, I might just be missing something.
This frame gets called from the main class with a simple new AffmView();. Could that cause this problem?
public class AffmView extends JFrame {
public AffmView() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
Logger.getLogger(AffmView.class.getName()).log(Level.SEVERE, null, ex);
}
this.setSize(700, 500);
this.setTitle("'s Factorio Mod Manager");
this.setLocationRelativeTo(null);
JMenuBar menubar = new JMenuBar();
JMenu packsMenu = new JMenu("Modpacks");
JMenuItem newPackMI = new JMenuItem("New Pack");
packsMenu.add(newPackMI);
menubar.add(packsMenu);
this.setJMenuBar(menubar);
this.rootPane.setLayout(new GridBagLayout());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//snipped so that StackOverflow wont make me write an essay, but its
//just some basic swing
this.setVisible(true);
}
}
I have confirmed that none of the snipped parts cause the issue.
As mentioned by #FastSnail and #camickr in the comments, the issue lies with the usage of this.rootPane.
getContentPane() should have been used instead.
I have a JFrame that I create in the main function. I want to add a JTextField to it. The problem I'm having is that the JFrame is created and then - with about a second delay - the JTextField is added. Is there a way I can draw the text field to my window and then show all at once? Thanks in advance!
For reference, here is my code:
public class Window {
public static final JFrame window = new JFrame();
public static final JTextField input = new JTextField();
private static void loadWindow(){
window.setSize(800, 600);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new FlowLayout());
input.setPreferredSize(new Dimension(400, 60));
window.add(input);
window.setVisible(true);
}
public static void main(String[] args){
loadWindow();
}
}
Here's the timeline of what's happening:
First second:
Second after:
Chalk this one to weirdness...
I changed
public static final JTextField input = new JTextField();
to
public static final JTextField input = new JTextField(20);
And it worked fine...
I would however encourage you...
to avoid using setPreferredSize as it won't always work on every platform as you don't control the rendering pipelines which can affect the amount of space a component will need in order to render properly
Start your UI's in the EDT...
For example...
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
loadWindow();
}
});
I don't believe you would want the JFrame and JTextField to have the final access modifiers.
How final keyword works
Try removing these.
Use setSize() to configure the size of Components. setPreferrredSize() will be applied at an unknown time or whenever you call pack on the component.
I'm trying to make a little application in console. The whole thing is yes or no if statements.
Everything is working somewhat perfectly, but the last thing that happens is a JPanel pops up with a picture in it.
The rest of the app works and the JPanel with the picture in it works but i don't know how to make the JPanel to show up, can someone tell me what I should do, plz? ty
Start by taking a look at Creating an UI with Swing
I order for any component to be showing on the screen, it needs to be attached to some kind of Window, for example...
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new YourAwesomePanelHere());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
when a JPanel draw a image you must repain it ;
test such as:
panel.revalidate();
panel.repain;
How do i create a JFrame window in eclipse on a Mac that has an icon that makes the window full screen like the double arrow icon on most windows at the top right??
Take a look at
Fullscreen feature for Java Apps on OSX Lion
And Java Runtime System Properties, which may be of interested
or How can I do full screen in Java on OSX if those were the wrong feature you wanted
UPDATE
Lucky for you JFrame extends Window via Frame...
public class TestMacFullScreen {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setLayout(new BorderLayout());
JLabel label = new JLabel("Look ma, no hands");
frame.add(label);
enableOSXFullscreen(frame);
frame.setVisible(true);
}
});
}
public static void enableOSXFullscreen(Window window) {
try {
Class util = Class.forName("com.apple.eawt.FullScreenUtilities");
Class params[] = new Class[]{Window.class, Boolean.TYPE};
Method method = util.getMethod("setWindowCanFullScreen", params);
method.invoke(util, window, true);
} catch (ClassNotFoundException exp) {
exp.printStackTrace();
} catch (Exception exp) {
exp.printStackTrace();
}
}
}