Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Can anybody explain why pack() isn't working on this JFrame?
It's got one JPanel inside (actually, a class that extends JPanel - inner).
Here's the code I'm using:
inner.setPreferredSize(new Dimension(800, 600));
add(inner);
pack();
setResizable(false);
setLocationRelativeTo(null); // to center the JFrame on screen
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
The extra space seems to be the exact width and height of the JFrame's decoration (that is, the JFrame's dimensions minus the JPanel's dimensions).
Invoke setResizable(false) before pack(). It's no coincidence that "extra space seems to be the exact width and height of the JFrame's decoration."
Addendum: Here's an sscce showing that my initial guess was incorrect.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
/** #see http://stackoverflow.com/questions/7924830 */
public class NonResizable extends JPanel {
public NonResizable() {
this.setPreferredSize(new Dimension(400, 300));
this.setBackground(Color.lightGray);
this.setBorder(BorderFactory.createLineBorder(Color.blue));
}
private void display() {
JFrame f = new JFrame("NonResizable");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new FlowLayout());
f.setBackground(Color.white);
f.add(this);
f.pack();
f.setResizable(false);
f.setLocationRelativeTo(null);
f.setSize(500, 400);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new NonResizable().display();
}
});
}
}
Turns out I was drawing (active rendering) from the JFrame and not the JPanel... so the extra space was the result of the JPanel's draw() object being aligned at 0,0 (in the JFrame).
Solved by this post.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a probably annoying request. Could someone demonstrate how to use one of these static Java swing utility methods? I am looking for a simple, extremely simple, example.
public static void paintComponent(java.awt.Graphics, java.awt.Component, java.
awt.Container, int, int, int, int);
public static void paintComponent(java.awt.Graphics, java.awt.Component, java.
awt.Container, java.awt.Rectangle);
These static Java swing methods are found in the javax.swing.SwingUtilities package.
Thank-you for reading this and any help given.
you can find some usages of most public api methods from grepcode. and here is yours.
EDIT
a running example may be like this
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.border.LineBorder;
public class Test {
public static void main(String[] args) {
SwingUtilities.invokeLater( () -> {
JFrame frame = new JFrame();
JPanel panel = new JPanel() {
JLabel label = new JLabel("<html>SwingUtilities.paintComponent method usage example");
{
label.setBorder(new LineBorder(Color.red));
}
protected void paintComponent(Graphics g) {
// render label which is not part of component hierarchy
// and paint it on this panel at location (10,10) with dimension (200,50)
SwingUtilities.paintComponent(g, label, this, 10, 10, 200, 50);
}
};
frame.setContentPane(panel);
frame.setSize(300, 200);
frame.setVisible(true);
});
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Helloo!
In the Java Application I have a JScrollPane and in that scroll I have a JTextArea
JTextArea TextArea = new JTextArea("Text");
scroll = new JScrollPane(TextArea);
scroll.setBounds(150,100,250,100);
And I got the scroll.
But If the user clicks a JButton the location of the JScrollPane should change...
I have this code and it works if the scroll doesn't have the TextArea
scroll.setBounds(50,100,250,100);
but if the scroll has the TextArea it doesn't move at all
Any idea what is happening?
Change bounds of a JScrollPane ..
The bounds come down to the position and size of the component.
The best way to change the size of a scroll pane is to change the size of the component it is displaying. A text area can be resized by setting the number of rows & columns (easily specified in the constructor), or by setting a different font size.
The best way to position the scroll pane is to use layouts, along with layout padding and borders for white space.
Use vScrollPane.setValue() hScrollPane.setValue() methods.
Like this
import java.awt.event.*;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
class JScrollPaneToTopAction implements ActionListener {
JScrollPane scrollPane;
public JScrollPaneToTopAction(JScrollPane scrollPane) {
if (scrollPane == null) {
throw new IllegalArgumentException(
"JScrollPaneToTopAction: null JScrollPane");
}
this.scrollPane = scrollPane;
}
public void actionPerformed(ActionEvent actionEvent) {
JScrollBar verticalScrollBar = scrollPane.getVerticalScrollBar();
JScrollBar horizontalScrollBar = scrollPane.getHorizontalScrollBar();
verticalScrollBar.setValue(20);
horizontalScrollBar.setValue(100);
}
}
public class JScrollPaneToTopActionDemo {
public static void main(String args[]) {
JFrame frame = new JFrame("Tabbed Pane Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel("Label");
label.setPreferredSize(new Dimension(1000, 1000));
JScrollPane jScrollPane = new JScrollPane(label);
JButton bn = new JButton("Move");
bn.addActionListener(new JScrollPaneToTopAction(jScrollPane));
frame.add(bn, BorderLayout.SOUTH);
frame.add(jScrollPane, BorderLayout.CENTER);
frame.setSize(400, 150);
frame.setVisible(true);
}
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
According to your instruction i decided to use GridBagLayout, but i also face a problem in positioning buttons in a panel the button expected to be at top right, But it is displayed in the center, Please tell me what is the problem in my code`
import java.awt.ComponentOrientation;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
public class Test extends JFrame {
private JPanel contentPane;
private JButton button2;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Test frame = new Test();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Test() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 573, 410);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new GridBagLayout());
contentPane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
setContentPane(contentPane);
button2 = new JButton("button2");
GridBagConstraints c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
contentPane.add(button2, c);
}
}
this the the output
http://postimg.org/image/bhnzskznj/
Since the answer just asks for a Layout - it's layout recommendation time!
My favourite - http://www.miglayout.com/ which satisfied all my layout needs for my last Swing project. wrap, span x and center layout arguments should be all that is needed to do what your picture does.
This is exactly why absolute layouts are not recommended.
Your best approach will be to use something like GridBagLayout which will position the elements and then let them move as the screen resizes and reshapes.
There are plenty of good documentation and tutorials online for GridBagLayout that will get you started. You can also use tools built into some IDEs (NetBeans has a good one for example) to let you lay things out graphically.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Here's how to change card layouts from a menu item. I asked how to do it earlier but no luck. I have figured out the answer so here's what it does; 1. Builds your main frame when running the java file. Then in the menu bar it allows you to switch JPanels (For this example welcome is a different public class inside of a package.) 2. Now you can build as many public classes as you want and still be able to go to that JPanel.
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
public class ArmyQuestions {
CardLayout cards;
JPanel cardPanel;
public static void main(String[] args) throws IOException {
//Use the event dispatch thread for Swing components
EventQueue.invokeLater(new Runnable()
{
#Override
public void run()
{
new ArmyQuestions();
}
});
}
public ArmyQuestions()
{
JFrame mainFrame = new JFrame();
//make sure the program exits when the frame closes
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainFrame.setTitle("Army Questions");
mainFrame.setSize(797,510);
//This will center the JFrame in the middle of the screen
mainFrame.setLocationRelativeTo(null);
mainFrame.getContentPane().setLayout(new BorderLayout());
//Adds a menu bar
JMenuBar menuBar = new JMenuBar();
mainFrame.getContentPane().add(menuBar, BorderLayout.NORTH);
//Adds a menu option
JMenu mnFile = new JMenu("File");
menuBar.add(mnFile);
//Adds an item to the menu option
JMenuItem mntmNew = new JMenuItem("New");
mnFile.add(mntmNew);
mntmNew.addActionListener(new ActionListener()
{
#Override
public void actionPerformed(ActionEvent arg0)
{
cards.show(cardPanel, "Welcome");
}
});
//Adds cardpanel to getContentPane
cards = new CardLayout();
cardPanel = new JPanel();
cardPanel.setLayout(cards);
mainFrame.getContentPane().add(cardPanel,BorderLayout.CENTER);
//Adds a JPanel to your cardpanel
Welcome welcome = new Welcome();
cardPanel.add(welcome, "Welcome");
mainFrame.setVisible(true);
}
}
Two things I see going on.
You have SuggestedQuesion_2 declared globally then you create a whole new one in you method. JPanel SuggestedQuestion_2 = new JPanel();
I see a CardLayout for your Welcome - Welcome.setLayout(new CardLayout(0, 0));, but not for you SuggestedQuestion_2. Yet you're trying to access SuggestedQuestions's CardLayout
You should learn how to post an SSCCE So it is easier for us to see the problem. Also, in trying to recreate the problem into a smaller, runnable version you sometimes figure out the solution yourself.
And please follow Java naming convention using lowercase letters first letter of reference variable
My app/JFrame, using Borderlayout, has a toolbar at the top or north, a statusbar at the bottom or south and a JPanel.JTabbedPane.JScrollPane.JTable in the center. The JPanel is always a fixed size which is roughly adjustable using the various set*Size() methods applied in various combinations to the various components. But it's always a fixed size and always has east and west gaps. The north and south components stay fixed height and resize horizontally as one would expect.
Surely this is not a new or unique design.
Is this normal behaviour?
Is there some trick I've missed?
This is characteristic of retaining the default FlowLayout of JPanel and adding the panel to the center of a BorderLayout. The example below compares panels having FlowLayout, the default, or GridLayout. For contrast, the two are added to a GridLayout, which allows expansion in a manner similar to that of BorderLayout center.
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
/** #see http://stackoverflow.com/questions/5822810 */
public class LayoutPanel extends JPanel {
public LayoutPanel(boolean useGrid) {
if (useGrid) {
this.setLayout(new GridLayout());
} // else default FlowLayout
this.add(new JTree());
}
private static void display() {
JFrame f = new JFrame("LayoutPanels");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new GridLayout(1, 0));
f.add(new LayoutPanel(false));
f.add(new LayoutPanel(true));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
display();
}
});
}
}