java Swing best way to place panels within frame - java

i have 3 panels, panel1 has 3 components(ADD, Delete, Edit) JButtons, panel2(scrollPane) has jtable and panel3 again as (5 JTextFields, 5JLabels). when I add all panels into frame panel3 components shows paritialy only means (4JTextFields) only visible within frame. for panels I used BorderLayout for frame.add(panel2,BorderLayout.SOUTH) , frame.add(panel3,BorderLayout.CENTER); panel1, panel2 shows all of components, but panel3 shows partially only. could you please suggest me precisely what layout should I use, for panels, and how to setsize for my frame to accomodate all panels(components)? clearly?
`
frame.add(topPanel, BorderLayout.NORTH);
frame.add(updatePanel, BorderLayout.SOUTH);
frame.add(middlePanel, BorderLayout.CENTER);
frame.setVisible(true);
frame.setSize(850, 500);
`

Try using Window#pack
Causes this Window to be sized to fit the preferred size and layouts
of its subcomponents. The resulting width and height of the window are
automatically enlarged if either of dimensions is less than the
minimum size as specified by the previous call to the setMinimumSize
method.
If the window and/or its owner are not displayable yet, both
of them are made displayable before calculating the preferred size.
The Window is validated after its size is being calculated.
Updated with example
Without a SSCCE (working example), it's impossible to fully diagnose your problem. Let me demonstrate...
So, based on your description, I can create this without any issues...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class TestPack {
public static void main(String[] args) {
new TestPack();
}
public TestPack() {
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 TopPanel(), BorderLayout.NORTH);
frame.add(new MiddlePanel(), BorderLayout.CENTER);
frame.add(new UpdatePanel(), BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TopPanel extends JPanel {
public TopPanel() {
setBackground(Color.red);
add(new JLabel("I'm on top"));
}
}
public class MiddlePanel extends JPanel {
public MiddlePanel() {
setLayout(new BorderLayout());
add(new JScrollPane(new JTable(new DefaultTableModel(new Object[]{"A", "B", "C"}, 5))));
}
}
public class UpdatePanel extends JPanel {
public UpdatePanel() {
for (int index = 0; index < 5; index++) {
add(new JLabel(Integer.toString(index)));
add(new JTextField(5));
}
add(new JButton("Button"));
}
}
}
But obviously, you're doing something different. Take the time to update your question with a working example, otherwise there's nothing more we can possible do

Related

java object resize on window resize

I've to resize few elements like JTable on window resize. I've been trying this code, but it doesn't work correctly:
table.setLocation(0, 23);
Dimension siz = contentPane.getMaximumSize();
table.setSize(siz.height, siz.width - 46);
It resizing my table, but it making it endless, what i don't want. Also I would like to connent scrollbar to this table, and if it's possible - set column width in precentage
Your main problem (with resizing) has more to do with your reliance on form editors then anything to do with Swing or Java
Have a look at Laying Out Components Within a Container for more details.
You're also don't seem to be utilising a JScrollPane to house the JTable in. Have a look at How to Use Tables and How to Use Scroll Panes for more details
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.table.DefaultTableModel;
public class ResizeTest {
public static void main(String[] args) {
new ResizeTest();
}
public ResizeTest() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTable table;
private JButton historyButton;
private JButton otherButton;
public TestPane() {
table = new JTable(new DefaultTableModel(10, 10));
historyButton = new JButton("History");
otherButton = new JButton("Other");
setLayout(new BorderLayout());
JPanel buttons = new JPanel(new FlowLayout(FlowLayout.LEFT));
buttons.add(historyButton);
buttons.add(otherButton);
add(buttons, BorderLayout.NORTH);
add(new JScrollPane(table));
JPanel footers = new JPanel(new GridLayout(1, 2));
JLabel left = new JLabel("Left");
left.setHorizontalAlignment(JLabel.LEFT);
JLabel right = new JLabel("Right");
right.setHorizontalAlignment(JLabel.LEFT);
footers.add(left);
footers.add(right);
add(footers, BorderLayout.SOUTH);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
}
}

I want to use a BoxLayout in a ScrollPane that has an ArrayList of Panels

When I set the outpanel into a BoxLayout then the panel disappears. However the scrollbar shows that indicates my panel in ArrayList are in the right position.
I am totally new to Java so I'll appreciate any comments.
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.*;
import javax.swing.*;
public class gui extends JFrame{
int ctr=0, top=5;
public List<JPanel> o_panels = new ArrayList<JPanel>(); //Your List
public gui(){
super("MCC");
setLayout(null);
//Output panel for the results
JPanel outpanel = new JPanel();
outpanel.setBackground(Color.blue);
outpanel.setVisible(true);
outpanel.setLayout(new BoxLayout(outpanel, BoxLayout.PAGE_AXIS));
//Scroll pane
JScrollPane scrollPane = new JScrollPane(outpanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(0,0,780,400);
add(scrollPane);
//result panel
//creating and adding panels in to the array list
while(ctr<=4){
JPanel label1 = new JPanel();
label1.setPreferredSize(new Dimension(600,100));
o_panels.add(label1);
outpanel.add(o_panels.get(ctr));
ctr++;
}
}
public void runGui(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(800,600);
this.setVisible(true);
this.setResizable(false);
//i call this on the other class
}
}
There is, not much, wrong with your code, the problem is, you've not established any means by which you can see what you've been adding
Have a look at this...
while (ctr <= 4) {
JPanel label1 = new JPanel();
label1.setPreferredSize(new Dimension(600, 100));
o_panels.add(label1);
outpanel.add(o_panels.get(ctr));
ctr++;
}
All the panels are the same color and you've added nothing to them, so how could you possible know if they were been added or layout correctly...
I simple added label1.setBorder(new LineBorder(Color.RED)); and got this result...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
public class Test extends JFrame {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
Test test = new Test();
test.runGui();
}
});
}
int ctr = 0, top = 5;
public List<JPanel> o_panels = new ArrayList<JPanel>(); //Your List
public Test() {
super("MCC");
//Output panel for the results
JPanel outpanel = new JPanel();
outpanel.setBackground(Color.blue);
outpanel.setVisible(true);
outpanel.setLayout(new BoxLayout(outpanel, BoxLayout.PAGE_AXIS));
//Scroll pane
JScrollPane scrollPane = new JScrollPane(outpanel,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
scrollPane.setBounds(0, 0, 780, 400);
add(scrollPane);
//result panel
//creating and adding panels in to the array list
while (ctr <= 4) {
JPanel label1 = new JPanel();
label1.setPreferredSize(new Dimension(600, 100));
label1.setBorder(new LineBorder(Color.RED));
o_panels.add(label1);
outpanel.add(o_panels.get(ctr));
ctr++;
}
}
public void runGui() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
this.setVisible(true);
// this.setResizable(false);
setLocationRelativeTo(null);
}
}
Also have a look at Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?
And you really should avoid using null layouts, pixel perfect layouts are an illusion within modern ui design. There are too many factors which affect the individual size of components, none of which you can control. Swing was designed to work with layout managers at the core, discarding these will lead to no end of issues and problems that you will spend more and more time trying to rectify

Buttons don't show when keybinding buttons

I am trying to make buttons change color when pressed. I have created the following code:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;
public class KeyboardTest extends JFrame {
public static void main(String[] args) {
new KeyboardTest();
}
public KeyboardTest() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
} catch (Exception ex) {
try {
UIManager.setLookAndFeel(UIManager
.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
e.printStackTrace();
}
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
frame.setBounds(100, 100, 650, 390);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
JPanel contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
add(contentPane);
contentPane.setLayout(null);
JPanel keyboardPanel = new JPanel();
keyboardPanel.setBounds(10, 122, 614, 181);
contentPane.add(keyboardPanel);
keyboardPanel.setLayout(new GridLayout(5, 0, 0, 0));
... adding panels and buttons...
addKeyBinding(A, "A", KeyEvent.VK_A);
addKeyBinding(B, "B", KeyEvent.VK_B);
addKeyBinding(C, "C", KeyEvent.VK_C);
addKeyBinding(D, "D", KeyEvent.VK_D);
addKeyBinding(E, "E", KeyEvent.VK_E);
}
protected void addKeyBinding(JButton btn, String name, int virtualKey) {
ActionMap am = getActionMap();
InputMap im = getInputMap(WHEN_IN_FOCUSED_WINDOW);
im.put(KeyStroke.getKeyStroke(virtualKey, 0, false), name
+ ".pressed");
im.put(KeyStroke.getKeyStroke(virtualKey, 0, true), name
+ ".released");
am.put(name + ".pressed", new KeyAction(btn, true));
am.put(name + ".released", new KeyAction(btn, false));
}
}
public class KeyAction extends AbstractAction {
private JButton btn;
private boolean highlight;
public KeyAction(JButton btn, boolean highlight) {
this.btn = btn;
this.highlight = highlight;
}
#Override
public void actionPerformed(ActionEvent e) {
if (highlight) {
btn.getModel().setPressed(true);
btn.setBackground(Color.RED);
btn.setOpaque(true);
} else {
btn.getModel().setPressed(false);
btn.setBackground(null);
btn.setOpaque(false);
}
}
}
}
Here is what it's supposed to look like:
Here is what actually appears:
Should I attach the full code? (it's pretty long...)
Can't test your code (because of your missing components). And I won't get on you about the null layout. But here's a problem and a solution, based on what I was able to test (using my own components).
I see a useless JPanel contentPane. The class you're using is already a panel, I don't see the point to create another one, and then wrap it in another one. (Not the whole problem though)
The class TestPane has a default FlowLayout, which means that it will respect the preferred size of the components inside, which is the contentPane panel. The problem with that is that it will have no preferred size. You are set the button sizes by using setBounds. This is not the same as preferredSize. So the there is no preferred size for the TestPane panel to calculate.
Simple fix is to set the layout of the TestPane to BorderLayout. This will not take preferred size into account, and will stretch the contentPane so you can see it. Problem with this though is that you need to set the size of the frame. pack() won't work, because the whole preferred size deal. That's why you frame blinks when you first open it, because it's first shrunk, and then you expand it with setBounds.
Again like I said though in my comment, I'd stay away from the null layouts (for many reasons). You can see a good example of a keyboard using GridBagLayout here as posted in my comment, from the mad man himself #MadProgrammer

GUI, BoxLayout adding a panel

Studying BoxLayout and GUI in general. I want to place a panel on a frame. Later I'll add an identical panel and will test BoxLaoyout. But I can't understand why this code produces not a panel sized 200x400 but just a red point in the middle of the left side of the frame (with coordinates about (300,0)).
public class View extends JFrame {
public View(){
this.setPreferredSize(new Dimension(600, 600));
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.pack();
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
Border border = BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.RED, Color.BLACK);
JPanel p1 = new JPanel();
p1.setSize(200, 400);
p1.setBorder(border);
p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
panel.add(p1);
this.add(panel);
this.setVisible(true);
}
}
The layout manager (BoxLayout) is using the preferred size of the components of the container it is managing. By default, the preferred size of a empty JPanel is 0x0, adding in the border has produced a preferred size closer to 2x2
When using layout managers, calling setSize is meaningless, as the layout manager will override anything you specify when the container is revalidate
Updated
It would appear that the combination of both BoxLayouts seems to be playing against you. If I remove the second BoxLayout from p1, it seems to work okay.
Also, BoxLayout seems to want to work with the maximum size of the component...
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
public class View extends JFrame {
public View() {
this.setPreferredSize(new Dimension(600, 600));
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.pack();
JPanel panel = new JPanel();
panel.setBorder(new LineBorder(Color.BLUE));
panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
Border border = BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.RED, Color.BLACK);
JPanel p1 = new JPanel() {
public Dimension getMaximumSize() {
return new Dimension(200, 400);
}
};
p1.setBorder(border);
p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));
panel.add(p1);
this.add(panel);
this.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new View();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}

Border with equal sizes on all 4 sides of JTextpane

I am setting a createLineborder to my JTextPane. But the border lines are little different in right-end and bottom of JTextpane when compared to Left-end and top of TextPane. I searched in net and I found that it is the default behavior of lineborder. So can anyone please tell me that is there any border which gives equal lines sizes in all 4 sides?
Hi I cannot put my code as it is very huge. So just putting a sample code here.
JPanel panel;
JTextPane pane;
public BorderedTextPane() {
// TODO Auto-generated constructor stub
pane = new JTextPane();
panel = new JPanel(null);
JPanel innerPanel = new JPanel(null);
innerPanel.setBounds(50,50,300,400);
pane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
pane.setSize(new Dimension(innerPanel.getWidth(),innerPanel.getHeight()));
innerPanel.add(pane);
panel.add(innerPanel);
add(panel);
setVisible(true);
setSize(new Dimension(500,500));
setLocationRelativeTo(null);
}
This could be clearly seen if you zoom the textpane or when you save it on a file. Below image would explain it in better way. Compare the left and right lines.
I would say it has more to do with using null layouts...
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
public class TestBorder {
public static void main(String[] args) {
new TestBorder();
}
public TestBorder() {
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 TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new BorderLayout());
setBorder(new EmptyBorder(10, 10, 10, 10));
JTextPane pane = new JTextPane();
JPanel panel = new JPanel(new BorderLayout());
JPanel innerPanel = new JPanel(new BorderLayout());
pane.setBorder(BorderFactory.createLineBorder(Color.BLACK));
innerPanel.add(pane);
panel.add(innerPanel);
add(panel);
}
}
}
From the JComponent#setBorder() API:
Although technically you can set the border on any object that inherits from JComponent, the look and feel implementation of many standard Swing components doesn't work well with user-set borders. In general, when you want to set a border on a standard Swing component other than JPanel or JLabel, we recommend that you put the component in a JPanel and set the border on the JPanel.
Consult API setBorder method (new EmptyBorder ());
As well as using the variable JTextPane varible.setBorder (BorderFactory.createLineBorder (Color.xxx);

Categories