Java AWT Component and Panel Padding/Border - java

I have no idea why I can't find a solution for this... I am trying to layout some AWT components in a flow layout. The only problem is the 'padding' between the components (Panels) when using a flow layout. This is what the applet currently looks like: http://i.stack.imgur.com/2KZgD.png
I need a way to set the Applet/Panels so that the two panels (black boxes) are touching (no 'padding'). The entire program is Swing free, all AWT, and I plan on keeping it that way. I feel this is a very simple solution, but I have not been able to find an answer.
This is the init() code from the applet class:
public void init() {
setLayout(new FlowLayout());
c1 = new TestPanel();
c2 = new TestPanel();
c1.setPreferredSize(new Dimension(640, 480));
c2.setPreferredSize(new Dimension(100, 480));
add(c1);
add(c2);
}
This is the TestPanel class I'm using:
public class TestPanel extends Panel {
public void paint(Graphics g) {
g.setColor(Color.BLACK);
g.fillRect(0, 0, this.getPreferredSize().width, this.getPreferredSize().height);
}
}

The default horizontal (and vertical) gap of FlowLayout is set to 5. Therefore, you must explicitly set the horizontal gap to 0.
FIRST APPROACH
Invoke setHgap(...) on the component's layout. Since the default layout of a JPanel is FlowLayout, simply do the following:
((FlowLayout)getLayout()).setHgap(0);
SECOND APPROACH
Use another FlowLayout constructor. That is, FlowLayout(int align, int hgap, int vgap). And simply do the following:
setLayout(new FlowLayout(FlowLayout.CENTER, 0, 5));

Related

How to refresh a JPanel that is nested inside another Jpanel?

I have nested a Jpanel within another JPanel. I want to refresh one of JPanels while not refreshing the other one. I have the following code, I can use the repaint() function however it will update all of the JPanels not just the one that I want (the Time JPanel).
How would I go about refreshing just the Time JPanel? Leaving the Weather JPanel untouched?
I would like to be able to do this from an external thread.
public class MainPanel extends JPanel{
public static JPanel TimePanel = new Time();
public static Weather WeatherPanel = new Weather();
public void paintComponent(Graphics g){
super.paintComponent(g);
this.setBackground(Color.BLACK);
this.setLayout(new FlowLayout(FlowLayout.LEADING, 0, 0));
TimePanel.setLocation(0, 0);
TimePanel.setSize(new Dimension(500, 300));
this.add(TimePanel);
WeatherPanel.setLocation(0,300);
WeatherPanel.setSize(new Dimension(100, 100));
this.add(WeatherPanel);
//repaint();//Just causes recursion
}
}
Your code is completely wrong. The paintComponent() method is used for painting with the Graphics object. You should NEVER add components to a panel or change the size, or change the location of a component in a painting method.
There is no need for you to override the paintComponent() method.
In the constructor of your class is where you create the child panels and add them to the main panel. Something like:
public class MainPanel extends JPanel
{
public JPanel timePanel = new Time();
public Weather teatherPanel = new Weather();
public MainPanel()
{
this.setBackground(Color.BLACK);
this.setLayout(new FlowLayout(FlowLayout.LEADING, 0, 0));
this.add(timePanel);
this.add(weatherPanel);
}
}
Notice how I also changed your variables:
you should not be using static
variable names start with a lower case character.
I suggest you start by reading the Swing Tutorial for Swing basics. You can check out the section on How To Use Panels for a working example.

Having trouble adding graphics to JPanel

I have looked online, but I am still having trouble understanding how to add graphics to a JPanel
Here is the code for my panel class:
public class GamePanel extends JPanel {
public GamePanel(){
}
public void paintComponent(Graphics g) {
g.drawString("asd", 5, 5);
}
}
And my main method:
public static void main(String[] args) {
frame.setLayout(new FlowLayout());
frame.getContentPane().setBackground(Color.WHITE);
//i is an instance of GamePanel
frame.add(i);
frame.setPreferredSize(new Dimension(500, 500));
frame.pack();
frame.setVisible(true);
}
Text will only appear in a very tiny section of the screen (this applies to any graphics object I try to draw). What am I doing wrong?
FlowLayout respects preferred sizes of components. Therefore override getPreferredSize to give your JPanel a visible size rather than the default zero size Dimension that the panel currently has after JFrame#pack is called:
class GamePanel extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g); // added to paint child components
g.drawString("asd", 5, 20);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(400, 300);
}
}
Edit:
To eliminate gap between JPanel and its containing JFrame, set the vertical gap to 0:
frame.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
Two things jump out
Your Game panel has no preferred size, which, by default, makes 0x0. FlowLayout will use this information to make decisions about how best to layout your panel. Because the size is 0x0, the repaint manager will ignore it. Try overriding the getPreferredSize method and return a appropriate size or use a layout manager that does not use the preferred size, like BorderLayout
Your paintComponent method MUST call super.paintComponet

