I plan to make Java image viewer
I just started today and I had hard time with GridBagSizer
After long time of pain it turned to be fruitful. Here is what I have so far. I want the following, of which I have failed:
1. Make the components fill the whole window when resized
2. I want the tree to be list of directories than what it is now. In wxPython there is Directory control I dont know of Swing
3. How will I hold the list of directories and files to play with. In Python there are lists, tuples and dictionaries

Thanks for your time.

package imageviewer;

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

/**
 * Created by IntelliJ IDEA.
 * User: Hosanna Technologies
 * Date: Jan 30, 2010
 * Time: 12:56:54 AM
 */
public class MainClass {
    public static void main(String args[]){
        Viewer iviewer = new Viewer() ;
        JFrame frame = new JFrame("Hosanna Image Viewer"); //add our panel
        frame.getContentPane().add(iviewer, BorderLayout.WEST);
        iviewer.MakeMenu(frame);
        frame.setSize(500, 500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }
}


class Viewer extends JPanel{
    private GridBagConstraints c ;
    private JButton prev;
    private JButton slideshow;
    private JButton next;
    private JButton zoom;
    private JButton delete;
    private JButton rotateleft;
    private JButton rotateright;
    private JButton fill;
    private JTree imagedir;
    private JPanel viewer;

    public Viewer(){
        this.setLayout(new GridBagLayout());
        c = new GridBagConstraints();

        imagedir = new JTree();
        c.fill = c.VERTICAL;
        c.gridx = 0;
        c.gridy = 0;
        c.insets = new Insets(0, 10, 0, 0);
        c.gridwidth = 1;
        c.gridheight = 10;
        this.add(imagedir, c);
        //add actionlisterner
        viewer = new JPanel();
        //viewer.setSize(200, 200);
        c.fill = c.BOTH;
        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 3;
        c.gridheight = 10;
        c.insets = new Insets(5, 5, 5, 5);
        this.add(viewer, c);

        //add buttons
        c.insets = new Insets(10, 10, 10, 10);
        c.gridwidth = 1;
        c.gridheight = 1;
        prev = new JButton("Previous");
        c.gridx = 0;
        c.gridy = 11;
        this.add(prev, c);
        //add listener
        prev.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        viewer.setBackground(Color.GREEN);
                    }
                }
        );

        slideshow = new JButton("SlideShow");
        c.gridx = 1;
        c.gridy = 11;
        this.add(slideshow, c);

        next = new JButton("Next");
        c.gridx = 2;
        c.gridy = 11;
        this.add(next, c);

        zoom = new JButton("Zoom");
        c.gridx = 3;
        c.gridy = 11;
        this.add(zoom, c);

        delete = new JButton("Delete");
        c.gridx = 0;
        c.gridy = 12;
        this.add(delete, c);

        rotateleft = new JButton("RotateLeft");
        c.gridx = 1;
        c.gridy = 12;
        this.add(rotateleft, c);

        rotateright = new JButton("RotateRight");
        c.gridx = 2;
        c.gridy = 12;
        this.add(rotateright, c);

        fill = new JButton("FullScreen");
        c.gridx = 3;
        c.gridy = 12;
        this.add(fill, c);


    }

    void MakeMenu(JFrame frame){
        JMenuBar menubar = new JMenuBar();
        frame.setJMenuBar(menubar);
        JMenu filemenu = new JMenu("File");
        menubar.add(filemenu);
        //add file items
        JMenuItem quitapp = new JMenuItem("Exit");
        filemenu.add(quitapp);
        //add listerner
        quitapp.addActionListener(
                new ActionListener(){
                    public void actionPerformed(ActionEvent e){
                        System.exit(0);
                    }
                }
        );

    }



}//end class viewer

Recommended Answers

All 4 Replies

To make the components fit the entire screen, you could programmatically intercept the user's 'maximize' action (using WindowListener). Then, when the user hits maximize, you can use this technique to get the screen size. Then set the size on the JFrame using setSize to the size that you just got using the Toolkit class (as seen in the link). You might have to play around a bit with setting the JFrame's size, I don't remember exactly but you might need to call revalidate() on the JFrame after doing so, or resizing the JPanel might do it as well. I honestly don't remember but try each of those things, hopefully my suggestion about how to get the screen size and using WindowListener is helpful. If you have any more questions don't hesitate

mh I'm newbee somehow.
would you take me step by step? I'm lost in your good explanation. Because I'm novice

isn't there a widget for showing dir tree like other toolkits in other languages?

I'm retreating for a while.
It seems somehow difficult. I will come back

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.