Java JFrame .setSize(x, y) not working?

Create a custom component, extending from JPanel, override its getPreferredSize method to return the size of the window you would like. Either add this to your frame or set it as the frame’s content pane. Call pack on the frame Updated with example On my PC, the Frame size = java.awt.Dimension[width=216,height=238] import java.awt.BorderLayout; import java.awt.Dimension; … Read more

How to switch JPanels in a JFrame from within the panel?

Like so : import java.awt.BorderLayout; import java.awt.CardLayout; import java.awt.Color; import java.awt.Dimension; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class CardLayoutDemo extends JFrame { public final String YELLOW_PAGE = “yellow page”; public final String RED_PAGE = “red page”; private final CardLayout cLayout; private final JPanel mainPane; boolean isRedPaneVisible; public CardLayoutDemo(){ setTitle(“Card Layout Demo”); setDefaultCloseOperation(EXIT_ON_CLOSE); setLocationRelativeTo(null); mainPane … Read more

How to add an image to a JFrame title bar?

Since JPanel doesn’t have a title bar, I’m assuming that you’re referring to JFrame. That being said, use setIconImage(…). Here is an example of using setIconImages(). import java.awt.Image; import javax.swing.*; import javax.imageio.ImageIO; import java.net.URL; import java.util.*; class FrameIcons { public static void main(String[] args) throws Exception { URL url16 = new URL(“http://i.stack.imgur.com/m0KKu.png”); URL url32 = … Read more

How to add JCheckBox in JTable?

Start with How to use tables. Your table model needs several things. It needs to return Boolean.class from the getColumnClass method for the appropriate column. You will need to override this method. The method isCellEditable will need to return true for the table column you want to make editable (so the user can change the … Read more

Center JDialog over parent

On the JDialog you’ve created you should call pack() first, then setLocationRelativeTo(parentFrame), and then setVisible(true). With that order the JDialog should appear centered on the parent frame. If you don’t call pack() first, then setting the location relative to the parent doesn’t work properly because the JDialog doesn’t know what size it is at that … Read more

Only allowing numbers and a symbol (-) to be typed into a JTextField

Use DocumentFilter: NumberOnlyFilter.java: import javax.swing.*; import javax.swing.text.*; import java.util.regex.*; public class NumberOnlyFilter extends DocumentFilter { public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException { StringBuilder sb = new StringBuilder(); sb.append(fb.getDocument().getText(0, fb.getDocument().getLength())); sb.insert(offset, text); if(!containsOnlyNumbers(sb.toString())) return; fb.insertString(offset, text, attr); } public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attr) … Read more