The native Windows LookAndFeel in Java 6 seems to incorrectly size some fonts.
Test program:
import java.awt.*;
import java.awt.event.KeyEvent;
import javax.swing.*;
public class Test {
public static void main(String[] arg) throws Exception {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
e.printStackTrace();
}
final JMenuBar mb = new JMenuBar();
final JMenu file = new JMenu("File");
file.setMnemonic(KeyEvent.VK_F);
mb.add(file);
final JToolBar toolbar = new JToolBar();
final JButton button = new JButton("Button");
toolbar.add(button);
final JLabel label = new JLabel("Basic Colors");
final JPanel panel = new JPanel(new BorderLayout());
panel.add(toolbar, BorderLayout.PAGE_START);
panel.add(label, BorderLayout.CENTER);
final JFrame frame = new JFrame();
frame.setJMenuBar(mb);
frame.add(panel);
frame.setTitle("Test");
frame.setSize(400,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
Output, compared with a native Windows app on Vista:
While the text in the test app menu bar is sized correctly, the rest of the text is smaller than in the native app next to it. Zoomed in, you can see that the text in the test app JLabel is 1px shorter than in the native app:
Is this a bug in the Windows LaF, or are we misusing it? If it's a bug, is there a known workaround?
Java 6 uses its own font renderer, including the implementation of subpixel antialiasing / hinting whatsit. While the output is meant to be similar to Windows' rendering, either the top of the B being at a pixel boundary, or it being rounded, or both, throws the Java renderer off. The Windows font renderer decides to place the top of the letter above the boundary, while the Java one places it below. The 'l' looks like it's at the same height in both samples, so it doesn't seem like the renderer gets the height of every letter wrong. Maybe try comparing with some letters where the top is a straight line, like a T or an E?
Related
I'm fairly new to coding and was trying to make a simple GUI with JAVA Swing. I wanted to place an image in a label but for some reason does the image does not show when I run the Code. Are there some requirements I don't know of or did I do something wrong?
package Part2Lables;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
//Label = a GUI display area for a string of text, an image, or both
ImageIcon image = new ImageIcon("Kakashi.png");
JLabel label = new JLabel(); // create a label
label.setText("This is text in a label"); // set text to the label
label.setIcon(image);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(500,500);
frame.setVisible(true);
frame.add(label);
}
}
This is the code I used (I did use imports but they are not visible in the pic.)
private void setupGUI(){
// Setup Frame
f = new JFrame("Shape Image Generator");
f.setBounds(500, 150, 450, 350);
f.setLayout(new GridLayout(8,1));
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
}
I create the frame above, then 8 panels. I create various components and add them to the panels and everything works fine. Until I created an ImageIcon and added it to a label and added that label to the 8th panel. The image used is 140x129 pixels. The problem is, only the top.... maybe 1/4 of the image is showing. If I change the frames dimensions in the code, more empty space is created between each panel, but only a slight bit more of the image is shown, so the image is still off of the screen. I'd say the window is easily adding 10 pixels of spacing for every 1 more pixel of the image it shows. If I drag the corners of the window to expand it, the same thing happens. If the window is maximized I still can only see a little over half of my now very stretched image.
Things I tried:
None of my components have preferred dimensions set, but I tried setting a preferred dimension for the label then panel that contains the ImageIcon and it only added the difference between the image and preferred size in gray space above the image, pushing it further offscreen. So, I undid that.
Adding the label containing the ImageIcon to a different panel which was not the 8th and last panel, in this case, the image is still cut off, but at the point that it gets cut off, the components on the panel underneath it appear (over top of the background coloring which cuts off the image).
Exhaustively Googling this situation with about 30 different ways of phrasing it and not finding a solution.
(row1 - row8 are JPanels, I didn't include the coding for them)
ImageIcon iconStart = createImageIcon("/images/ShapeClipart.png", "Shapes");
JLabel imgLabel = new JLabel();
row8.add(imgLabel);
// Add image to image label
imgLabel.setIcon(iconStart);
// Add panels to frame
f.add(row1);
f.add(row2);
f.add(row3);
f.add(row4);
f.add(row5);
f.add(row6);
f.add(row7);
f.add(row8);
f.setVisible(true);
Window at execution
Window when stretched
edit:
adding f.pack() makes a very tall skinny window (the windows height taller than my screen) but it still looks like when I manually expand the window (empty space between panels, image partially offscreen), even if I take out f.setBounds and only use f.setLocation.
You are using a GridLayout. This gives all of the enclosed panels the same amount of space. In this case it is a vertical grid.
You should probably use something a bit different. I might try a BorderLayout in the JFrame and put the a panel containing the top seven panels (in a GridLayout) into the CENTER, and then put the JLabel into the SOUTH portion of the JFrame.
There are other ways to lay it out, but this is the first I could think of.
GridLayout makes each cell in the grid the same size and the size of each cell is determined by the largest Component contained in the grid.
In your code, the icon is the largest component and you also have only one column in your grid so every row has the same height as your icon.
Since you also limit the size of your JFrame by calling method setBounds(), the Swing infrastructure cuts off the icon so that all the components fit into the bounds you specified.
One alternative, but not the only one, is to use BoxLayout since it uses the preferred size of each of its contained components.
Here is a sample GUI that matches the screen capture that you posted and uses BoxLayout.
import static javax.swing.WindowConstants.EXIT_ON_CLOSE;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.net.URL;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Shapes23 implements Runnable {
private JFrame frame;
#Override // java.lang.Runnable
public void run() {
showGui();
}
private JPanel createEighthRow() {
JPanel eighthRow = new JPanel();
URL url = getClass().getResource("paint-bursht.jpg");
Icon ico = new ImageIcon(url);
JLabel label = new JLabel(ico);
eighthRow.add(label);
return eighthRow;
}
private JPanel createFifthRow() {
JPanel fifthRow = new JPanel();
JTextField textField = new JTextField(20);
fifthRow.add(textField);
return fifthRow;
}
private JPanel createFirstRow() {
JPanel firstRow = new JPanel();
JLabel label = new JLabel("2D Shapes");
firstRow.add(label);
return firstRow;
}
private JPanel createFourthRow() {
JPanel fourthRow = new JPanel();
fourthRow.add(createRadioButton("Sphere"));
fourthRow.add(createRadioButton("Cube"));
fourthRow.add(createRadioButton("Cone"));
fourthRow.add(createRadioButton("Cylinder"));
fourthRow.add(createRadioButton("Torus"));
return fourthRow;
}
private JPanel createMainPanel() {
JPanel mainPanel = new JPanel();
BoxLayout layout = new BoxLayout(mainPanel, BoxLayout.PAGE_AXIS);
mainPanel.setLayout(layout);
mainPanel.add(createFirstRow());
mainPanel.add(createSecondRow());
mainPanel.add(createThirdRow());
mainPanel.add(createFourthRow());
mainPanel.add(createFifthRow());
mainPanel.add(createSixthRow());
mainPanel.add(createSeventhRow());
mainPanel.add(createEighthRow());
return mainPanel;
}
private JRadioButton createRadioButton(String text) {
JRadioButton radioButton = new JRadioButton(text);
return radioButton;
}
private JPanel createSecondRow() {
JPanel secondRow = new JPanel();
secondRow.add(createRadioButton("Circle"));
secondRow.add(createRadioButton("Rectangle"));
secondRow.add(createRadioButton("Square"));
secondRow.add(createRadioButton("Triangle"));
return secondRow;
}
private JPanel createSeventhRow() {
JPanel seventhRow = new JPanel();
JButton button = new JButton("Enter");
seventhRow.add(button);
return seventhRow;
}
private JPanel createSixthRow() {
JPanel sixthRow = new JPanel();
JTextField textField = new JTextField(20);
sixthRow.add(textField);
return sixthRow;
}
private JPanel createThirdRow() {
JPanel thirdRow = new JPanel();
JLabel label = new JLabel("3D Shapes");
thirdRow.add(label);
return thirdRow;
}
private void showGui() {
frame = new JFrame("Shape Image Generator");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Shapes23());
}
}
Here is a screen capture of how it looks. Note that I couldn't find the same icon as in your screen capture so I just used a different one.
I am not new to Java and OOP but I'm new to swing.
I want to write a software for building pedigrees. That means: right click into the middle of the drawing area and choose "New -> Man". A rectangle appears where I have right-clicked. Then I click on the rectangle and choose "New -> Sibling -> Woman" and the pedigree expands dynamically with a circle that is connected to the rectangle. You get the idea.
Additionally I need to save information for each node of the pedigree. Such as "mutation in gene x: positive".
I thought this must be perfect for OOP. I need every node of my pedigree to be an instance which draws itself into the drawing area. So ... a Jpanel? I extend JPanel, I give that class some attributes (such as "int mutationX = 1") and a method to add itself to the JFrame. At the moment I am only trying to add a rectangle into the middle of the screen via the menu. Easy step for a swing beginner. But the desired rectangle doesn't show up. So basically my questions are:
Am I even following the right approach of solving what I'm trying to achieve?
Why doesn't the rectangle show up?
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class HelloWorld
{
public static void main(String[] args)
{
JFrame frame = new JFrame("Hello World");
JPanel mainpanel = new JPanel();
JScrollPane scroll = new JScrollPane(mainpanel);
frame.add(scroll);
JMenuBar menubar = new JMenuBar();
frame.setJMenuBar(menubar);
JMenu file = new JMenu("File");
menubar.add(file);
JMenuItem exit = new JMenuItem("Exit");
exit.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
System.exit(0);
}
}
);
file.add(exit);
JMenuItem newMember = new JMenuItem("Add");
newMember.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
FamilyMember fm = new FamilyMember();
mainpanel.add(fm);
mainpanel.revalidate();
}
}
);
file.add(newMember);
frame.setLocation(400, 100);
frame.setPreferredSize(new Dimension(500,500));
frame.pack();
frame.setVisible(true);
}
}
public class FamilyMember extends JPanel {
#Override
protected void paintComponent(Graphics g){
super.paintComponent(g);
g.drawRect(230,80,10,10);
g.setColor(Color.RED);
g.fillRect(230,80,10,10);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(50, 50);
}
}
For the second point (why, a minima, it does not work/show any rectangle), you could take a look at Oracle docs (from more precise to broader concern) :
FlowLayout
Layout managers
Swing panels
In short,
your JPanel instance mainpanel has a FlowLayout as layout manager (which is the default for a new instance)
a flow layout will use each child preferred size
and you force the preferred size of your FamilyMember to 50x50
but you want to draw only south-east of 230,80 which is not in the 50x50 area.
=> it won't show.
For the first point, I'd say first : separate the model from the view.
I'm currently doing a Java assignment as a computer science fresher. As a part of that assignment I'm trying to bring up a secondary frame that the user can write UML code into which will then be passed into my main application and then into a class diagram.
The bit that I'm stuck with is that the JTextBox that I have put into this secondary frame is the size I want it to be, however the writing starts in the middle and does not change to a new line when it gets to the other size of the frame.
This is the image of what is currently happening:
Code
And this is the code that I currently have for this class if it's needed.
package classdesign;
import java.awt.*;
import javax.swing.*;
public class ClassCreation extends JFrame {
private JFrame frame;
private JLabel instructionlabel;
private JTextField inputUML;
private JButton upButton;
private String Message;
public void ClassCreation(){
frame = new JFrame();
frame.setSize(300, 400);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Class Design");
JPanel CreationPanel = new JPanel();
CreationPanel.setLayout(new BorderLayout());
instructionlabel = new JLabel("Fill Class details in using UML");
CreationPanel.add(instructionlabel,BorderLayout.NORTH);
inputUML = new JTextField("",20);
CreationPanel.add(inputUML,BorderLayout.CENTER);
frame.add(CreationPanel);
}
public Frame getFrame() {
return frame;
}
}
So, to summarise what I was hoping somebody could tell me how to do is to get the text input from the user to start in the top left and change to the next line when it gets to the far right, like any normal text editor etc...
use JTextPane or JEditorPane. Sample can be found at
http://docs.oracle.com/javase/tutorial/uiswing/components/editorpane.html
JTextField is a lightweight component that allows the editing of a single line of text. (source)
As it is a single-line component, whatever its size is the cursor will always be centered and will never go to the next line.
I would suggest you use a JTextArea as it is a multi-line area and allow the user to enter input as you want him to.
An example of using a text area (with a few other tips thrown in free - check the comments).
import java.awt.*;
import javax.swing.*;
// Has an instance of frame, does not need to extend it.
public class ClassCreation { //extends JFrame {
private JFrame frame;
private JLabel instructionlabel;
// as mentioned by talnicolas
private JTextArea inputUML;
// Don't give a method the same name as a class!!
//public void ClassCreation(){
public void initGui(){
frame = new JFrame();
//frame.setSize(300, 400); //pack() instead!
//frame.setLocationRelativeTo(null); // do something better
frame.setLocationByPlatform(true); // better!
//frame.setVisible(true); // do later
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Class Design");
JPanel CreationPanel = new JPanel();
CreationPanel.setLayout(new BorderLayout());
instructionlabel = new JLabel("Fill Class details in using UML");
CreationPanel.add(instructionlabel,BorderLayout.NORTH);
inputUML = new JTextArea("",7,30);
// very important next 2 lines
inputUML.setLineWrap(true);
inputUML.setWrapStyleWord(true);
// add it to a scrollpane
CreationPanel.add(new JScrollPane(inputUML),BorderLayout.CENTER);
frame.add(CreationPanel);
frame.pack(); // assume the natural size!
frame.setVisible(true);
for (int ii=0; ii<150; ii++) {
inputUML.append(SENTENCE);
inputUML.setCaretPosition( inputUML.getText().length() );
}
}
public static void main(String[] args) {
// Swing GUIs should be created and altered on the EDT.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ClassCreation cc = new ClassCreation();
cc.initGui();
}
});
}
private static String SENTENCE = "The quick brown fox jumps over the lazy dog! ";
}
I'm trying to make a GUI in java, but JMenuBar has been giving me a hard time for two days. Can someone please tell me why it isn't showing up?
import java.awt.*;
import javax.swing.*;
import javax.swing.JPanel;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.Arrays;
import javax.imageio.ImageIO;
public class selectionFrame extends JFrame
{
Font name;
Font title;
public void setup() //can't use constructer because this isn't given a size until after it is constructed.
{
//getContentPane().add(menuBar);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
getContentPane().setLayout(new FlowLayout());
//getContentPane().add(j);
setJMenuBar(createMenuBar());
//getContentPane().add(createMenuBar());
}
public JMenuBar createMenuBar()
{
JMenuBar menuBar;
JMenu m_file;
JMenuItem mi_save;
JMenuItem mi_load;
JMenu m_edit;
JMenuItem mi_tileHeight;
JMenuItem mi_tileWidth;
menuBar = new JMenuBar();
m_file = new JMenu("File");
m_edit = new JMenu("Edit");
mi_save = new JMenuItem("Save file", KeyEvent.VK_S);
mi_load = new JMenuItem("Load file", KeyEvent.VK_L);
mi_tileHeight = new JMenuItem("Set tile height", KeyEvent.VK_H);
mi_tileWidth = new JMenuItem("Set tile width", KeyEvent.VK_W);
menuBar.add(m_file);
m_file.add(mi_save);
m_file.add(mi_load);
menuBar.add(m_edit);
m_edit.add(mi_tileHeight);
m_edit.add(mi_tileWidth);
return menuBar;
}
public static void main(String[] args) //run
{
selectionFrame sel = new selectionFrame();
sel.setLocationRelativeTo(null);
sel.setSize((int) 400 + (sel.getInsets().left + sel.getInsets().right),(int) 400 + (sel.getInsets().top + sel.getInsets().bottom));
sel.setVisible(true);
sel.setTitle("Tiles/Meta");
sel.setResizable(false);
sel.setFocusable(true);
sel.getContentPane().setSize(sel.getSize());
sel.setLocation((int) sel.getX() - (sel.getWidth()/2),(int) sel.getY() - (sel.getHeight()/2));
sel.setup();
sel.repaint();
}
}
You have an awful lot of extra code there.
public class SelectionFrame extends JFrame
{
Font name;
Font title;
public SelectionFrame()
{
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setJMenuBar(createMenuBar());
}
public JMenuBar createMenuBar()
{
JMenuBar menuBar;
JMenu m_file;
JMenuItem mi_save;
JMenuItem mi_load;
JMenu m_edit;
JMenuItem mi_tileHeight;
JMenuItem mi_tileWidth;
menuBar = new JMenuBar();
m_file = new JMenu("File");
m_edit = new JMenu("Edit");
mi_save = new JMenuItem("Save file", KeyEvent.VK_S);
mi_load = new JMenuItem("Load file", KeyEvent.VK_L);
mi_tileHeight = new JMenuItem("Set tile height",
KeyEvent.VK_H);
mi_tileWidth = new JMenuItem("Set tile width",
KeyEvent.VK_W);
menuBar.add(m_file);
m_file.add(mi_save);
m_file.add(mi_load);
menuBar.add(m_edit);
m_edit.add(mi_tileHeight);
m_edit.add(mi_tileWidth);
return menuBar;
}
public void main( String args[] )
{
SelectionFrame sel = new SelectionFrame();
sel.setLocationRelativeTo(null);
sel.setSize(400 + (sel.getInsets().left + > sel.getInsets().right), 400
+ (sel.getInsets().top + sel.getInsets().bottom));
sel.setTitle("Tiles/Meta");
sel.setResizable(false);
sel.setFocusable(true);
sel.getContentPane().add( new JLabel( "Content", SwingConstants.CENTER),
BorderLayout.CENTER );
sel.setLocation(sel.getX() - (sel.getWidth() / 2), sel.getY() - > (sel.getHeight() / 2));
sel.setVisible(true);
}
}
That shows up with a menu bar and everything. if you add your content to the CENTER of the content pane (by default a border layout), the center automatically fills the whole content area, you don't have to resize anything.
This shows up as a window with a menu bar and everything works fine.
What platform are you doing this on? I'm on Vista, i get what i expect to see.
What Java version are you using? Your menu bar shows up fine in 1.6.0_10 on my system. Try wrapping the body of your main method in an invokeLater() call so that it runs on the correct thread, like so:
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
selectionFrame sel = new selectionFrame();
sel.setLocationRelativeTo(null);
sel.setSize((int) 400 + (sel.getInsets().left + sel.getInsets().right),
(int) 400 + (sel.getInsets().top + sel.getInsets().bottom));
sel.setTitle("Tiles/Meta");
sel.setResizable(false);
sel.setFocusable(true);
sel.getContentPane().setSize(sel.getSize());
sel.setLocation((int) sel.getX() - (sel.getWidth() / 2),
(int) sel.getY() - (sel.getHeight() / 2));
sel.setup();
sel.setVisible(true); // Follow Kendrick's advice too.
}
});
}
Turns out you HAVE to set the JMenuBar inside the JFrame's constructor. I figured this out while looking at the differences between my code, and the marked answers code.
Thank you for your wonderful answer, John Gardner. Without you I would have most likely been stuck for months.
In my case I have tracked down a missing menu bar to a bug where I set the RootPane layout, tsk tsk. The RootPane (see eg http://download.java.net/jdk7/archive/b123/docs/api/javax/swing/JRootPane.html ) controls the layout of the menu bar, so when I changed its layout manager it lost the bar.
Instead, one should use the ContentPane to layout and add components to, eg:
frame.getContentPane().setLayout(...);
frame.getContentPane().add(...);
For future reference... this has nothing to do with the component being visible (as the OP said, the frame is visible but the menu bar is not), I have working code that sets the JMenuBar outside the constructor, and while being swing-thread-safe is Good Practice, it is not the cause of the problem.
sel.setVisible(true);
Should be the last thing you call......
Also, just before the call to sel.setVisible(true); pls invoke sel.pack();
Pls note that instead of setSize it is better to use setPreferredSize, which is leveraged during frame packing.
Not directly relevant to your question, but still -- the use of a good layout manager is a huge time and frustration saviour when using Swing. MigLayout is simply an excellent one-stop layout manager.
When I compile and run it, it shows up with a menu bar with file and edit menu items. Were you expecting more?
Also, capitalize your class- SelectionFrame
EDIT:
One thing I forgot to look at, your code and every answer here is technically wrong. Often, it happens to work, but you are not allowed to do anything with Swing components unless you are in the AWT worker thread.
Normally you don't have to think about the worker thread much because every event that comes from your window will be on the worker thread anyway, but there is a tendency to forget about it when you construct your initial frame--and more often than not it just works anyway.
Sun used to recommend that you can work with components outside the AWT thread until the window has been realized (with either pack() or setVisible(true)) but this is no longer considered safe.
The easiest way to fix this might be for your main to create a swing worker thread before newing your SelectionFrame.
There is only a 50-50 chance this will fix your problem, but you should still take it into consideration whenever working on a GUI.