Results 1 to 5 of 5
- 09-19-2010, 09:18 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 43
- Rep Power
- 0
Adding and removing components from a GridBagLayout
So what i want to do is display some JScrollPanes with graphs on then, depending on wheather their JCheckBox is selected. The JScrollPanes do appear and disappear when they are supposed to, but for the layout manager to rearange the JScrollPanes on the JPanel, the frame has to be resized. How can I get the layout manager to automatically rearange the JScrollPanes on the JPanel?
Here is the code for basic layout of my program with the JCheckBoxes, JScrollPanels.
Java Code:import java.awt.Dimension; import javax.swing.JFrame; public class Run { public static void main(String[] args){ ProjectileProgramFixedSize runProgram = new ProjectileProgramFixedSize(); runProgram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //setting frame runProgram.setSize(1920,1080); runProgram.setMinimumSize(new Dimension(800,600)); runProgram.setResizable(true); runProgram.setVisible(true); } }Java Code:import javax.swing.*; import java.awt.*; public class ProjectileProgramFixedSize extends JFrame{ //class objects SettingJComponents settingJComponents = new SettingJComponents(); ControlPanelJPanel controlPanelJPanel = new ControlPanelJPanel(this, settingJComponents); ProjectileAnimationJPanel projectileAnimationJPanel = new ProjectileAnimationJPanel(controlPanelJPanel); TimeSpeedGraphJPanel timeSpeedGraphJPanel = new TimeSpeedGraphJPanel(controlPanelJPanel); HorizontalVelocityGraphJPanel horizontalVelocityGraphJPanel = new HorizontalVelocityGraphJPanel(controlPanelJPanel); VerticalVelocityGraphJPanel verticalVelocityGraphJPanel = new VerticalVelocityGraphJPanel(controlPanelJPanel); //variables JScrollPane projectileAnimationJScrollPane = new JScrollPane(projectileAnimationJPanel); JScrollPane timeSpeedGraphJScrollPane = new JScrollPane(timeSpeedGraphJPanel); JScrollPane horizontalVelocityGraphJScrollPane = new JScrollPane(horizontalVelocityGraphJPanel); JScrollPane verticalVelocityGraphJScrollPane = new JScrollPane(verticalVelocityGraphJPanel); JPanel scrollPanesJPanel = new JPanel(); //constructor public ProjectileProgramFixedSize(){ //housekeeping super("Projectile Program"); //setting scrollPanesJPanel settingScrollPanesJPanel(); //setting layout settingLayout(); settingVariablesForOtherClasses(); } public void paint(Graphics g){ super.paint(g); this.setBackground(Color.DARK_GRAY); } public void settingVariablesForOtherClasses(){ //giving actionPerformed() in ControlPanelJPanel class the variables needed to start and run threads controlPanelJPanel.settingValues(projectileAnimationJPanel, timeSpeedGraphJPanel, horizontalVelocityGraphJPanel, verticalVelocityGraphJPanel); } public void settingScrollPanesJPanel(){ settingJComponents.settingScrollPanesJPanel(scrollPanesJPanel = new JPanel(), projectileAnimationJScrollPane, timeSpeedGraphJScrollPane, horizontalVelocityGraphJScrollPane, verticalVelocityGraphJScrollPane, ((controlPanelJPanel.timeSpeedGraphJCheckBox.isSelected())?true:false), ((controlPanelJPanel.horizontalVelocityGraphJCheckBox.isSelected())?true:false), ((controlPanelJPanel.verticalVelocityGraphJCheckBox.isSelected())?true:false)); } public void settingLayout(){ add(scrollPanesJPanel, BorderLayout.CENTER); add(controlPanelJPanel, BorderLayout.SOUTH); } }Java Code:import javax.swing.*; public class ProjectileAnimationJPanel extends JPanel{ ControlPanelJPanel controlPanelJPanel; public ProjectileAnimationJPanel(ControlPanelJPanel c){ controlPanelJPanel = c; } }Java Code:import javax.swing.*; public class TimeSpeedGraphJPanel extends JPanel{ ControlPanelJPanel controlPanelJPanel; //constructor public TimeSpeedGraphJPanel(ControlPanelJPanel c){ controlPanelJPanel = c; } }Java Code:import javax.swing.*; public class HorizontalVelocityGraphJPanel extends JPanel{ ControlPanelJPanel controlPanelJPanel; public HorizontalVelocityGraphJPanel(ControlPanelJPanel c){ controlPanelJPanel = c; } }Java Code:import javax.swing.*; public class VerticalVelocityGraphJPanel extends JPanel{ ControlPanelJPanel controlPanelJPanel; public VerticalVelocityGraphJPanel(ControlPanelJPanel c){ controlPanelJPanel = c; } }Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class ControlPanelJPanel extends JPanel implements ItemListener{ ProjectileProgramFixedSize projectileProgramFixedSize; ProjectileAnimationJPanel projectileAnimationJPanel; TimeSpeedGraphJPanel timeSpeedGraphJPanel; HorizontalVelocityGraphJPanel horizontalVelocityGraphJPanel; VerticalVelocityGraphJPanel verticalVelocityGraphJPanel; SettingJComponents settingJComponents; //creating JCheckBoxes JCheckBox timeSpeedGraphJCheckBox, verticalVelocityGraphJCheckBox, horizontalVelocityGraphJCheckBox; JPanel timeSpeedGraphJCheckBoxJPanel, horizontalVelocityGraphJCheckBoxJPanel, verticalVelocityJGraphCheckBoxJPanel, GraphJCheckBoxesJPanel; GridBagConstraints gc = new GridBagConstraints(); //constructor public ControlPanelJPanel(ProjectileProgramFixedSize pr, SettingJComponents JP){ projectileProgramFixedSize = pr; settingJComponents = JP; //setting ControlPanelJPanel Size this.setMinimumSize(new Dimension(projectileProgramFixedSize.getWidth(), 200)); this.setPreferredSize(new Dimension(projectileProgramFixedSize.getWidth(), 200)); //setting JCheckBoxes settingJComponents.settingJCheckBoxes(timeSpeedGraphJCheckBox = new JCheckBox("Time Speed Graph"), this); settingJComponents.settingJCheckBoxes(horizontalVelocityGraphJCheckBox = new JCheckBox("Horozontal Velocity Graph"), this); settingJComponents.settingJCheckBoxes(verticalVelocityGraphJCheckBox = new JCheckBox("Vertical Velocity Graph"), this); //setting graph check boxes JPanels settingJComponents.settingGraphCheckBoxesJPanels(timeSpeedGraphJCheckBoxJPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)), timeSpeedGraphJCheckBox, 150); settingJComponents.settingGraphCheckBoxesJPanels(horizontalVelocityGraphJCheckBoxJPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)), horizontalVelocityGraphJCheckBox, 200); settingJComponents.settingGraphCheckBoxesJPanels(verticalVelocityJGraphCheckBoxJPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)), verticalVelocityGraphJCheckBox, 200); settingJComponents.settingGraphCheckBoxesJPanel(GraphJCheckBoxesJPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)), timeSpeedGraphJCheckBoxJPanel, horizontalVelocityGraphJCheckBoxJPanel, verticalVelocityJGraphCheckBoxJPanel); //setting layout this.setLayout(new GridBagLayout()); //adding components to ControlPanelJPanel settingJComponents.addItemToGridBagLayout(gc, 0, 1, 8, 1, 25, 25, GridBagConstraints.WEST, GridBagConstraints.NONE, this, GraphJCheckBoxesJPanel); } public void paintComponent(Graphics g){ super.paintComponent(g); this.setBackground(SystemColor.activeCaption); g.setColor(Color.BLACK); g.drawLine(0,0,this.getWidth(),0); } //item listener public void itemStateChanged(ItemEvent event){ if(event.getSource().equals(timeSpeedGraphJCheckBox)){ if(timeSpeedGraphJCheckBox.isSelected()) projectileProgramFixedSize.timeSpeedGraphJScrollPane.setVisible(true); else projectileProgramFixedSize.timeSpeedGraphJScrollPane.setVisible(false); }else if(event.getSource().equals(horizontalVelocityGraphJCheckBox)){ if(horizontalVelocityGraphJCheckBox.isSelected()) projectileProgramFixedSize.horizontalVelocityGraphJScrollPane.setVisible(true); else projectileProgramFixedSize.horizontalVelocityGraphJScrollPane.setVisible(false); }else if(event.getSource().equals(verticalVelocityGraphJCheckBox)){ if(verticalVelocityGraphJCheckBox.isSelected()) projectileProgramFixedSize.verticalVelocityGraphJScrollPane.setVisible(true); else projectileProgramFixedSize.verticalVelocityGraphJScrollPane.setVisible(false); } } //setting values from ProjectileAnimationJPanel public void settingValues(ProjectileAnimationJPanel p, TimeSpeedGraphJPanel t, HorizontalVelocityGraphJPanel hv, VerticalVelocityGraphJPanel vv){ projectileAnimationJPanel = p; timeSpeedGraphJPanel = t; horizontalVelocityGraphJPanel = hv; verticalVelocityGraphJPanel = vv; } }Java Code:import java.awt.*; import javax.swing.*; public class SettingJComponents { //layout variables GridBagLayout layout; GridBagConstraints gc; Container container; public void addItemToGridBagLayout(GridBagConstraints gc, int x, int y, int w, int h, int wx, int wy, int a, int f, JComponent p, JComponent c){ gc.gridx = x; gc.gridy = y; gc.gridwidth = w; gc.gridheight = h; gc.weightx = wx; gc.weighty = wy; gc.insets = new Insets(5,5,5,5); gc.anchor = a; gc.fill = f; p.add(c, gc); } public void settingVariablesDisplayJPanel(JPanel variablesDisplayJPanel, JPanel speedDisplayJPanel, JPanel horizontalVelocityDisplayJPanel, JPanel verticalVelocityDisplayJPanel, JPanel timeDisplayJPanel){ variablesDisplayJPanel.add(speedDisplayJPanel); variablesDisplayJPanel.add(horizontalVelocityDisplayJPanel); variablesDisplayJPanel.add(verticalVelocityDisplayJPanel); variablesDisplayJPanel.add(timeDisplayJPanel); variablesDisplayJPanel.setOpaque(false); } public void settingButtonJPanel(JPanel buttonJPanel, JButton start, JButton reset){ buttonJPanel.add(start); buttonJPanel.add(reset); buttonJPanel.setOpaque(false); buttonJPanel.setMinimumSize(new Dimension(200,30)); buttonJPanel.setPreferredSize(new Dimension(200,30)); } public void settingGraphCheckBoxesJPanels(JPanel v, JCheckBox cb, int i){ v.add(cb); v.setOpaque(false); v.setPreferredSize(new Dimension(i,30)); v.setMinimumSize(new Dimension(i,30)); } public void settingGraphCheckBoxesJPanel(JPanel GraphJCheckBoxesJPanel, JPanel timeSpeedGraphJCheckBoxJPanel, JPanel horizontalVelocityGraphJCheckBoxJPanel, JPanel verticalVelocityJGraphCheckBoxJPanel){ GraphJCheckBoxesJPanel.add(timeSpeedGraphJCheckBoxJPanel); GraphJCheckBoxesJPanel.add(horizontalVelocityGraphJCheckBoxJPanel); GraphJCheckBoxesJPanel.add(verticalVelocityJGraphCheckBoxJPanel); GraphJCheckBoxesJPanel.setOpaque(false); GraphJCheckBoxesJPanel.setMinimumSize(new Dimension(600,30)); GraphJCheckBoxesJPanel.setPreferredSize(new Dimension(600,30)); } public void settingJCheckBoxes(JCheckBox cheackBox, ControlPanelJPanel c){ cheackBox.setOpaque(false); cheackBox.setSelected(true); cheackBox.addItemListener(c); } public void settingScrollPanesJPanel(JPanel scrollPanesJPanel, JScrollPane psroll, JScrollPane tsroll, JScrollPane hsroll, JScrollPane vsroll, boolean t, boolean h, boolean v){ //setting layout layout = new GridBagLayout(); gc = new GridBagConstraints(); scrollPanesJPanel.setLayout(new GridBagLayout()); scrollPanesJPanel.remove(tsroll); scrollPanesJPanel.remove(hsroll); scrollPanesJPanel.remove(vsroll); //setting size psroll.setPreferredSize(new Dimension(scrollPanesJPanel.getWidth(), scrollPanesJPanel.getHeight())); tsroll.setPreferredSize(new Dimension(scrollPanesJPanel.getWidth(), scrollPanesJPanel.getHeight())); hsroll.setPreferredSize(new Dimension(scrollPanesJPanel.getWidth(), scrollPanesJPanel.getHeight())); vsroll.setPreferredSize(new Dimension(scrollPanesJPanel.getWidth(), scrollPanesJPanel.getHeight())); scrollPanesJPanel.setBackground(Color.DARK_GRAY); //adding objects to ScrollPanesJPanel addItemToGridBagLayout(gc, 0, 0, 1, 3, 100, 100, GridBagConstraints.CENTER, GridBagConstraints.BOTH, scrollPanesJPanel, psroll); addItemToGridBagLayout(gc, 1, 0, 1, 1, 50, 100, GridBagConstraints.CENTER, GridBagConstraints.BOTH, scrollPanesJPanel, tsroll); addItemToGridBagLayout(gc, 1, 1, 1, 1, 50, 100, GridBagConstraints.CENTER, GridBagConstraints.BOTH, scrollPanesJPanel, hsroll); addItemToGridBagLayout(gc, 1, 2, 1, 1, 50, 100, GridBagConstraints.CENTER, GridBagConstraints.BOTH, scrollPanesJPanel, vsroll); } }Last edited by peterhabe; 09-19-2010 at 10:07 PM. Reason: To give runnable code to demonstate the problem as suggested by Fubarable
-
Almost sounds as if you want to use CardLayout.
Is that a reasonable thing to ask a volunteer to do? My own take on this is if it is your question, then the onus should be on you to do the work to create a small compilable, runnable program that we can run, test, fiddle with and help you correct. Else there's small chance that someone is going to do this grunt work for you.Here is the code for basic layout of my program with the JCheckBoxes, JScrollPanels. I've copyed and pasted the minimum code so you might have to mess about with it abit before you can get it to work.
Much luck!
-
Also, have you tried calling revalidate() on the containers holding your components? Again, I cannot test this myself without compilable runnable code.
Luck.
- 09-19-2010, 10:11 PM #4
Member
- Join Date
- Jul 2010
- Posts
- 43
- Rep Power
- 0
revalidate() works, thanks.
-
Similar Threads
-
Arrange components in a GridBagLayout
By ze snow in forum New To JavaReplies: 1Last Post: 02-27-2010, 02:22 PM -
Adding \ removing items from Jlist
By Desperado in forum AWT / SwingReplies: 10Last Post: 12-17-2009, 12:48 PM -
Adding and removing panels dynamically
By kbyrne in forum AWT / SwingReplies: 1Last Post: 04-12-2008, 08:28 PM -
gridbaglayout: increase/decrease size of components.
By newtojava7 in forum New To JavaReplies: 2Last Post: 01-28-2008, 07:22 AM -
Removing components from JPanel
By Echilon in forum New To JavaReplies: 0Last Post: 12-30-2007, 04:05 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks