Resize JFrame without flickering

When I try to resize my frame with the setSize() methode it starts
flickering but if i resize with the mouse it works fine.
Is there an other solution to resize a JFrame without flickering?
import java.awt.*;
import javax.swing.*;
public class TestFrame extends JFrame  {
    JPanel panel = new JPanel();
    JButton button = new JButton("Resize");
    public TestFrame() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(200,200);
        setLayout(new GridLayout());
        panel.setBackground(Color.red);
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setSize(getSize().width + 1, getSize().height + 1);
        this.add(button);
        this.add(panel);
    public static void main(String[] args) {
        TestFrame frame = new TestFrame();
        frame.setVisible(true);
}

frame.setUndecorated(true);and implement MouseListener and MouseMotionListener for resizing by mouse dragging on the edge

Similar Messages

  • How can a Swing JWindow be resized without flickering?

    Hi,
    I'm having trouble trying to get a custom UI I have made based on JWindow to resize in an acceptable way due to the amount of flicker.
    The problem is described here:
    http://stackoverflow.com/questions/4324221/how-can-a-swing-jwindow-be-resized-without-flickering
    I have tried various attempts at (manual) double buffering but it seems that nothing can help with the fact that the top window's ComponentPeer is resizing the window and drawing it with a 'stale' view of the layout and then doing the new layout afterwards. This happens for me in Windows XP and Ubuntu Linux under Sun/Oracle Java 1.6.0_u22 (latest), and apparently in Windows 7 too.
    I am unsure if this is a bug, defect or is this way by design. I also notice that Netbeans suffers from the same issue on resizing but Eclipse does not. Does this mean that Swing is currently incapable of laying out/painting a top level Component to a back buffer before drawing to the screen? Does this affect/annoy anyone else?
    Or if not, any ideas how a smooth redraw can be achieved?
    I understand that this issue does not bother everyone, but for custom UIs like the one I am trying to achieve the result is not really acceptable.
    Any help much appreciated!

    Thanks that's an interesting approach, I think I may use...
    AWTUtilities.setWindowShape(jWindow, newshape)...on a JWindow set to be the same size as the entire desktop as I need the UI to remain in place whilst allowing mouse and key events to reach other applications.
    Does any one think it would be worth submtting a bug report or feature request for the desired behaviour?
    That is: getting top level (heavyweight) containers to layout and repaint into a back buffer, and provide this buffer to the underlying window manager during the resize so that 'stale' window layouts are not drawn to the screen during resizing. This is the way SWT appears to function and so should be possible in theory.

  • Resizing JFrames and JPanels !!!

    Hi Experts,
    I had one JFrame in which there are three objects, these objects are of those classes which extends JPanel. So, in short in one JFrame there are three JPanels.
    My all JPanels using GridBagLayout and JFrame also using same. When I resize my JFrame ,it also resize my all objects.
    My Problem is how should i allow user to resize JPanels in JFrame also?? and if user is resizing one JPanel than other should adjust according to new size ...
    Plese guide me, how should i do this ...
    Thanknig Java Community,
    Dhwanit Shah

