0

This program is an experiment to make the jPanel move position and size then back again using swiping (touch screen).

Here is a picture of a frame: before anything, after minimizing and then after maximizing. The first and third one should be identical but are not.

enter image description here

With the code that I am using I can see no reason for the change in text location, and it is only text nothing other.

Edit: just found out the if i minimize the actual program and maximize it the panel is corrected so it could be a drawing problem (so i tried repaint()) and it didn't make a difference. any idea's what is causing this?

My working code example:

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JPanel;

public class TestExample extends javax.swing.JFrame {

    public TestExample() {
        initComponents();
    }
    @SuppressWarnings("unchecked")                       
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel3.setText("A Text Line");
        jPanel1.add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 0, -1, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 20, 90, 50));

        jButton1.setText("Test");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 20, -1, -1));

        pack();
    }                    

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {                                      
   JPanel p = jPanel1;
    int div = 5;
    if(small == true) {//SWIPE LEFT
        small = false;
        p.setSize(dim);
            for(Component c : p.getComponents()) {//maximize
                c.setSize(dim);
                c.setLocation(location.get(c));
            }
        p.setLocation(ploc);
    } else if( small == false) {//SWIPE RIGHT
        small = true; 
        ploc = p.getLocation();
        dim = p.getSize();
        for(Component c : p.getComponents()) {
            location.put(c, c.getLocation());
        }
        for(int i = p.getWidth()/div;i<p.getWidth();i++) {//minimize X coordinate
            Dimension d = new Dimension(p.getWidth()-i, p.getHeight());
            p.setSize(d);
            for(Component c : p.getComponents()) {
                c.setSize(d);
            }
        }
        for(int i = p.getHeight()/div;i<p.getHeight();i++) {//minimize Y coordinate
            Dimension d = new Dimension(p.getWidth(), p.getHeight()-i);
            p.setSize(d);
            for(Component c : p.getComponents()) {
                c.setSize(d);

            }
        }

        for(int i = p.getX(); i < Toolkit.getDefaultToolkit().getScreenSize().getWidth(); i++)p.setLocation(i-p.getWidth(), p.getY());//move to left hand side
    }

    }                                     
int previousX,previousY;
int xMouse,yMouse;
boolean small = false;
Dimension dim = null;
Map<Component,Point> location = new HashMap<>();
Point ploc = null;
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestExample().setVisible(true);
            }
        });
    }                
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;               
}
Greg
  • 754
  • 9
  • 18
  • Write a layout manager and define the constraints for the component – MadProgrammer Feb 19 '14 at 23:26
  • For better help sooner, post a [MCTaRE](http://stackoverflow.com/help/mcve) (Minimal Complete Tested and Readable Example). – Andrew Thompson Feb 19 '14 at 23:40
  • @MadProgrammer Because It is made using netbean's gui builder the layout manager is set as absolute layout. Once I have the constraints from the original how do I then reuse them to change the panel back to its original state? – Greg Feb 20 '14 at 00:13
  • 1
    You can change the layout manager used by NetBeans, I recommend making your own to achieve what it is you are trying to do, otherwise, every time the container is invalidated and validated, everything you've done will be discard by the currently installed layout manager – MadProgrammer Feb 20 '14 at 00:18

0 Answers0