6 Replies - 10425 Views - Last Post: 28 October 2008 - 04:35 PM Rate Topic: -----

#1 abgorn  Icon User is offline

  • sudo apt-get install brain
  • member icon

Reputation: 30
  • View blog
  • Posts: 1,410
  • Joined: 05-June 08

Resizing Components with the Window

Posted 28 October 2008 - 05:07 AM

I've been working on a simple text editor but I can't seem to figure out how to resize the JTextArea when the window resizes. Here's the source:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*; 

public class Main extends JFrame implements ActionListener, 
        ItemListener {
    
    public static JTextArea tA = new JTextArea("", 20, 50);
    JScrollPane scroller = new JScrollPane(tA,
            ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
            ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
    
    JMenuBar mB = new JMenuBar();
    JMenu fM = new JMenu("File");
    JMenuItem newFile = new JMenuItem("New");
    JMenuItem openFile = new JMenuItem("Open...");
    JMenuItem saveFile = new JMenuItem("Save...");
    JMenuItem exit = new JMenuItem("Exit");  
    JMenu eM = new JMenu("Edit");
    JMenuItem cut = new JMenuItem("Cut");
    JMenuItem copy = new JMenuItem("Copy");
    JMenuItem paste = new JMenuItem("Paste");
    JMenuItem selectAll = new JMenuItem("Select All");
    JMenuItem time = new JMenuItem("Time & Date");
    JMenu foM = new JMenu("Format");
    JCheckBoxMenuItem wordWrap = new JCheckBoxMenuItem("Word Wrap", true);
    JMenuItem font = new JMenuItem("Fonts...");
    
    public static void getTime() {
        String time;
        Calendar now = Calendar.getInstance();
        int hour = now.get(Calendar.HOUR_OF_DAY);
        int minute = now.get(Calendar.MINUTE);
        int month = now.get(Calendar.MONTH) + 1;
        int day = now.get(Calendar.DAY_OF_MONTH);
        int year = now.get(Calendar.YEAR);

        String monthName = "";
        switch (month) {
            case (1):
                monthName = "January";
                break;
            case (2):
                monthName = "February";
                break;
            case (3):
                monthName = "March";
                break;
            case (4):
                monthName = "April";
                break;
            case (5):
                monthName = "May";
                break;
            case (6):
                monthName = "June";
                break;
            case (7):
                monthName = "July";
                break;
            case (8):
                monthName = "August";
                break;
            case (9):
                monthName = "September";
                break;
            case (10):
                monthName = "October";
                break;
            case (11):
                monthName = "November";
                break;
            case (12):
                monthName = "December";
        }
        time = monthName + " " + day + ", " + year + " "
            + hour + ":" + minute;
    tA.append(time);
    }
    
    public Main() {
        super("nText 0.0.5");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(Frame.MAXIMIZED_VERT, Frame.MAXIMIZED_HORIZ);
        
        newFile.setToolTipText("Create a New File");
        openFile.setToolTipText("Open a File");
        saveFile.setToolTipText("Save Current File");
        exit.setToolTipText("Exit nText");
        
        copy.setToolTipText("Copy Selected Text");
        paste.setToolTipText("Paste Text");
        cut.setToolTipText("Cut Current Text");
        time.setToolTipText("Add Time & Date");
        
        wordWrap.setToolTipText("");
        font.setToolTipText("Change Current Font");
        
        newFile.addActionListener(this);
        openFile.addActionListener(this);
        saveFile.addActionListener(this);
        exit.addActionListener(this);
        copy.addActionListener(this);
        paste.addActionListener(this);
        cut.addActionListener(this);
        selectAll.addActionListener(this);
        time.addActionListener(this);
        
        wordWrap.addItemListener(this);
        font.addActionListener(this);
        
        
        eM.add(copy);
        eM.add(paste);
        eM.add(cut);
        eM.addSeparator();
        eM.add(selectAll);
        eM.add(time);
        
        fM.add(newFile);
        fM.add(openFile);
        fM.add(saveFile);
        fM.addSeparator();
        fM.add(exit);
        
        foM.add(wordWrap);
        foM.add(font);
        
        mB.add(fM);
        mB.add(eM);
        mB.add(foM);
        
        tA.setWrapStyleWord(true);
        tA.setLineWrap(true);
        
        BorderLayout bord = new BorderLayout();
        setLayout(bord);
        JPanel pane = new JPanel();
        pane.add(scroller, BorderLayout.CENTER);
        add(pane);
        setJMenuBar(mB);
        setVisible(true);
        pack();
        
    }
    
    public void itemStateChanged(ItemEvent ie) {
        Object itemSource = ie.getSource();
        if (itemSource == wordWrap) {
            if (wordWrap.getState() == true) {
                tA.setLineWrap(true);
            } else if (wordWrap.getState() == false) {
                tA.setLineWrap(false);
            }
        }
    }
    
    public void actionPerformed(ActionEvent evt) {
        Object source = evt.getSource();
        if (source == exit) {
            System.exit(0);
        } else if (source == saveFile) {
             new saveFileClass();
        } else if (source == openFile) {
            new openFileClass();
        } else if (source == newFile) {
            tA.setText("");
        } else if (source == copy) {
            tA.copy();
        } else if (source == paste) {
            tA.paste();
        } else if (source == cut) {
            tA.cut();
        } else if (source == selectAll) {
            tA.selectAll();
        } else if (source == time) {
            getTime();
        } else {
            
        }
    }
    
    public static void main(String[] args) {
        Main mW = new Main();
    }
}

class openFileClass {
    
    openFileClass() {
        final JFileChooser fc = new JFileChooser();
        int returnVal = fc.showOpenDialog(fc);
        if (returnVal == JFileChooser.APPROVE_OPTION) {  
            Main.tA.setText("");
            try {
                Scanner scan = new Scanner(new FileReader(fc.getSelectedFile().getPath())); 
                while (scan.hasNext())
                    Main.tA.append(scan.nextLine() + "\n");
            }
            catch (Exception ex) { 
            }  
        } 
    }
}

class saveFileClass {
    
    saveFileClass() {
        final JFileChooser fc = new JFileChooser();
        int returnVal = fc.showSaveDialog(fc);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            try {
                BufferedWriter out = new BufferedWriter(new FileWriter(fc.getSelectedFile().getPath()));  
                out.write(Main.tA.getText());
                out.close();
            }
            catch (Exception ex) {
            }
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Resizing Components with the Window

#2 absynthe  Icon User is offline

  • DIC Tease
  • member icon

Reputation: 28
  • View blog
  • Posts: 2,807
  • Joined: 20-September 08

Re: Resizing Components with the Window

Posted 28 October 2008 - 09:45 AM

add the jscrollpane to the panel borderlayout.center. You probably shouldn't set preferred size or minimal size on the jscrollpane, just let the borderlayout figure out what it should be. You'll probably need to set the preferred size on your panel though.
Was This Post Helpful? 0
  • +
  • -

#3 abgorn  Icon User is offline

  • sudo apt-get install brain
  • member icon

Reputation: 30
  • View blog
  • Posts: 1,410
  • Joined: 05-June 08

Re: Resizing Components with the Window

Posted 28 October 2008 - 10:48 AM

Nope, it didn't work. ~.^
Was This Post Helpful? 0
  • +
  • -

#4 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: Resizing Components with the Window

Posted 28 October 2008 - 01:07 PM

You could try adding an event listener for the form's resize event, then resize the components upon the form resize.

Alternatively, you could try doing something similar to what I did in my tutorial.

:)
Was This Post Helpful? 1
  • +
  • -

#5 abgorn  Icon User is offline

  • sudo apt-get install brain
  • member icon

Reputation: 30
  • View blog
  • Posts: 1,410
  • Joined: 05-June 08

Re: Resizing Components with the Window

Posted 28 October 2008 - 01:50 PM

Thanks gab! I'm not quite sure how it works, but it works and that's all that matters. ^_^
Was This Post Helpful? 0
  • +
  • -

#6 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1377
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: Resizing Components with the Window

Posted 28 October 2008 - 02:03 PM

Glad I could help :)

My brain's practically shut down for today, so I probably can't explain it too well.

BUT, I'll try anyway. It's the BorderLayout. It's basically like saying "our content pane is always going to match the border of the JFrame~ It will never be too big to extend it."

Then, you're adding the text panel to the content pane, and it fills it gets applied to the entire content pane.

^_^
Was This Post Helpful? 1

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Resizing Components with the Window

Posted 28 October 2008 - 04:35 PM

Absynthe is right
don't bother about the resizing Swing will do it for you
You must have a setMinimumSize() or setMaximumSize() or setPreferredSize() lying somewhere around
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1