move up or move down the contents in a row(texboxes,comboboxes etc i)
Hello to all ,
I am working as a fresher and doing a project in swing , and i am facing problems in adding logic to the GUI. Please help me if any one have ever handled a problem i am mentioning belolow:
My appliction needs to move up or down the contents inside the textboxes and comboboxes (as the textfields and comboboxes present in one row). i want to select the row by clicking on the components and when i will click on moveup/down button then the whole row have to move one step up or down as clicked on the moveup/down button.my application needs to add many rows(components ) later.
and i am thinking as i will take jpanels and add the textfields and comboboxes on the jpanel and move the jpanel up or down as required.Can i do this.
I am waitin for your kind reply as i am serching for this logic and already waisted many times here.
Thanks
Mahendra
# 1
> and i am thinking as i will take jpanels and add the textfields and comboboxes on the jpanel and move the jpanel up or down as required.
Very good strategy. Now all you'll need is a simple class to do the management of the Panels, like a kinda model. This way, you will change the panel order in the model, and then update your view (where you display the panels) by rebuilding it taking information from the model. This how most Swing components are designed.
Moving the panels up and down would require you to obtain the required panel, remove it from the model and then re-insert it at the row above it. Here is some skeletal code
// this assumes moveIndex >= 1. You would to account for such a condition
JPanel panel = someModel.getPanelAt( moveIndex );
someModel.removePanelAt( moveIndex );
someModel.insertPanelAt( moveIndex - 1);
panelDisplay.rebuildUsingModel();
Here is an implementation of a such a model
http://www.geocities.com/icewalker2g/downloads/PanelModel.java
ICE
# 3
Here is a complete code example making use of the PanelModel you've downloaded. Just select a panel and click Move Up or Move Down to move the panel up or down. The major code concentration should be on the movePanel( ) and layoutPanels( ) methods
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.*;
import javax.swing.table.*;
import java.io.*;
import java.util.*;
public class PanelMoveExample extends JPanel {
public PanelModel model;
public JPanel selectedPanel, central;
public JButton moveUp, moveDown;
private int DOWN = 1, UP = 2;
public PanelMoveExample() {
super( new BorderLayout() );
model = new PanelModel();
createPanels();
moveUp = new JButton("Move Up");
moveUp.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
movePanel(UP);
}
});
moveDown = new JButton("Move Down");
moveDown.addActionListener( new ActionListener() {
public void actionPerformed(ActionEvent e) {
movePanel(DOWN);
}
});
JPanel butPanel = new JPanel( new FlowLayout(FlowLayout.RIGHT) );
butPanel.add(moveUp);
butPanel.add(moveDown);
add(butPanel, BorderLayout.NORTH);
}
public void layoutPanels() {
central.removeAll();
for(int i = 0; i < model.getSize(); i++) {
central.add( model.getPanel(i) );
}
central.validate();
central.repaint();
}
public void createPanels() {
if(central != null) {
remove(central);
}
central = new JPanel( new GridLayout(0,1,5,10) );
for(int i = 0; i < 5; i++) {
JPanel panel = new JPanel();
panel.addMouseListener( new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
JPanel thisPanel = (JPanel)e.getSource();
JPanel oldPanel;
if(selectedPanel != null) {
oldPanel = selectedPanel;
oldPanel.setBorder( new MatteBorder(1,1,1,1, Color.black) );
oldPanel.setBackground(null);
}
selectedPanel = thisPanel;
selectedPanel.setBorder( new MatteBorder(1,1,1,1,Color.red) );
selectedPanel.setBackground( new Color(220,220,230) );
}
});
panel.setBorder( new MatteBorder(1,1,1,1, Color.black) );
panel.setToolTipText("Click To Select");
panel.add( new JLabel("Selectable Panel " + (i+1))) ;
//for(int x = 0; x < 3; x++) {
//panel.add( new JLabel("Item " + (x+1) ) );
//}
model.addPanel( panel );
}
layoutPanels();
add(central, BorderLayout.CENTER);
}
public void movePanel(int direction) {
if(selectedPanel == null) {
return;
}
if(direction == DOWN) {
if( model.getPanelIndex(selectedPanel) == (model.getSize() - 1)) {
return;
}
int panelIndex = model.getPanelIndex( selectedPanel );
model.removePanel(selectedPanel);
model.insertPanelAt(selectedPanel, panelIndex + 1);
} else {
if( model.getPanelIndex(selectedPanel) == 0) {
return;
}
int panelIndex = model.getPanelIndex( selectedPanel );
model.removePanel(selectedPanel);
model.insertPanelAt(selectedPanel, panelIndex - 1);
}
layoutPanels();
}
public static void main(String[] args) {
JFrame frame = new JFrame("Panel Move Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add( new PanelMoveExample() );
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
ICE
# 4
Thanks icewalker2g for your support.
The code which you have sent i have tested, it is showing error like:
getSize() in panelModel can't override getSize() in java.awt.component;
but the GUI with 5 jpanels are coming and also jpanels are moving up and down by clicking moveup/down buttons.(this is strange that this code while compiling shows error and by running the GUI is coming)
The problem is that when i am trying to implement the logic from your code , my tool (Sun java Sudio Enterprise 8.1 and JDK1.5) is showing errors like:
can not find symbol though i have imported all the packages related to it . And all the errors are related to PanelModel.
while runnig the code with javac and java its showing the errror (getSize()) error but GUI appears with the required functionality but when i am trying to run itthrough tool its totally not allowing to compile .
Hope i have explained my problem clearly and needing your help.Its very urgent to me , i have done all related searches and experiments to it but still facing problem.
Looking for help and support.
Regards:
Mahendra