aktualizacja komponentów

0

czesc

chce narysowac ponownie kilka komponentów po tym jak aplikacja zostala juz uruchomiona. prosze o wskazówki jak sie za to zabrac.

thx

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

public class Start implements ActionListener
{
	static boolean a = true;
	static boolean b = false;
	static String figureLabel = "Square";
	
	
	public JTable createTable(boolean a, boolean b)
	{
		JTable table;
		
		int squareRow = 9;
		int squareColumn = 9;
		
		int rectRow = 5;
		int rectColumn = 5;
		
		int triangRow = 6;
		int triangCol = 6;
			
		if (a && !b)
		{			
			table = new JTable(squareRow, squareColumn);
			figureLabel = "Square";
			Dimension d = new Dimension(300, 150);
        	table.setPreferredSize(d);			
		}
		else if (!a && b)
		{
			table = new JTable(rectRow, rectColumn);
			figureLabel = "Rectangle";
			Dimension d = new Dimension(300, 150);
        	table.setPreferredSize(d);
		}
		else
		{
			table = new JTable(triangRow, triangCol);
			figureLabel = "Triangle";
			Dimension d = new Dimension(300, 150);
        	table.setPreferredSize(d);		
		}
				
		return table;
	
	}
	
	public JMenuBar createMenuBar() 
	{
        JMenuBar menuBar;
        JMenu subFigure, subHelp;
        JMenuItem miTriangle, miSquare, miRectangle;
        JMenuItem miAuthor, miAbout;
        
        menuBar = new JMenuBar();
        
        subFigure = new JMenu("Figure");
        subHelp = new JMenu("Help");
        
        miTriangle = new JMenuItem("Triangle");
        miSquare = new JMenuItem("Square"); 
        miRectangle = new JMenuItem("Rectangle");
        miAuthor = new JMenuItem("Info");  
        miAbout = new JMenuItem("About");
               
        menuBar.add(subFigure);
        menuBar.add(subHelp);
        
        subFigure.add(miTriangle);
        subFigure.add(miSquare);
        subFigure.add(miRectangle);
        
        subHelp.add(miAuthor);
        subHelp.add(miAbout);
        
        //adding listeners
        miTriangle.addActionListener(this);
        miSquare.addActionListener(this);
        miRectangle.addActionListener(this);
        miAuthor.addActionListener(this);
        miAbout.addActionListener(this);
        
        
        return menuBar;
        
    }
    
    public JPanel createDrawPanel()
    {
    	JPanel drawPanel = new JPanel();
    	Dimension d = new Dimension(300, 220);
        drawPanel.setPreferredSize(d);
        drawPanel.setBackground(Color.green);
    	return drawPanel;
    }
    
    //actions
    
    public void actionPerformed(ActionEvent e) 
    {
    	//testing actions
        String info = e.getActionCommand();
        Character sign = new Character(info.charAt(0));
        int switches = sign.getNumericValue(sign);
     
        
        System.out.println(switches); //remove 
       
        
        switch (switches)
        {
        	case 29: System.out.println("Triangle");
        	
        	a = true; b = false;
        	
        	break;
        	
        	case 28: System.out.println("Square"); 
        	
        	a = true; b = false;
        	
        	
        	break;
        	
        	case 27: System.out.println("Rectangle"); 
        	
        	a = false; b = true;
        	      	
        	break;
        	
        	//unfinished
        }
     	
        
    }
  
    private static void createAndShowGUI() 
    {
    	Start start = new Start();
    	//creating frame
        Dimension d = new Dimension(300, 500);
        JFrame frame = new JFrame("Math_Chapter_1");
        frame.setPreferredSize(d);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        //frame created
       
        
        //setting layout
        frame.setLayout(new FlowLayout());
        //end of layout
        
        //buttons + listeners
        JButton changeAxis = new JButton("Change Axis");
        changeAxis.addActionListener(start);
        JButton rotaterButton = new JButton("Rotate");
        rotaterButton.addActionListener(start);
        //end of buttons
        
        //labels
        JLabel rotate = new JLabel("Rotate " + figureLabel);
        JLabel draw = new JLabel("Drawing " + figureLabel);
        //end of labels
       
        
        //adding components
        frame.setJMenuBar(start.createMenuBar());
        Container container = frame.getContentPane();
        container.setBackground(Color.blue);
        container.add(rotate);
        container.add(changeAxis); 
        container.add(rotaterButton);
        container.add(start.createTable(a, b));
        container.add(start.createDrawPanel());
        container.add(draw);
        //components added
      
        frame.pack();
        frame.setVisible(true);
    }
 

    public static void main(String[] args) 
    {
  
        javax.swing.SwingUtilities.invokeLater(new Runnable() 
        {
            public void run() 
            {
                createAndShowGUI();
            }
        });
    }
}


0

Metoda 1. Statycznie, tworzysz wszystkie potrzebne komponenty i bawisz się setVisible. Takie coś to zuo.
Metoda 2. Dynamicznie. Po aktualizacji danych w komponencie wywołujesz repaint(), względnie pack() dla obiektów JFrame/Frame.

duplikat skasowany:)

0

dzieki koziolek.

potestuje obie metody.

0

Ja bym to minimalnie uściślił. Invalidate() na wszystkich zmienionych komponentach, add/remove na tych do dodania i usunięcia (dostają automatycznie status invalid) i na koniec pack() lub validate(). Różnica jest taka, że validate jest częścią pack odpalaną już po obliczeniu preferowanych rozmiarów komponentów.

0

Olamagato, wielkie dzieki. Juz mialem, jak to okreslil koziolek, czynic zuo. Az dziw bierze ze na tutorialach suna ani slowa o tym nie ma.

0

forumowicze mi pomogli, w zamian, moze ktos inny będzie miał podobny problem to moze skorzysta z tego kodu

