4

I need some help with the code below. What I'm trying to do is to expand the size of TextArea (named preview) to include the last three buttons: algo1, algo2 and algo3.

I have tried many times to change the code but it still only shows one button, which is algo1, and not all three buttons. Has it got something to do with the BASELINE or LEADING? Can someone please show where I have gone wrong? Thanks.

import java.awt.*; 
import javax.swing.*; 

// Create a simple GUI window
public class win {

private static void createWindow() {

   //Create and set up the window. 
   JFrame frame = new JFrame("PDF Denoiser");
   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

   //My edit
   JPanel panel = new JPanel();
   GroupLayout layout = new GroupLayout(panel);
   panel.setLayout(layout);

   layout.setAutoCreateGaps(true);
   layout.setAutoCreateContainerGaps(true);

   JLabel label1 = new JLabel("Image File");
   JLabel label2 = new JLabel("Destination");
   JLabel label3 = new JLabel("Preview");

   JTextField current = new JTextField();
   JTextField dest = new JTextField();
   JTextArea preview = new JTextArea();

   JButton choose1 = new JButton("Search1");
   JButton choose2 = new JButton("Search2");
   JButton algo1 = new JButton("MDWM");
   JButton algo2 = new JButton("BFMR");
   JButton algo3 = new JButton("Mine");

   //Horizontal arrangement
   layout.setHorizontalGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(label1)
            .addComponent(label2)
            .addComponent(label3))
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(current)
            .addComponent(dest)
            .addComponent(preview))
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(choose1)
            .addComponent(choose2)
            .addComponent(algo1)
            .addComponent(algo2)
            .addComponent(algo3))
    );

    layout.linkSize(SwingConstants.HORIZONTAL, choose1, choose2, algo1, algo2, algo3);

    //Vertical arrangement
    layout.setVerticalGroup(layout.createSequentialGroup()
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
            .addComponent(label1)
            .addComponent(current)
            .addComponent(choose1))
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
            .addComponent(label2)
            .addComponent(dest)
            .addComponent(choose2))
        .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addComponent(label3)
            .addComponent(preview)
                .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(algo1)
                    .addComponent(algo2)
                    .addComponent(algo3))))
    );






   //Display the window. 
   frame.setLocationRelativeTo(null); 
   frame.add(panel);
   frame.pack();
   frame.setVisible(true); 
}

public static void main(String[] args) {

   createWindow();

}

}

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
evarey
  • 75
  • 1
  • 6
  • +1 for including good example code – Duncan Jones Apr 08 '13 at 07:28
  • I think that those two JButtons are hidded behind one, two another, please did you tried to call (after code frame.setVisible(true);) System.out.println(xxx.getBounds()); wrapped into invokeLater, both group should be corresponding ..., – mKorbel Apr 08 '13 at 07:47
  • @mKorbel, I'm quite new to Java. How do I do the invokeLater wrap? Any examples? Thanks. – evarey Apr 08 '13 at 08:07
  • please to edit question and to descibe how this group on JButtons could be layed, there aren't a users that knows and to use this Layout Manager, but basically there isn't an issue to create this contianer by GridBagLayout, MigLayout or (most easiest and I'll be preffer) by using two JPanels with /by using different Layout Manager for each of the JPanels – mKorbel Apr 08 '13 at 08:17
  • Did you try my examples ? I hope one them is what you want – hudi Apr 08 '13 at 08:42

2 Answers2

1

I'm quite new to Java. How do I do the invokeLater wrap? Any examples? Thanks

run:
java.awt.Rectangle[x=208,y=12,width=82,height=26]
java.awt.Rectangle[x=208,y=12,width=82,height=26]
java.awt.Rectangle[x=208,y=76,width=82,height=26]
java.awt.Rectangle[x=208,y=76,width=82,height=26]
java.awt.Rectangle[x=208,y=76,width=82,height=26]
  • see that all three JButtons have get the same coordinates on the screen

meaning

System.out.println(algo1.getBounds());
System.out.println(algo2.getBounds());
System.out.println(algo3.getBounds());

returns

java.awt.Rectangle[x=208,y=76,width=82,height=26]
java.awt.Rectangle[x=208,y=76,width=82,height=26]
java.awt.Rectangle[x=208,y=76,width=82,height=26]

enter image description here

import java.awt.*;
import javax.swing.*;

// Create a simple GUI window
public class Win {

    private static void createWindow() {
        //Create and set up the window. 
        JFrame frame = new JFrame("PDF Denoiser");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //My edit
        JPanel panel = new JPanel();
        GroupLayout layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        JLabel label1 = new JLabel("Image File");
        JLabel label2 = new JLabel("Destination");
        JLabel label3 = new JLabel("Preview");
        JTextField current = new JTextField(10);
        JTextField dest = new JTextField(10);
        JTextArea preview = new JTextArea(5, 10);
        final JButton choose1 = new JButton("Search1");
        //choose1.setPreferredSize(new Dimension(80,20));
        final JButton choose2 = new JButton("Search2");
        //choose2.setPreferredSize(new Dimension(80,20));
        final JButton algo1 = new JButton("MDWM");
        final JButton algo2 = new JButton("BFMR");
        final JButton algo3 = new JButton("Mine");
        //Horizontal arrangement
        layout.setHorizontalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(label1)
                .addComponent(label2)
                .addComponent(label3))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(current)
                .addComponent(dest)
                .addComponent(preview))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(choose1)
                .addComponent(choose2)
                .addComponent(algo1)
                .addComponent(algo2)
                .addComponent(algo3)));
        layout.linkSize(SwingConstants.HORIZONTAL, choose1, choose2, algo1, algo2, algo3);
        //Vertical arrangement
        layout.setVerticalGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(label1)
                .addComponent(current)
                .addComponent(choose1))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(label2)
                .addComponent(dest)
                .addComponent(choose2))
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(label3)
                .addComponent(preview)
                .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(algo1)
                .addComponent(algo2)
                .addComponent(algo3)))));
        //Display the window. 
        frame.setLocationRelativeTo(null);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                System.out.println(choose1.getBounds());
                System.out.println(choose1.getBounds());
                System.out.println(algo1.getBounds());
                System.out.println(algo2.getBounds());
                System.out.println(algo3.getBounds());
            }
        });
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                createWindow();
            }
        });
    }
}
  • end of story
mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • thanks for your help. I tried the code you provided, yet the result remains the same. Only one button is showing...Did I miss something? – evarey Apr 08 '13 at 08:25
  • this is only about getBounds wrapped into invokeLater, not an answer how (important are coordinates where) to display rest of JButtons – mKorbel Apr 08 '13 at 08:32
  • Ok. Thanks for giving me an insight of it. – evarey Apr 08 '13 at 08:53
0

I dont know where you want to add your button but please try this example. I updated your code, Now you can see your buttons: here are button: but1 but2 but3

package Core;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

// Create a simple GUI window
public class win {

    private static void createWindow() {

        // Create and set up the window.
        JFrame frame = new JFrame("PDF Denoiser");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // My edit
        JPanel panel = new JPanel();
        GroupLayout layout = new GroupLayout(panel);
        panel.setLayout(layout);

        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        JLabel label1 = new JLabel("Image File");
        JLabel label2 = new JLabel("Destination");
        JLabel label3 = new JLabel("Preview");

        JTextField current = new JTextField();
        JTextField dest = new JTextField();
        JTextArea preview = new JTextArea();

        JButton choose1 = new JButton("Search1");
        JButton choose2 = new JButton("Search2");
        JButton algo1 = new JButton("MDWM");
        JButton algo2 = new JButton("BFMR");
        JButton algo3 = new JButton("Mine");

        // Horizontal arrangement
        layout.setHorizontalGroup(layout
                .createSequentialGroup()
                .addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(label1)
                                .addComponent(label2).addComponent(label3))
                .addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(current)
                                .addComponent(dest).addComponent(preview))
                .addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                .addComponent(choose1)
                                .addComponent(choose2)
                                .addGroup(
                                        layout.createSequentialGroup().addComponent(algo1).addComponent(algo2)
                                                .addComponent(algo3))));

        layout.linkSize(SwingConstants.HORIZONTAL, choose1, choose2, algo1, algo2, algo3);

        // Vertical arrangement
        layout.setVerticalGroup(layout
                .createSequentialGroup()
                .addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label1)
                                .addComponent(current).addComponent(choose1))
                .addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label2)
                                .addComponent(dest).addComponent(choose2))
                .addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label3)
                                .addComponent(preview).addComponent(algo1).addComponent(algo2).addComponent(algo3)));

        // Display the window.
        frame.setLocationRelativeTo(null);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        createWindow();

    }
}

or try this: and here are:

butt1

butt2

butt3

package Core;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

// Create a simple GUI window
public class win {

    private static void createWindow() {

        // Create and set up the window.
        JFrame frame = new JFrame("PDF Denoiser");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // My edit
        JPanel panel = new JPanel();
        GroupLayout layout = new GroupLayout(panel);
        panel.setLayout(layout);

        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        JLabel label1 = new JLabel("Image File");
        JLabel label2 = new JLabel("Destination");
        JLabel label3 = new JLabel("Preview");

        JTextField current = new JTextField();
        JTextField dest = new JTextField();
        JTextArea preview = new JTextArea();

        JButton choose1 = new JButton("Search1");
        JButton choose2 = new JButton("Search2");
        JButton algo1 = new JButton("MDWM");
        JButton algo2 = new JButton("BFMR");
        JButton algo3 = new JButton("Mine");

        // Horizontal arrangement
        layout.setHorizontalGroup(layout
                .createSequentialGroup()
                .addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(label1)
                                .addComponent(label2).addComponent(label3))
                .addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(current)
                                .addComponent(dest).addComponent(preview))
                .addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(choose1)
                                .addComponent(choose2).addComponent(algo1).addComponent(algo2).addComponent(algo3)));

        layout.linkSize(SwingConstants.HORIZONTAL, choose1, choose2, algo1, algo2, algo3);

        // Vertical arrangement
        layout.setVerticalGroup(layout
                .createSequentialGroup()
                .addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label1)
                                .addComponent(current).addComponent(choose1))
                .addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(label2)
                                .addComponent(dest).addComponent(choose2))
                .addGroup(
                        layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                                .addComponent(label3)
                                .addComponent(preview)
                                .addGroup(
                                        layout.createSequentialGroup().addComponent(algo1).addComponent(algo2)
                                                .addComponent(algo3))));

        // Display the window.
        frame.setLocationRelativeTo(null);
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        createWindow();

    }
}
hudi
  • 15,555
  • 47
  • 142
  • 246
  • hudi, I tried your code. It is the second layout which I wanted (the parallel-positioned buttons). Your code works perfectly. Thanks a lot for the help ;) – evarey Apr 08 '13 at 08:55
  • I am trying to add scrollbar to the textarea and my code follows: `JScrollPane myScrollPane = new JScrollPane(preview,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED); frame.add(myScrollPane);` This isn't working – Rudra Jun 21 '17 at 06:05