Simple GUI Java layout issue

I'm trying to do the following and I'm not sure what type of Java layout to use.
I want a JFrame with a single panel totalPanel. totalPanel should contain two panels which are custom classes I write, PanelA and PanelB. PanelA starts out with height 200 and PanelB starts out with height 400. When the user expands the window or resizes the window, only PanelB should increase in height, but both panels can increase in width.
How can I set this up? I've tried to use BorderLayout but then "North" is always too small in height. I've tried to use BoxLayout but then both PanelA and PanelB are always the same height.
Consider having totalPanel use a BorderLayout. Add the PanelA to the BorderLayout.PAGE_START position and the PanelB to the BorderLayout.CENTER position.
For more on the layout managers, please review the tutorial: Lesson: Laying Out Components Within a Container
Edit: I see that you've used BorderLayout, that it "doesn't work" but you don't show code. For more fine tuning on why it doesn't work, consider showing us code.
To get it to work consider giving your PanelX classes getPreferredSize() overrides that would help set the initial sizes of the JPanels.
For example:
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.*;
public class SimpleGuiLayout {
private static void createAndShowGui() {
JPanel totalPanel = new JPanel(new BorderLayout());
totalPanel.add(new PanelX(800, 200, "Panel A"), BorderLayout.PAGE_START);
totalPanel.add(new PanelX(800, 400, "Panel B"), BorderLayout.CENTER);
JFrame frame = new JFrame("Simple Gui Layout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(totalPanel);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class PanelX extends JPanel {
private int prefW;
private int prefH;
public PanelX(int prefW, int prefH, String title) {
this.prefW = prefW;
this.prefH = prefH;
setBorder(BorderFactory.createTitledBorder(title));
}
#Override
public Dimension getPreferredSize() {
return new Dimension(prefW, prefH);
}
}
If run, it would look like so:
And would resize appropriately.
One approach (probably not the simplest) would be a GridBagLayout.
Use the weightx and weighty constraints to control which members can resize.
I recommend a GridBagLayout. It is complex to use, but it can solve your problem. Assuming panelA is on top of panelB, you would have the following constraints:
panelA and panelB's gridx would be 0
panelA would be ad gridy 0, and panelB at gridy 1
for both, you would set fill = BOTH
but to have only panelB increase in height, you would set panelB's weighty to 1.0, and panelA's to 0
You originally solution using BorderLayout will work already, you just need to set preferred height of panelA:
panelA.setPreferredSize(new Dimension(0, 200));
The width doesn't matter because BorderLayout will ignore preferred width of component added to NORTH or SOUTH.

JViewport won't generate a viewport for JPanel's derived class

I believe JViewport does work with JPanel, but when I build a new class that extends JPanel, it seem as if the JViewport is ignore by the program. I don't know if I do anything wrong, so this is the test I conduct and still get the same result:
public class panel extends JPanel
{
public panel()
{
super();
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(Color.BLUE);
g.drawString("Hello World", 50, 50);
g.setColor(Color.RED);
g.fillRect(50,50,100,100);
g.setColor(Color.BLACK);
g.fillOval(100, 100, 50, 50);
}
}
public class test extends JFrame
{
private panel p;
public void init()
{
this.setSize(1000, 1000);
this.setLayout(new BorderLayout());
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
p = new panel();
p.setOpaque(false);
JViewport v = new JViewport();
v.setViewSize(new Dimension(200,200));
v.setViewPosition(new Point(2000,2000));
v.setView(p);
this.add(v,BorderLayout.CENTER);
}
public test()
{
init();
}
public static void main(String[] args)
{
test t = new test();
}
}
It suppose to show part of the painted JPanel, but the JFrame window just display the whole JPanel. Therefore, I don't know if I did any wrong or JViewport is not built for this purpose. If it is the latter, then it would be great if anyone can suggest a workaround solution.
Thanks
The BorderLayout you're using is causing the viewport, which is placed in the center, to take the entire space inside the frame, since there are no other components in that layout. That's how the BorderLayout works.
Thus the viewport is also given a bigger size than defined (the size is overwritten by the layout manager). Since the panel doesn't have a fixed size either, it will also be resized.
In order to change that, either use a different layout manager or set a minimum/maximum size for the viewport and override getPreferredSize() for the panel.
As a side note: don't use lower case class names like panel.

Overlay panel (above another)

I am learning how to use Swing and found myself quite difficult task.
What I am trying to accomplish: I want to have panel (call it menu panel) on the left side (let's say 100px width) and the second panel (call it content panel), which takes the rest of available place.
In menu panel there are 3 buttons. When I press on of them, to the right side of menu panel (over content panel) second menu panel (submenu) should appear (and it should start in the middle of button which was pressed).
It may be hard to understand, so I've created simple draft:
I tried JLayeredPane but there were problems with resizing window (elements in Layered Pane didn't resize).
JLayeredPane miss implementations for LayoutManager, you have to setPreferredSize or setBounds manually for sizing/place JComponents,
there is one possible workaround you can add ComponentListener to the JFrame, then on componentResized(ComponentEvent e) you can resize/replace JComponent(s) to the desired Bounds
for example
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class LayeredPaneWithOverlap {
private JTextArea textArea = new JTextArea(2, 10);
private JPanel textPanel = new JPanel(new BorderLayout());
private JTable table = new JTable(30, 5);
private JScrollPane scroll = new JScrollPane(table);
private JLayeredPane layer = new JLayeredPane();
private JFrame frame = new JFrame("Frame with resiziable JLayeredPane");
public void makeUI() {
textArea.setBorder(new LineBorder(Color.DARK_GRAY));
textArea.setText("Frame with resiziable JLayeredPane");
textPanel.setOpaque(false);
textPanel.add(textArea, BorderLayout.NORTH);
Font font = textArea.getFont();
FontMetrics fontMetrics = textArea.getFontMetrics(font);
int h = fontMetrics.getHeight() + frame.getInsets().top +
textPanel.getInsets().top + textArea.getInsets().top
+ textArea.getInsets().bottom;
scroll.setBounds(0, h, 400, 300);
layer.add(textPanel, new Integer(2));
layer.add(scroll, new Integer(1));
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 400);
frame.addComponentListener(new ComponentAdapter() {
#Override
public void componentResized(ComponentEvent e) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
resizeAll();
}
});
}
});
frame.setLocationRelativeTo(null);
frame.add(layer);
resizeAll();
frame.setVisible(true);
}
void resizeAll() {
Insets insets = frame.getInsets();
int w = frame.getWidth() - insets.left - insets.right;
int h = frame.getHeight() - insets.top - insets.bottom;
textPanel.setSize(w, h);
scroll.setSize(w, h - scroll.getY());
layer.revalidate();
layer.repaint();
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new LayeredPaneWithOverlap().makeUI();
}
});
}
}
You can set a layoutmanager for the layered pane, javax.swing.OverlayLayout uses the full available space and allows resizing.
JLayeredPane layer = new JLayeredPane();
layer.setLayout(new OverlayLayout(layer));
You probably don't want the submenu to occupy the fullspace. To avoid it you can override its get…size-methods. Or you can add a second LayeredPane (for it's transperancy and it's layoutmanager), set a normal BoxLayout and use a spacer.
JPanel normalContents = new JPanel();
layer.add(normalContents, JLayeredPane.DEFAULT_LAYER);
JLayeredPane subMenuAuxiliaryLayer = new JLayeredPane()
subMenuAuxiliaryLayer.setLayout(new BoxLayout(subMenuAuxiliaryLayer, BoxLayout.LINE_AXIS));
layer.add(subMenuAuxiliaryLayer, JLayeredPane.PALETTE_LAYER);
JPanel submenuContents = new JPanel();
subMenuAuliliaryLayer.add(submenuContents);
subMenuAuxiliaryLayer.add(Box.createHorizontalGlue());
contentPanel.setLayout(null); // Absolute positioning of children.
#Override
public void actionPerformed(ActionEvent evt) {
final JButton btn = (JButton) evt.getSource();
final int buttonY = btn.getY(); // Must be final for usage in new Runnable object.
SwingUtilities.invokeLater(new Runnable() { // Return fast from event handling.
#Override
public void run() {
JPanel child = new JPanel();
child.setBackground(Color.RED); // So we'll see it.
child.setBounds(0, buttonY, 100, 300);
contentPanel.removeAll(); // Clear content panel of prior additions.
contentPanel.add(child); // Add a new panel.
contentPanel.repaint(10L);
}
});
}
The JLayeredPane works by defualt with no Layout manager, which means that you are using absolute positioning and no resizing. You could add a resize listener and adjust positions and size of inner components from code, as you see fit.
If you don't want to do this from code, you will need a layout manager, nothing fancy, just something to fill the container as it resizes. But here's the thing... if you add a layout manager, it will layout the components as if they are all in one layer, but most layout managers don't overlap their children so they are useless. The only one you could use is the OverlayLayout - it can also resize children. But using an OverlayLayout with JLayeredPane is overkill. You can just use OverlayLayout with a JPanel. So, yes, JLayeredPane is kind of useless. I recommend using a JPanel with an OverlayLayout instead.
Here is how to set things up so that you can have great control over almost any overlapping UI scenario out there: Using a JPanel with an OverlayLayout, have a separate transparent JPanel for each layer. In this way you can combine various LayoutManagers on different layers, by setting a diferent layout manager for each pane, including absolute positioning if necessary. Then add your visible components inside the panels representing the layers. Don't add them directly to the OverlayLayout panel. Just make sure that all of the JPanels you are using as layers have setAlignmentX and Y to center (0.5f) so that they fill the entire OverlayLayout panel as it resizes.

Categories

Resources