    Hey there, thanx for your kind intereset.
    Here is sample code .
    In which there is JFrame, JPanel and in JPanel ther is one JButton.Jpanel is added to JFrame.
    I want to resize JPanel within JFrame,I am able to do resize JFrame and JPanel sets accroding to it.
    import java.awt.*;
    import javax.swing.*;
    import com.borland.jbcl.layout.*;
    public class FramePanel extends JFrame {
    JPanel contentPane;
    GridBagLayout gridBagLayout1 = new GridBagLayout();
    public FramePanel() {
    try {
    jbInit();
    catch(Exception e) {
    e.printStackTrace();
    public static void main(String[] args) {
    FramePanel framePanel = new FramePanel();
    private void jbInit() throws Exception {
    contentPane = (JPanel) this.getContentPane();
    contentPane.setLayout(gridBagLayout1);
    this.setSize(new Dimension(296, 284));
    this.setTitle("Frame and Panel Together");
    MyPanel myPanel = new MyPanel();
    this.getContentPane().add(myPanel);
    this.setVisible(true);
    class MyPanel extends JPanel {
    public MyPanel() {
    this.setSize(200,200);
    this.setLayout(new FlowLayout());
    this.setBackground(Color.black);
    this.setVisible(true);
    this.add(new JButton("Dhwanit Shah"));
    I think i might explained my problem
    Dhwanit Shah
    [email protected]

  • Print JFrame without title bar and border

    I am trying to print out a JFrame which contains two JPanels. The following code is part of my implementation of Printable which prints the entire JFrame including the title bar and border. I've considered implementing Printable in the JPanel classes but then I don't know how I would get everything on one page as a single image. Is there some way to just print the contentPane of the JFrame?
         public int print(Graphics g, PageFormat pf, int pi)
              throws PrinterException {
              if (pi >= 1) {
                   return Printable.NO_SUCH_PAGE;
              Graphics2D g2 = (Graphics2D) g;
              double scaleX = pf.getImageableWidth()/getWidth();
              double scaleY = pf.getImageableHeight()/getHeight();
              double scale = 1;
              g2.translate((int) pf.getImageableX(), (int) pf.getImageableY());
              if (scaleX < 1 || scaleY < 1) {
                   scale = scaleX < scaleY ? scaleX : scaleY;
                   g2.scale(scale,scale);
              printAll(g2);
              return Printable.PAGE_EXISTS;
         }                    

    Thanks.
    I can get the Jframe without the title bar and I can get it to drag on the desktop with your suggestions. I still need it to have rounded edges.

  • Closing all JFrames without killing the application

    Is there a way I can close all JFrames without quiting the application? I have several JFrames open that I want to dispose of at the same time. Is this possible?

    If you close all the JFrames, then how does the user
    interact with the application?I think that's outside the scope of the original question, but there are a number of possibilities. To name just a few:
    1) The console
    2) The program opens a new JFrame after some time has elapsed (maybe an appointment reminder application, or an instant messenger client, for example)
    3) The program is really a server, so you don't need to see anything
    4) The program is "spyware" so it's meant to be hidden
    5) The program runs in full-screen mode part of the time (for example, a game which uses JFrames to choose options, then full-screen for game play)
    ...

  • JFrame without  _| |X

    Hi, I guess it's in the APIs, but I have looked for it and I cannot find. I guess it must be something really easy. How can I create a JFrame without the "minimize", "maximize", and "close" buttons?
    Thanks.

    As well as the apis there are zillions of posts already asking this question:
    http://onesearch.sun.com/search/developers/index.jsp?charset=UTF-8&qt=jframe+minimise+button&col=devforums
    I believe it is not possible to keep the title bar and lose the buttons, if that is what you wanted.

  • Resize partitions without erasing existing data

    I have one drive (internal), dual partition (OS X 10.5.2 and Vista) and I want to split the volume OS X 10.5.2 in two. I try this using Disk Utility, but one warning was showed: ..changing the partition map may make this disk unbootable using windows"
    Is it warning true? What do I expect to happen in case of resizing?
    If someone had have any related experience about that, I will appreciate Your advice.

    You can resize partitions without losing data but you won't be able to boot into Windows after that. That was a bad experience I had. My first bad experience was to repartition a dual-partition (Tiger+Windows) drive, ignoring the warning. Guess what? Lost everything and had to reinstall both OS.
    Mac OS X Extended (journalled) drives can be repartitioned without losing data using the "diskutil" command in the Terminal window. Type "diskutil" and you get a list of available commands.

  • Disabling Maximize option from JFrame without disabling the resizing option

    Hi all,
    I want to know how we can disable the maximize option of a JFrame window without disabling the resizing option. I have tried JFrame.resizable(false). It disables both maximizing and resizable option. But I need the resizable option to be working.
    Anyone can help me?
    Thanks in Advance

    this works OK on 1.5.0_05 (unsure about other versions)
    and you'll lose the minimize as well
    import javax.swing.*;
    import java.awt.*;
    class Testing
      public void buildGUI()
        JFrame f = new JFrame("JFrame");
        f.setUndecorated(true);
        f.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
        f.setSize(400,300);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
      public static void main(String[] args)
        SwingUtilities.invokeLater(new Runnable(){
          public void run(){
            new Testing().buildGUI();
    }

  • Is it possible to stop decorated JFrame/JDialog flickering while resizing

    Hi,
    In my application I use decorated JFrames and JDialogs. While resizing awkward flickering occurs, especially under Windows (under Linux is a little bit better).
    I've tried what I found:
    System.setProperty("sun.awt.noerasebackground", "true");
    Toolkit.getDefaultToolkit().setDynamicLayout(true);
    but nothing goes better.
    Any help is appriciated.
    Thanks in advance

    dont think so that is why i dont use it i was runing java 1.5 on 3.00gh with a very good graphics card and 512mb ram and still so i dont think there is a way.... but if there is some one will find it but it sure wont be me.

  • How to show different JTable without flickering JFrame?

    I write a simple application with 2 different tables. if user clicks "1" button, the frame will display table 1. else frame display table2. at the moment the code is like:
         JPanel panel1 = new JPanel();     
         ......//make the panel1 contains 2 buttons
         JPanel panel2 = new JPanel();
          ......//make the panel2 contains one table
         JFrame frame = new JFrame();     
         frame.getContentPane().add(panel1,BorderLayout.NORTH);
         frame.getContentPane().add(panel2,BorderLayout.CENTER);
         frame.setSize(700,500); frame.setLocatio(50,25);
         frame.setVisible(true);in ActionPerformed() method, the code is like:
          if (e.getActionCommand().equals("1")) {
              panel2 = new JPanel();
               ......//make the panel contains table1    
              frame.setVisible(false);
              frame.getContentPane().add(panel2, BorderLayout.CENTER);
              frame.setSize(700,500); frame.setLocatio(50,25);
              frame.setVisible(true);
           } else {  
               ...//do same thing with table2
           }there are 2 problems:
    1. every time the frame will flush once if user clicks one button. how to make the frame only change the panel which contains new table but not the button panel?
    2. about size: the user may resize the frame by draging one border of the frame. but the new frame always gets a fixed size. how to make the frame get the size the user adjusted?
    Maybe solve the first problem can automatically finish the second problem. but I don't know how to do.
    Thanks for any help.

    I post the same question twice yesterday but receive no answer. I think it should be straight-forward if you know. Is it really that difficult? I need help!
    Thanks.

  • Resize jdialog without flicker effect

    I made a vertical tool palette with a JDialog : I have "setcontentpane" a JPanel with vertical boxlayout.
    This JPanel contains other JPanels (class PaletteZone extends JPanel) that act like tool zones : they can be collapse or expand. If you want to have look of what i'm talking about, there is an image here of the palette :
    http://db-maths.nuxit.net/im1.png
    Here is my problem... I have a PaletteZone method used to collapse the tool zone :
    public void collapse(){
            ContentVisible=false;
            ZoneTitle.title.setIcon(rightTriangle);
    // component 0 is the zone title bar, component 1 an icon set (within a JPanel) :
            this.remove(1);
            JP.validate();
    }******* 1) When I use JP.validate, there is no flickering at all and the zone collapse nicely. BUT the JDialog isn't set to the good size. If you want to have a look on what you get when you clic on "Construction" :
    http://db-maths.nuxit.net/im2.png
    ******* 2) If I use JP.pack(), of course the dialog is correctly resized and redrawn. BUT there is a flickering effect : I've made some tries on JFrames and JDialogs and I saw that a pack() call clear immediatly the background and then send a paint call : this "clear/paint" sequence makes a bad flickering effect. I have a quite recent computer (G5 and OS X), and I use java 1.4.2 so I don't think the fact that I see this flicker is caused by hardware or software...
    ******* 3) If I use JP.setSize(), things behave exactly like if I do a pack() call...
    Do you know what I could do to avoid this flicker ? to set the size to the JDialog without this "clear/paint" sequence ?

    you will have to create a jpanel and place this in the dialog's NORTH.
    add a mouseListener to the panel and provide your own moving-handling.
    this.setLocation((int) (this.getX() + e.getX() - m_iOffset.getX()) ...
    this.repaint();you'll have to set m_iOffset wehen mousePressend, and call the moving method when mouseDragged.

  • Jframe is flickering -- Urgent help please!

    Basically i want to resize the window when i click some button say, NEW or EDIT and making some components visible.
    I am using setSize() method to resize the Jframe/window and setVisible() method.
    Do i need to use other methods. If you need some other details let me know as soon as possible.
    Below is the code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import javax.swing.event.*;
    import java.awt.Insets;
    import java.awt.GridBagLayout;
    import java.net.*;
    import java.io.*;
    import java.util.*;
    import com.symantec.itools.javax.swing.borders.LineBorder;
    import com.symantec.itools.javax.swing.borders.EtchedBorder;
    import com.symantec.itools.javax.swing.borders.BevelBorder;
    import com.symantec.itools.javax.swing.borders.CompoundBorder;
    import com.symantec.itools.javax.swing.borders.EmptyBorder;
    public class psRulesEditQ extends JFrame implements WindowListener
    public boolean inDebugMode = false;
    DBClient mydb;
    String SchemaID;
    int flagQ;
    int flagA;     
    DefaultListModel QuestionsModel = new DefaultListModel();
    DefaultListModel AnswersModel = new DefaultListModel();
    YesNoDialog alert;
    public psRulesEditQ (String title, DBClient mydb, String SchemaID)
    super(title);
    this.mydb = mydb;
    this.SchemaID = SchemaID;
    init();
    getQuestions();
    setTitle("Edit Questions for Domain: " + UserAttributes.getCurrentDomainName());
         public void init()
              // Take out this line if you don't use symantec.itools.net.RelativeURL or symantec.itools.awt.util.StatusScroller
              //symantec.itools.lang.Context.setApplet(this);
              // This line prevents the "Swing: checked access to system event queue" message seen in some browsers.
              getRootPane().putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
              // This code is automatically generated by Visual Cafe when you add
              // components to the visual environment. It instantiates and initializes
              // the components. To modify the code, only use code syntax that matches
              // what Visual Cafe can generate, or Visual Cafe may be unable to back
              // parse your Java file into its visual environment.
              //{{INIT_CONTROLS
              getContentPane().setLayout(null);
              setSize(660,330);
              //setBounds(50,50,660,330);
              EditQuestionsPanel.setLayout(null);
              getContentPane().add(EditQuestionsPanel);
              EditQuestionsPanel.setBounds(0,0,660,520);
              QuestionsPane.setOpaque(true);
              EditQuestionsPanel.add(QuestionsPane);
              QuestionsPane.setBounds(12,48,396,144);
              QuestionsPane.getViewport().add(QuestionsList);
              QuestionsList.setBounds(0,0,393,141);
              AnswersPane.setOpaque(true);
              EditQuestionsPanel.add(AnswersPane);
              AnswersPane.setBounds(444,48,192,144);
              AnswersPane.getViewport().add(AnswersList);
              AnswersList.setBounds(0,0,189,141);
              EditQuestionsPane.setOpaque(true);
              EditQuestionsPanel.add(EditQuestionsPane);
              EditQuestionsPane.setBounds(12,324,360,120);
              EditQuestionsPane.setVisible(false);
              EditQuestionsPane.getViewport().add(EditQuestionsTextArea);
              EditQuestionsTextArea.setBounds(0,0,357,117);
              EditQuestionsPanel.add(EditAnswersTextField);
              EditAnswersTextField.setBounds(444,324,134,25);
              EditAnswersTextField.setVisible(false);
              DeleteQuestionButton.setText("Delete");
              DeleteQuestionButton.setActionCommand("Delete");
              EditQuestionsPanel.add(DeleteQuestionButton);
              DeleteQuestionButton.setBounds(12,204,70,24);
              EditQuestionButton.setText("Edit");
              EditQuestionButton.setActionCommand("Edit");
              EditQuestionsPanel.add(EditQuestionButton);
              EditQuestionButton.setBounds(96,204,70,24);
              NewQuestionButton.setText("New");
              NewQuestionButton.setActionCommand("New");
              EditQuestionsPanel.add(NewQuestionButton);
              NewQuestionButton.setBounds(180,204,70,24);
              DeleteAnswerButton.setText("Delete");
              DeleteAnswerButton.setActionCommand("Delete");
              EditQuestionsPanel.add(DeleteAnswerButton);
              DeleteAnswerButton.setBounds(444,204,70,24);
              EditAnswerButton.setText("Edit");
              EditAnswerButton.setActionCommand("Edit");
              EditQuestionsPanel.add(EditAnswerButton);
              EditAnswerButton.setBounds(515,204,60,24);
              NewAnswerButton.setText("New");
              NewAnswerButton.setActionCommand("New");
              EditQuestionsPanel.add(NewAnswerButton);
              NewAnswerButton.setBounds(576,204,60,24);
              OkButton.setText("Ok");
              OkButton.setActionCommand("Ok");
              EditQuestionsPanel.add(OkButton);
              OkButton.setBounds(12,456,60,24);
              OkButton.setVisible(false);
              OkButton1.setText("Ok");
              OkButton1.setActionCommand("Ok");
              EditQuestionsPanel.add(OkButton1);
              OkButton1.setBounds(444,360,60,24);
              OkButton1.setVisible(false);
              CancelButton.setText("Cancel");
              CancelButton.setActionCommand("Cancel");
              EditQuestionsPanel.add(CancelButton);
              CancelButton.setBounds(84,456,73,24);
              CancelButton.setVisible(false);
              CancelButton1.setText("Cancel");
              CancelButton1.setActionCommand("Cancel");
              EditQuestionsPanel.add(CancelButton1);
              CancelButton1.setBounds(504,360,73,24);
              CancelButton1.setVisible(false);
              ShowRulesButton.setText("Show Rules");
              ShowRulesButton.setActionCommand("Show Rules");
              EditQuestionsPanel.add(ShowRulesButton);
              ShowRulesButton.setBounds(444,240,108,24);
              CloseButton.setText("Close");
              CloseButton.setActionCommand("Close");
              EditQuestionsPanel.add(CloseButton);
              CloseButton.setBounds(564,240,72,24);
              QuestionsLabel.setText("Questions");
              EditQuestionsPanel.add(QuestionsLabel);
              QuestionsLabel.setBounds(12,18,72,24);
              AnswersLabel.setText("Answers");
              EditQuestionsPanel.add(AnswersLabel);
              AnswersLabel.setBounds(444,12,60,36);
              EditQuestionsLabel.setText("Edit Questions");
              EditQuestionsPanel.add(EditQuestionsLabel);
              EditQuestionsLabel.setBackground(new java.awt.Color(204,204,204));
              EditQuestionsLabel.setBounds(12,276,90,48);
              EditQuestionsLabel.setVisible(false);
              NewQuestionsLabel.setText("New Questions");
              EditQuestionsPanel.add(NewQuestionsLabel);
              NewQuestionsLabel.setBounds(12,276,90,48);
              NewQuestionsLabel.setVisible(false);
              NewAnswersLabel.setText("New Answers");
              EditQuestionsPanel.add(NewAnswersLabel);
              NewAnswersLabel.setBounds(444,276,80,48);
              NewAnswersLabel.setVisible(false);
              EditAnswersLabel.setText("Edit Answers");
              EditQuestionsPanel.add(EditAnswersLabel);
              EditAnswersLabel.setBounds(444,276,80,48);
              EditAnswersLabel.setVisible(false);
              BorderLabel1.setBorder(etchedBorder1);
              EditQuestionsPanel.add(BorderLabel1);
              BorderLabel1.setBounds(5,12,643,264);
              BorderLabel2.setBorder(etchedBorder2);
              EditQuestionsPanel.add(BorderLabel2);
              BorderLabel2.setBounds(5,274,643,216);
              BorderLabel2.setVisible(false);
              //$$ etchedBorder1.move(0,540);
              //$$ etchedBorder2.move(24,540);
         QuestionsList.setModel(QuestionsModel);
         AnswersList.setModel(AnswersModel);
    //add all the listeners
    LAction LAction1 = new LAction();
         LSelector LSelector1 = new LSelector();
         //buttons
    CloseButton.addActionListener(LAction1);
    OkButton.addActionListener(LAction1);
    OkButton1.addActionListener(LAction1);
    DeleteAnswerButton.addActionListener(LAction1);
    DeleteQuestionButton.addActionListener(LAction1);
    NewAnswerButton.addActionListener(LAction1);
    NewQuestionButton.addActionListener(LAction1);
         EditQuestionButton.addActionListener(LAction1);
         EditAnswerButton.addActionListener(LAction1);
         CancelButton.addActionListener(LAction1);
         CancelButton1.addActionListener(LAction1);
    //ShowRulesButton.addActionListener(LAction1);
    //lists
         QuestionsList.addListSelectionListener(LSelector1);
         AnswersList.addListSelectionListener(LSelector1);
    //window
    this.addWindowListener(this);
         //{{DECLARE_CONTROLS
         javax.swing.JPanel EditQuestionsPanel = new javax.swing.JPanel();
         javax.swing.JScrollPane QuestionsPane = new javax.swing.JScrollPane();
         javax.swing.JList QuestionsList = new javax.swing.JList();
         javax.swing.JScrollPane AnswersPane = new javax.swing.JScrollPane();
         javax.swing.JList AnswersList = new javax.swing.JList();
         javax.swing.JScrollPane EditQuestionsPane = new javax.swing.JScrollPane();
         javax.swing.JTextArea EditQuestionsTextArea = new javax.swing.JTextArea();
         javax.swing.JTextField EditAnswersTextField = new javax.swing.JTextField();
         javax.swing.JButton DeleteQuestionButton = new javax.swing.JButton();
         javax.swing.JButton EditQuestionButton = new javax.swing.JButton();
         javax.swing.JButton NewQuestionButton = new javax.swing.JButton();
         javax.swing.JButton DeleteAnswerButton = new javax.swing.JButton();
         javax.swing.JButton EditAnswerButton = new javax.swing.JButton();
         javax.swing.JButton NewAnswerButton = new javax.swing.JButton();
         javax.swing.JButton OkButton = new javax.swing.JButton();
         javax.swing.JButton OkButton1 = new javax.swing.JButton();
         javax.swing.JButton CancelButton = new javax.swing.JButton();
         javax.swing.JButton CancelButton1 = new javax.swing.JButton();
         javax.swing.JButton ShowRulesButton = new javax.swing.JButton();
         javax.swing.JButton CloseButton = new javax.swing.JButton();
         javax.swing.JLabel QuestionsLabel = new javax.swing.JLabel();
         javax.swing.JLabel AnswersLabel = new javax.swing.JLabel();
         javax.swing.JLabel EditQuestionsLabel = new javax.swing.JLabel();
         javax.swing.JLabel NewQuestionsLabel = new javax.swing.JLabel();
         javax.swing.JLabel NewAnswersLabel = new javax.swing.JLabel();
         javax.swing.JLabel EditAnswersLabel = new javax.swing.JLabel();
         javax.swing.JLabel BorderLabel1 = new javax.swing.JLabel();
         javax.swing.JLabel BorderLabel2 = new javax.swing.JLabel();
         javax.swing.JOptionPane JOptionPane1 = new javax.swing.JOptionPane();
         javax.swing.JOptionPane JOptionPane2 = new javax.swing.JOptionPane();
         javax.swing.JOptionPane JOptionPane3 = new javax.swing.JOptionPane();
         javax.swing.JOptionPane JOptionPane4 = new javax.swing.JOptionPane();
         javax.swing.JOptionPane JOptionPane5 = new javax.swing.JOptionPane();
         javax.swing.JOptionPane JOptionPane6 = new javax.swing.JOptionPane();
         javax.swing.JOptionPane JOptionPane7 = new javax.swing.JOptionPane();
         com.symantec.itools.javax.swing.borders.EtchedBorder etchedBorder1 = new com.symantec.itools.javax.swing.borders.EtchedBorder();
         com.symantec.itools.javax.swing.borders.EtchedBorder etchedBorder2 = new com.symantec.itools.javax.swing.borders.EtchedBorder();
    public void getQuestionInfo()
    String SQL, line, QuestionID, info="";
    QuestionID = parseID(QuestionsList.getSelectedValue().toString());
    SQL = "SELECT Additional FROM ps_questions WHERE QuestionID = " + QuestionID;
    mydb.closeSQL();
    mydb.sendSQL(SQL);
    line = mydb.getRow();
    while (mydb.more())
    info = mydb.getColumn(0);
    line = mydb.getRow();
    YesNoDialog d = new YesNoDialog(this, "Additional Information for Question " + QuestionID, info, "OK", null, null);
    if (d.getAnswer() == d.YES)
    d.dispose();
    public void getAnswerInfo()
    String SQL, line, AnswerID, info="";
    AnswerID = parseID(AnswersList.getSelectedValue().toString());
    SQL = "SELECT Additional FROM ps_answers WHERE AnswerID = " + AnswerID;
    mydb.closeSQL();
    mydb.sendSQL(SQL);
    line = mydb.getRow();
    while (mydb.more())
    info = mydb.getColumn(0);
    line = mydb.getRow();
    YesNoDialog d = new YesNoDialog(this, "Additional Information for Answer " + AnswerID, info, "OK", null, null);
    if (d.getAnswer() == d.YES)
    d.dispose();
    public void getQuestions()
    String SQL, line;
    SQL = "SELECT QuestionID, QuestionText FROM tps_questions WHERE SchemaID = " + SchemaID + " ORDER BY QuestionID ASC";
    mydb.closeSQL();
    mydb.sendSQL(SQL);
    line = mydb.getRow();
    while (mydb.more())
    QuestionsModel.addElement("Q" + mydb.getColumn(0) + ": " + mydb.getColumn(1));
    line = mydb.getRow();
    public String parseID(String str)
    return str.substring(1, str.indexOf(":"));
    public void getAnswers()
    String SQL, line, QuestionID;
    String Question = QuestionsList.getSelectedValue().toString();
    AnswersModel.removeAllElements();
    QuestionID = parseID(Question);
    SQL = "SELECT AnswerID, AnswerText FROM tps_answers WHERE QuestionID = " + QuestionID;
    mydb.closeSQL();
    mydb.sendSQL(SQL);
    line = mydb.getRow();
    while (mydb.more())
    AnswersModel.addElement("A" + mydb.getColumn(0) + ": " + mydb.getColumn(1));
    line = mydb.getRow();
    public void setQuestion()
         EditAnswersTextField.setText("");
    String q = QuestionsList.getSelectedValue().toString();
    EditQuestionsTextArea.setText(q.substring(q.indexOf(":")+2, q.length()));
    public void setAnswer()
    String q = AnswersList.getSelectedValue().toString();
    EditAnswersTextField.setText(q.substring(q.indexOf(":")+2, q.length()));
    public String getNewID(String tableName, String fieldName)
    String SQL, line, newID;
    SQL = "";
    if (tableName.equals("tps_answers"))
    SQL = "SELECT MAX(AnswerID) FROM " + tableName;
    else if (tableName.equals("tps_questions"))
    SQL = "SELECT MAX(QuestionID) FROM " + tableName;
    mydb.closeSQL();
    mydb.sendSQL(SQL);
    line = mydb.getRow();
    int n = Integer.parseInt(mydb.getColumn(0).trim());
    n++;
    newID = n + "";
    return newID;
    public void addNewQuestion()
    //add to db and list
    int index;
    String q, QuestionID;
    q = EditQuestionsTextArea.getText();
    QuestionID = getNewID("tps_questions", "QuestionID");
    if (!(q.substring(q.length()-1, q.length())).equals("?"))
    q = q + "?";
    QuestionsModel.addElement("Q" + QuestionID + ": " + q);
    mydb.addRow("tps_questions");
    mydb.setColumn("QuestionID", QuestionID);
    mydb.setColumn("SchemaID", SchemaID);
    mydb.setColumn("QuestionText", "'" + q + "'");
    mydb.setColumn("Additional", "'null'");
    mydb.update();
         EditQuestionsTextArea.setText("");
         AnswersModel.removeAllElements();
         index = QuestionsModel.getSize();
         QuestionsList.setSelectedIndex(index-1);
    public void addEditQuestion()
         int i = QuestionsModel.indexOf(QuestionsList.getSelectedValue());
         String QuestionID = parseID(QuestionsList.getSelectedValue().toString());
         String q = EditQuestionsTextArea.getText();
              if (!(q.substring(q.length()-1, q.length())).equals("?"))
    q = q + "?";
         QuestionsModel.set(i, "Q" + QuestionID + ": " + q);
         mydb.editRow("tps_questions","QuestionID = " + QuestionID);
         mydb.setColumn("QuestionID", QuestionID);
    mydb.setColumn("SchemaID", SchemaID);
    mydb.setColumn("QuestionText", "'" + q + "'");
    mydb.setColumn("Additional", "'null'");
    mydb.update();
    public void addEditAnswer()
         String a, AnswerID, QuestionID;
    String q = QuestionsList.getSelectedValue().toString();
    a = EditAnswersTextField.getText();
    if (inDebugMode) System.err.println("a = " + a);
    QuestionID = parseID(q);
         int i = AnswersModel.indexOf(AnswersList.getSelectedValue());
    AnswerID = parseID(AnswersList.getSelectedValue().toString());
    AnswersModel.set(i, "A" + AnswerID + ": " + a);
    mydb.editRow("tps_answers","AnswerID = " + AnswerID);
    mydb.setColumn("QuestionID", QuestionID);
    mydb.setColumn("AnswerID", AnswerID);
    mydb.setColumn("SchemaId", SchemaID);
    mydb.setColumn("AnswerText", "'" + a + "'");
    mydb.setColumn("Additional", "'null'");
    mydb.update();
    // EditAnswersTextField.setText("");
    public void addNewAnswer()
    //getItems() method in List
    String a, AnswerID, QuestionID;
    String q = QuestionsList.getSelectedValue().toString();
    a = EditAnswersTextField.getText();
    if (inDebugMode) System.err.println("a = " + a);
    QuestionID = parseID(q);
    AnswerID = getNewID("tps_answers", "AnswerID");
    mydb.addRow("tps_answers");
    mydb.setColumn("QuestionID", QuestionID);
    mydb.setColumn("AnswerID", AnswerID);
    mydb.setColumn("SchemaId", SchemaID);
    mydb.setColumn("AnswerText", "'" + a + "'");
    mydb.setColumn("Additional", "'null'");
    mydb.update();
    AnswersModel.addElement("A" + AnswerID + ": " + a);
    EditAnswersTextField.setText("");
    //added 9-2-97 lmc - deletes an answer
    public void deleteAnswer(String answer)
    String msg;
    //getRulesA(answer);
    String Answer = AnswersList.getSelectedValue().toString();
    String AnswerID = answer.substring(1, answer.indexOf(":"));
    msg = "Are you sure you want to delete the answer:\n";
    msg += " \n";
    msg += AnswersList.getSelectedValue().toString() + " \n";
    msg += " \nThe following rules also use this question as a precondition.\n";
    //for (int i=0; i<rules.getLastVisibleIndex(); i++)
    // rules.setSelectedIndex(i);
    // msg += " \n rule " + rules.getSelectedIndex();
    msg += " \n \nDeleting the answer will affect these rules.";
    YesNoDialog confirm = new YesNoDialog(this, "Delete Answer " + AnswerID, msg, "Delete", "Cancel", null);
    if (confirm.getAnswer() == confirm.YES)
    mydb.deleteRow("tps_answers", "AnswerID=" + AnswerID);
    mydb.deleteRow("tps_preconditions", "AnswerID=" + AnswerID);
    AnswersModel.removeElementAt(AnswersList.getSelectedIndex());
    else System.err.println("did not delete");
    public void deleteQuestion(String question)
    String msg, SQL, line;
    String QuestionID = question.substring(1, question.indexOf(":"));
    //getRulesQ(question);
    msg = "Are you sure you want to delete the question:\n";
    msg += " \n";
    msg += QuestionsList.getSelectedValue().toString() + " \n";
    msg += " \nThe following rules also use this question as a precondition.\n";
    //for (int i=0; i<rules.getLastVisibleIndex(); i++)
    // rules.setSelectedIndex(i);
    // msg += " \n rule " + rules.getSelectedIndex();
    msg += " \n \nDeleting the question will affect these rules.";
    YesNoDialog confirm = new YesNoDialog(this, "Delete Question " + QuestionID, msg, "Delete", "Cancel", null);
    if (confirm.getAnswer() == confirm.YES)
    mydb.deleteRow("tps_questions", "QuestionID=" + QuestionID);
    SQL = "SELECT QuestionID FROM tps_answers WHERE QuestionID=" + QuestionID;
    mydb.sendSQL(SQL);
    line = mydb.getRow();
    while (mydb.more())
    mydb.deleteRow("tps_answers", "QuestionID=" + QuestionID);
    line = mydb.getRow();
    QuestionsModel.removeElementAt(QuestionsList.getSelectedIndex());
    AnswersModel.removeAllElements();
         EditQuestionsTextArea.setText("");          
    else System.err.println("did not delete");
    public void showRules(String question)
    ListFrame allRules;
    String QuestionID = question.substring(1, question.indexOf(":"));
    allRules = new ListFrame("All the Rules that Contain Question " + QuestionID, "Q", question, rules);
    allRules.show();
    /* //added 2/18/01 srh to make the "Show Rules" button work
    public void showRules(String question, String answer)
    ListFrame allRules;
    java.awt.List matchingRules = new java.awt.List();
    String listId, type="", SQL, line;
    if (question != null)
    {  listId = question.substring(1, question.indexOf(":"));
    type = "Quesiton";
    else if (answer != null)
    {  listId = answer.substring(1, question.indexOf(":"));
    type = "Answer";
    else
    {  //AppletGlobals.eval("alert('You must choose a quesiton or answer to display.')");
    alert = new YesNoDialog(this, "Add Answer ...", "You must choose a quesiton or answer to display.", "OK", null, null);
    return;
    SQL = "SELECT RuleID FROM tps_preconditions WHERE PreQuestionId = "+ listId;
    mydb.closeSQL();
    mydb.sendSQL(SQL);
    line = mydb.getRow();
    while (mydb.more())
    matchingRules.add(mydb.getColumn(0));
    line = mydb.getRow();
    allRules = new ListFrame("All the Rules that Contain "+type+" " + listId, "P", question, matchingRules);
    allRules.show();
    public void getRulesQ(String question)
    String SQL, line;
    String QuestionID;
         rules = new javax.swing.Jlist();
    QuestionID = question.substring(1, question.indexOf(":"));
    SQL = "SELECT RuleID FROM tps_preconditions WHERE PreQuestionID=" + QuestionID;
    mydb.closeSQL();
    mydb.sendSQL(SQL);
    line = mydb.getRow();
    while (mydb.more())
    rules.add(mydb.getColumn(0));
    line = mydb.getRow();
    public void getRulesA(String answer)
    String SQL, line;
    String AnswerID;
    rules = new javax.swing.Jlist();
    AnswerID = answer.substring(1, answer.indexOf(":"));
    SQL = "SELECT RuleID FROM tps_preconditions WHERE AnswerID=" + AnswerID;
    mydb.closeSQL();
    mydb.sendSQL(SQL);
    line = mydb.getRow();
    while (mydb.more())
    rules.add(mydb.getColumn(0));
    line = mydb.getRow();
         //ActionListener method
         class LAction implements java.awt.event.ActionListener
    public void actionPerformed(java.awt.event.ActionEvent event)
         Object object = event.getSource();
              if (object == CloseButton)
              dispose();
              else if (object == NewQuestionButton)
                        flagQ = 0;
                        setSize(660,550);
                        psRulesEditQ.this.setVisible(true);
                        //QuestionsList.setEnabled(false);
                        EditQuestionsLabel.setVisible(false);
                        NewAnswersLabel.setVisible(false);
                        EditAnswersLabel.setVisible(false);
                        EditAnswersTextField.setVisible(false);
              OkButton1.setVisible(false);
                        CancelButton1.setVisible(false);
                        BorderLabel2.setVisible(true);
                        CancelButton.setVisible(true);
                        NewQuestionsLabel.setVisible(true);
                        EditQuestionsPane.setVisible(true);
              EditQuestionsTextArea.setText("");
                        OkButton.setVisible(true);
                        //setSize(670,551);
                        //psRulesEditQ.this.getContentPane().setSize(670,551);
                        //EditQuestionsPane.repaint();
              else if (object == NewAnswerButton)
                        flagA = 0;
                        EditAnswersLabel.setVisible(false);          
                        NewQuestionsLabel.setVisible(false);
                        EditQuestionsLabel.setVisible(false);
                        EditQuestionsPane.setVisible(false);
                        OkButton.setVisible(false);
                        CancelButton.setVisible(false);
                             if (QuestionsList.isSelectionEmpty())
                             JOptionPane5.showMessageDialog(psRulesEditQ.this, "Select the question first!","Message",JOptionPane.PLAIN_MESSAGE);
                             else
                             setSize(660,550);
                             psRulesEditQ.this.setVisible(true);
                             BorderLabel2.setVisible(true);
                             //QuestionsList.setEnabled(true);
                             NewAnswersLabel.setVisible(true);
                             EditAnswersTextField.setVisible(true);
                   EditAnswersTextField.setText("");
                             OkButton1.setVisible(true);
                             CancelButton1.setVisible(true);
                   else if (object == EditQuestionButton)
                        flagQ = 1;
                        //QuestionsList.setEnabled(true);
                        NewQuestionsLabel.setVisible(false);
                        NewAnswersLabel.setVisible(false);
                        EditAnswersLabel.setVisible(false);
                        EditAnswersTextField.setVisible(false);
              OkButton1.setVisible(false);
                        CancelButton1.setVisible(false);
                        EditQuestionsLabel.setVisible(true);
                             if (QuestionsList.isSelectionEmpty())
                             JOptionPane2.showMessageDialog(psRulesEditQ.this, "Select a question to edit!","Message",JOptionPane.PLAIN_MESSAGE);
                             else
                             setSize(660,550);
                             psRulesEditQ.this.setVisible(true);
                             BorderLabel2.setVisible(true);
                             EditQuestionsPane.setVisible(true);     
                             OkButton.setVisible(true);
                             CancelButton.setVisible(true);
                             setQuestion();
                   else if (object == EditAnswerButton)
                        flagA = 1;
                        NewQuestionsLabel.setVisible(false);
                        NewAnswersLabel.setVisible(false);
                        EditQuestionsLabel.setVisible(false);
                        EditQuestionsPane.setVisible(false);
                        OkButton.setVisible(false);
                        CancelButton.setVisible(false);
                        EditAnswersLabel.setVisible(true);
                             if (AnswersList.isSelectionEmpty())
                             JOptionPane3.showMessageDialog(psRulesEditQ.this, "Select an answer to edit!","Message",JOptionPane.PLAIN_MESSAGE);
                             else
                             setSize(660,550);
                             psRulesEditQ.this.setVisible(true);
                             BorderLabel2.setVisible(true);
                             EditAnswersTextField.setVisible(true);
                             OkButton1.setVisible(true);
                             CancelButton1.setVisible(true);
                             setAnswer();
              else if (object == OkButton)
                   if (flagQ == 0)
                                  String x = EditQuestionsTextArea.getText();
                                  if (x.equals(""))
                                  JOptionPane6.showMessageDialog(psRulesEditQ.this, "You must type in a question first!","Message",JOptionPane.PLAIN_MESSAGE);
                                  else
                                  addNewQuestion();
                                  QuestionsList.setEnabled(true);
                             else if (flagQ == 1)
                             addEditQuestion();
                             setSize(660,330);
                             BorderLabel2.setVisible(false);
                             NewQuestionsLabel.setVisible(false);
                             EditQuestionsLabel.setVisible(false);
                             EditQuestionsPane.setVisible(false);
                             OkButton.setVisible(false);
                             CancelButton.setVisible(false);
              else if (object == OkButton1)
                             if (flagA == 0)
                                  String y = EditAnswersTextField.getText();
                                  if (y.equals(""))
                                  JOptionPane7.showMessageDialog(psRulesEditQ.this, "You must type in an answer first!","Message",JOptionPane.PLAIN_MESSAGE);
                                  else
                                  addNewAnswer();     
                             else if (flagA == 1)
                             addEditAnswer();
                             setSize(660,330);
                             BorderLabel2.setVisible(false);
                             NewAnswersLabel.setVisible(false);
                             EditAnswersLabel.setVisible(false);
                             EditAnswersTextField.setVisible(false);
                   OkButton1.setVisible(false);
                             CancelButton1.setVisible(false);
              else if (object == DeleteQuestionButton)
                             if (QuestionsList.isSelectionEmpty())
                             JOptionPane1.showMessageDialog(psRulesEditQ.this, "Select a question to delete!","Message",JOptionPane.PLAIN_MESSAGE);
              BorderLabel2.setVisible(false);
                        NewQuestionsLabel.setVisible(false);
                        EditQuestionsLabel.setVisible(false);
                        EditQuestionsPane.setVisible(false);
                        OkButton.setVisible(false);
                        CancelButton.setVisible(false);
                        CancelButton1.setVisible(false);
                        NewAnswersLabel.setVisible(false);
                        EditAnswersLabel.setVisible(false);
                        EditAnswersTextField.setVisible(false);
              OkButton1.setVisible(false);
                        deleteQuestion(QuestionsList.getSelectedValue().toString());
              else if (object == DeleteAnswerButton)
                   if (AnswersList.isSelectionEmpty())
                             JOptionPane4.showMessageDialog(psRulesEditQ.this, "Select an answer to delete!","Message",JOptionPane.PLAIN_MESSAGE);
                        BorderLabel2.setVisible(false);
                        NewQuestionsLabel.setVisible(false);
              EditQuestionsLabel.setVisible(false);
                        EditQuestionsPane.setVisible(false);
                        OkButton.setVisible(false);
                        CancelButton.setVisible(false);
                        CancelButton1.setVisible(false);
                        NewAnswersLabel.setVisible(false);
                        EditAnswersLabel.setVisible(false);
                        EditAnswersTextField.setVisi

    Hi,
    I have a JApplet with JComponents. When the size of the JApplet is larger than the browser, scrollbar of the browser is used. Flickering is very worse when scrolled. Any help in this regard is appreciated.
    Kiru

  • Resizing JFrame on button click to show an image on the JFrame

    Dear All,
    I have a JFrame which has an empty label. On button click I want to set an icon for the label and want the JFrame to be resized to show that icon. I am using frame.pack() and I am not using any other sizing function. The code that I have right now, prints the image on the panel, but does not resize the frame to show the image. Pleae could someone help.package gui;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.ItemEvent;
    import java.awt.event.ItemListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    public class ComponentDemo extends JPanel implements ActionListener,
    ItemListener, MouseListener, KeyListener {
         private JTextArea textarea;
         private JButton button;
         private final static String newline = "\n";
         private JLabel imageIcon;
         public ComponentDemo() {
              button = new JButton("JButton welcomes you  to CO2001");
              button.addActionListener(this);
              add(button);
              textarea = new JTextArea(10, 50);
              textarea.setEditable(false);
              addMouseListener(this);
              textarea.addKeyListener(this);
              JScrollPane scrollPane = new JScrollPane(textarea);
              add(scrollPane);
              imageIcon = new JLabel();
              add(imageIcon);
              setBackground(Color.pink);
              new JScrollPane(this);
          * Create the GUI and show it. For thread safety, this method should be
          * invoked from the event-dispatching thread.
         private static void createAndShowGUI() {
              // Create and set up the window.
              JFrame frame = new JFrame("Simple FrameDemo");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
              frame.setLocation(700, 200);
              // get the content pane and set the background colour;
              frame.add(new ComponentDemo());
         //     frame.setSize(screenSize);
              // frame.getContentPane().setBackground(Color.cyan);
              // Display the window.
              frame.pack();
              frame.setVisible(true);
              frame.setResizable(true);
         public static void main(String[] args) {
              // Schedule a job for the event-dispatching thread:
              // creating and showing this application's GUI.
              javax.swing.SwingUtilities.invokeLater(new Runnable() {
                   public void run() {
                        createAndShowGUI();
         @Override
         public void actionPerformed(ActionEvent e) {
              // TODO Auto-generated method stub
              if (e.getSource() instanceof JButton) {
                   // System.out.println(e.getSource());
                   String text = ((JButton) e.getSource()).getText();
                   textarea.append(text + newline);
                   textarea.setBackground(Color.cyan);
                   textarea.setForeground(Color.BLUE);
                   textarea.setCaretPosition(textarea.getDocument().getLength());
                   imageIcon.setIcon(createImageIcon("SwingingDuke.png",
                   "Image to be displayed"));
         @Override
         public void itemStateChanged(ItemEvent arg0) {
              // TODO Auto-generated method stub
         @Override
         public void mouseClicked(MouseEvent arg0) {
              textarea.append("A Mouse click welcomes you to CO2001" + newline);
              textarea.setBackground(Color.green);
              textarea.setCaretPosition(textarea.getDocument().getLength());
         @Override
         public void mouseEntered(MouseEvent arg0) {
              // TODO Auto-generated method stub
         @Override
         public void mouseExited(MouseEvent arg0) {
              // TODO Auto-generated method stub
         @Override
         public void mousePressed(MouseEvent arg0) {
              // TODO Auto-generated method stub
         @Override
         public void mouseReleased(MouseEvent arg0) {
              // TODO Auto-generated method stub
         @Override
         public void keyPressed(KeyEvent e) {
              System.out.println(e.getKeyChar());
              textarea.append("The key " + e.getKeyChar()
                        + " click welcomes you to CO2001" + newline);
              textarea.setBackground(Color.YELLOW);
              textarea.setFont(new Font("Arial", Font.ITALIC, 16));
              textarea.setCaretPosition(textarea.getDocument().getLength());
         @Override
         public void keyReleased(KeyEvent e) {
              System.out.println(e.getKeyChar());
              // textarea.append("The key "+
              // e.getKeyChar()+" click welcomes you to CO2001" + newline);
              // textarea.setBackground(Color.green);
              // textarea.setCaretPosition(textarea.getDocument().getLength());
         @Override
         public void keyTyped(KeyEvent e) {
              // TODO Auto-generated method stub
              System.out.println(e.getKeyChar());
              // textarea.append("The key "+
              // e.getKeyChar()+" click welcomes you to CO2001" + newline);
              // textarea.setBackground(Color.blue);
              // textarea.setCaretPosition(textarea.getDocument().getLength());
         /** Returns an ImageIcon, or null if the path was invalid. */
         protected ImageIcon createImageIcon(String path, String description) {
              java.net.URL imgURL = getClass().getResource(path);
              if (imgURL != null) {
                   System.out.println("found");
                   return new ImageIcon(imgURL, description);
              } else {
                   System.err.println("Couldn't find file: " + path);
                   return null;
    }

    myJPanel.setPerferredSize(new Dimension(new_width, new_hight));
    myJFrame.pack();

  • Resize canvas without moving/resizing items

    i'd like to resize the canvas of the page without moving or resizing any of the items within it. i tried file>document setup but it moves stuff around. i just want to trim a couple pixels off the top and bottom. in illustrator there's the canvas resize tool and in photoshop you can do it through a menu. how do you do it in indesign?
    thanks.

    Ah, no easy way to do it.  You can try turning on Layout Adjustment (Layout > Layout Adjustment) then with nothing selected changing the proxy in this thing:
    I can never remember it's name...
    However I doubt any of the above things will work like you want them to, they're not very intuitive either.

  • Increasing font size when resizing JFrame

    Hello,
    I am writing a calculator program, and therefore have a JLabel to display the numbers and multiple buttons. The JLabel and JButton components are added to a panel with a GridBagLayout, which is then added to the content pane of a JFrame. When the JFrame is resized, the size of the JButton and JLabel are increased, but the font in both of them stays the same. Is there anyway to increase the font size proportional to the size of the JButton and JLabel?

    Note: This thread was originally posted in the [Java Programming|http://forums.sun.com/forum.jspa?forumID=31] forum, but moved to this forum for closer topic alignment.

Maybe you are looking for