pozdr

//only gui
//add logic for table


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

public class Start2 extends JComponent implements ActionListener
{
	//declaration
	static boolean b = false;
	static boolean a = true;
	private static String figureLabel = "Square";
	private JTable table;
	private JPanel pane, tablepane;
	private JLabel rotate;
    private JLabel draw;
    private JButton changeAxis, rotaterButton;
	//end of declaration

    public JTable createTable(boolean a, boolean b)
    {
    	//setting rows
                       
                int squareRow = 9;
                int squareColumn = 9;
                
                int rectRow = 5;
                int rectColumn = 5;
                
                int triangRow = 6;
                int triangCol = 6;
                        
                if (a && !b)
                {                        
                        table = new JTable(squareRow, squareColumn);
                        figureLabel = "Square";
                        Dimension d = new Dimension(300, 150);
               		    table.setPreferredSize(d);                        
                }
                else if (!a && b)
                {
                        table = new JTable(rectRow, rectColumn);
                        figureLabel = "Rectangle";
                        Dimension d = new Dimension(300, 150);
                	    table.setPreferredSize(d);
                }
                else
                {
                        table = new JTable(triangRow, triangCol);
                        figureLabel = "Triangle";
                        Dimension d = new Dimension(300, 150);
               		    table.setPreferredSize(d);                
                }
                                
                return table;
        
    }
	
	public JMenuBar createMenuBar() 
	{
        JMenuBar menuBar;
        JMenu subFigure, subHelp;
        JMenuItem miTriangle, miSquare, miRectangle;
        JMenuItem miAuthor, miAbout;
        
        menuBar = new JMenuBar();
        
        subFigure = new JMenu("Figure");
        subHelp = new JMenu("Help");
        
        miTriangle = new JMenuItem("Triangle");
        miSquare = new JMenuItem("Square"); 
        miRectangle = new JMenuItem("Rectangle");
        miAuthor = new JMenuItem("Info");  
        miAbout = new JMenuItem("About");
               
        menuBar.add(subFigure);
        menuBar.add(subHelp);
        
        subFigure.add(miTriangle);
        subFigure.add(miSquare);
        subFigure.add(miRectangle);
        
        subHelp.add(miAuthor);
        subHelp.add(miAbout);
        
        //adding listeners
        miTriangle.addActionListener(this);
        miSquare.addActionListener(this);
        miRectangle.addActionListener(this);
        miAuthor.addActionListener(this);
        miAbout.addActionListener(this);
        
        
        return menuBar;
        
    }
    
    public void validating()
    {
    	//validating and setting components; see ActionPerformed
     		tablepane.invalidate();
			tablepane.remove(table);
			table = this.createTable(a, b);//creating new table
			tablepane.add(table);	
			tablepane.revalidate();
			tablepane.repaint();		
    	
    }
    
    public JPanel createDrawPanel()
    {
    	JPanel drawPanel = new JPanel();
    	Dimension d = new Dimension(300, 200);
        drawPanel.setPreferredSize(d);
        drawPanel.setBackground(Color.green);
    	return drawPanel;
    }
    
    //actions
    
    public void actionPerformed(ActionEvent e) 
    {
    	//testing actions
        String info = e.getActionCommand();
        Character sign = new Character(info.charAt(0));
        int switches = sign.getNumericValue(sign);
     
        
        System.out.println(switches); //remove 
       
        
        switch (switches)
        {
        	case 29: System.out.println("Triangle");
        	
        	//a and b determines the kind table displayed
        	a = false;	b = false;
        	
        	this.validating();
        	//6
    
        	break;
        	
        	case 28: System.out.println("Square"); 
        	
        	a = true;	b = false;
        	
        	this.validating();
        	//9
        
        	break;
        	
        	case 27: System.out.println("Rectangle"); 
        	
            a = false;	b = true; 
            
            this.validating(); 	
			
        	   	
        	break;
        
        	//unfinished
        }
        
    }
    
    ///end of actions
        
    public JPanel createComponents() 
    {
    			   
       //buttons + listeners
        JButton changeAxis = new JButton("Change Axis");
        changeAxis.addActionListener(this);
        JButton rotaterButton = new JButton("Rotate");
        rotaterButton.addActionListener(this);
        //end of buttons
        
        //labels
        rotate = new JLabel("Rotate " + figureLabel);
        draw = new JLabel("Drawing " + figureLabel);
        //end of labels
       
        pane = new JPanel(new FlowLayout());
        tablepane = new JPanel();
        table = this.createTable(a, b); 
         
        Dimension d = new Dimension(300, 150);
        tablepane.setPreferredSize(d);
        tablepane.add(table);           
        
        //adding components to pane             
        pane.setBackground(Color.blue);
        pane.add(rotate);
        pane.add(changeAxis);
        pane.add(tablepane);       
        pane.add(rotaterButton);          
        pane.add(this.createDrawPanel());
        pane.add(draw);
        //components added
            
        return pane;
    }
    	
    private static void createAndShowGUI() 
    {
    	Start2 start = new Start2();
    	
    	//create frame
    	Dimension d = new Dimension(300, 500);
        JFrame frame = new JFrame("Math_Chapter_1");
        frame.setPreferredSize(d);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setResizable(false);
        frame.setJMenuBar(start.createMenuBar());
        //frame created      
        
        //addding components to frame
        JPanel contents = start.createComponents();
        frame.getContentPane().add(contents, BorderLayout.CENTER);
        
    	
        frame.pack();
        frame.setVisible(true);
    }
 

    public static void main(String[] args) 
    {
  
        javax.swing.SwingUtilities.invokeLater(new Runnable() 
        {
            public void run() 
            {
                createAndShowGUI();
            }
        });
    }
    	
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1