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

[909 byte] By [Mahenda_Kumara] at [2007-11-27 9:53:06]
# 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

icewalker2ga at 2007-7-13 0:22:18 > top of Java-index,Desktop,Core GUI APIs...
# 2

Thanks for ur valuable reply.

I have seen the code (http://www.geocities.com/icewalker2g/downloads/PanelModel.java) which you have told to go through but that code is not running i.e the GUI is not comming(after making changes by me also not running) .

If u please give the code ,(if u have any) which u have given as sample or give me any url where i can get more information regarding this.

It will be better if u please describe clearly what logic i will implement in my application.

once again thanks for giving me your valuable time , looking for your responce .

Regards:

Mahendra

Mahenda_Kumara at 2007-7-13 0:22:18 > top of Java-index,Desktop,Core GUI APIs...
# 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

icewalker2ga at 2007-7-13 0:22:18 > top of Java-index,Desktop,Core GUI APIs...
# 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

Mahenda_Kumara at 2007-7-13 0:22:18 > top of Java-index,Desktop,Core GUI APIs...
# 5

> getSize() in panelModel can't override getSize() in java.awt.component;

PanelModel is class that does not even extend java.awt.Component so such an error should not be thrown.

I compile and run the code using JCreator and this error does not show, because it should not. Unless you have modified the code for PanelModel to extend a subclass of Component, then you should not get such an error.

Be sure to check your configuratiions for the compiler you are using and also ensure you have not made PanelModel extend JPanel or some other subclass of Component. It should stand alone.

Also the code will run if you have precompiled the class before making and changes that caused the error to be thrown.

ICE

icewalker2ga at 2007-7-13 0:22:18 > top of Java-index,Desktop,Core GUI APIs...
# 6

Hello ICE,

I have tried this code once again according to your suggestion , and facing the same problem as i mentioned in previous mail.

I have not made any changes to the code given by you , i am simply copying this code and trying to run but it is showing the same error related to PanelModel.

I am using JDK1.5 and Sun java studio enterprise 8.1 tool(built on netbeans technlogy). when i am trying to compile it through tool it is showing error like can not find symbol and no imports found for the following types:PanelModel. . I have checked all these configurations related to compiler and tool.

what is the problem i am unable to find out, give me if u have any other idea or other code which i can implement .plz its very urgent

Thanks & Regards:

Mahendra

Mahenda_Kumara at 2007-7-13 0:22:19 > top of Java-index,Desktop,Core GUI APIs...
# 7

Once again I believe the problem is with how you are compiling the code.

1. Dont copy and paste the code into another file, keep it in a separate file with the same name as the class.

2. Place the file in the same folder as all your other source files and ensure your ide creates the class files where they are supposed be.

3. If necessary, edit the source file to include the necessary package information if you are using packages ie package com.something;

4. Do not edit it the class to extend any other class especially Component or any of its subclasses

From all indications, your problem is with the compilation. You could also try creating a new class with a different name and transfer the methods to that class, probably one at a time, and compile each time until you can identify which methods are causing the problems and then rename them or fix them accordingly.

ICE

icewalker2ga at 2007-7-13 0:22:19 > top of Java-index,Desktop,Core GUI APIs...
# 8
Thanks for ur help ICE. that code is running fine.
Mahenda_Kumara at 2007-7-13 0:22:19 > top of Java-index,Desktop,Core GUI APIs...