Removing JTextFields after dynamically adding it - java

My goal is to delete two JTextFields at the same time by clicking a JLabel.
I've created the textfields like this:
public void mouseClicked(MouseEvent e) {
inc++;
txtName= new JTextField();
txtNumber = new JTextField();
txtName.setName("txtName"+inc);
txtNumber.setName("txtNumber" + inc);
pnlPanel.add(txtName);
pnlPanel.add(txtNumber);
if(count>0){
x+=50;
y+=50;
txtName.setBounds(225,6+y, 182, 27);
txtNumber.setBounds(35, 6+y, 182, 27);
txtName.setName(tempBox+count);
if(pnlTxtText.getComponentCount() >9){
pnlPanel.setPreferredSize(new Dimension(450+y,50+y));
pnlPanel.add(txtStudName);
pnlPanel.add(txtStudentNumber);
frmFrame.repaint();
scrpPanel.revalidate();
}
}
frmFrame.repaint();
}
});
And this is my code for removing the textfields:
public void mouseClicked(MouseEvent e) {
int countPlace= pnlPanel.getComponentCount();
int countOfRemaining =countPlace;
pnlPanel.remove(--countOfRemaining);
frmFrame.revalidate();
pnlPanel.remove(--countOfRemaining);
frmFrame.revalidate();
}
});
Instead of deleting the txtfields on the same row, it deletes it one by one, i dont want that. Please help me. Thank you.

Call pnlPanel.revalidate() before repaint()
And don't use setBounds(). Define porper LayoutManager instead.

Instead of this,
public void mouseClicked(MouseEvent e) {
int countPlace= pnlPanel.getComponentCount();
int countOfRemaining =countPlace;
pnlPanel.remove(countOfRemaining-1);
frmFrame.repaint();
pnlPanel.remove(countOfRemaining-1);
frmFrame.repaint();
}
});
Use this
public void mouseClicked(MouseEvent e) {
int countPlace= pnlPanel.getComponentCount();
int countOfRemaining =countPlace;
pnlPanel.remove(--countOfRemaining);
frmFrame.revalidate();
pnlPanel.remove(--countOfRemaining);
frmFrame.revalidate();
}
});
The above one throws an ArrayIndeOutOfBounds Exception because the count remaining variable is not being decremented after you remove a component. Hence the index goes out of bound when you try to remove the second time.

I recommend that you add all of your JPanels to a Map (I use HashMap) in the form of <Integer, JPanel>. Name them all in order, and then just do Map.remove(Map.size() - 1) and Map.remove(Map.size() - 2).
You can also get the current set of Integers (the keys) by doing a Map.keySet();
Map<Integer, JPanel> temp = new HashMap<Integer, JPanel>();
temp.put(0, new JPanel());
temp.put(1, new JPanel());
temp.put(2, new JPanel());
temp.remove(temp.size() - 1);
Makes it much easier to maintain numerous sets of panels. In my applications I don't control them numerically, but with short names instead.

Related

Jtable dynamical fixed columns problems

I created a class that can dynamically to lock and unlock columns .
In my program i create two tables with the same tablemodel.
One is in the Jviewport of the scrollpane, the other in the RowHeaderView.
The problem is when you unlock all the locked columns
and you want to start to lock again, doesn't work. There are no errors but it's like the event doesn't answer.
Steps to produce the problem:
Try the code,
put all the columns in the fixed table,
then unlock with right double click,
then start again to lock, and unlock
Do this procedure and you can see that the mouse event doesnt answer anymore
public class Prova extends JFrame{
private JTable mainTable,fixedTable;
private JScrollPane scrollPane;
private JTableHeader mainTableHeader;
private TableColumnModel originalColumnModel,mainColumnModel,fixedColumnModel;
private TableColumn[] columns;
private int ncols,counter;
public Prova(){
counter = 0;
TableModel mainTableModel = new DefaultTableModel(5, 10);
scrollPane = new JScrollPane();
mainTable = new JTable(mainTableModel);
mainColumnModel = mainTable.getColumnModel();
fixedTable = new JTable();
fixedTable.setAutoCreateColumnsFromModel(false);
fixedTable.setModel(mainTable.getModel() );
ncols = mainTableModel.getColumnCount();
columns = new TableColumn[ncols];
for (int i=0;i<ncols;i++){
columns[i] = mainColumnModel.getColumn(i);
}
mainColumnModel = mainTable.getColumnModel();
fixedColumnModel = fixedTable.getColumnModel();
mainTableHeader = mainTable.getTableHeader();
mainTableHeader.addMouseListener( new MouseAdapter(){
#Override
public void mouseClicked(MouseEvent me){
if (SwingUtilities.isRightMouseButton(me)){
if (ncols - counter>1){
counter ++;
int col = mainTable.columnAtPoint(me.getPoint());
TableColumn column = mainColumnModel.getColumn(col);
mainColumnModel.removeColumn(column);
fixedTable.getColumnModel().addColumn(column);
scrollPane.setRowHeaderView(fixedTable);
scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedTable.getTableHeader());
}
}
}
});
fixedTable.getTableHeader().addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent me){
if (SwingUtilities.isRightMouseButton(me) && me.getClickCount()== 2 ){
while (mainColumnModel.getColumnCount() > 0){
mainColumnModel.removeColumn(mainColumnModel.getColumn(0));
}
while (fixedColumnModel.getColumnCount() > 0){
fixedColumnModel.removeColumn(fixedColumnModel.getColumn(0));
}
for(int i=0;i<ncols;i++){
mainColumnModel.addColumn(columns[i]);
}
scrollPane.setRowHeaderView(null);
}
}
});
scrollPane.setViewportView(mainTable);
add(scrollPane, BorderLayout.CENTER);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Prova().setVisible(true);
}
});
}
}
A few pointers when posting a SSCCE:
for (int i=0;i<ncols;i++){
Don't be afraid to use whitespace in you code to make it more readable be separating the 3 statements of the for statement.
for (int i = 0; i < ncols; i++){
Keep the code simple and directly related to the problem:
TableModel mainTableModel = new EmployeeTableModel(listEmployees);
You question is about "moving columns", not about the data in the table so there is no need for a special TableModel and the Employee class. Just use the DefaultTableModel:
TableModel mainTableModel = new DefaultTableModel(5, 10);
Your current code won't compile because you didn't include the Employee class. By using JDK classes the code is smaller and easier to read.
The problem is when you unlock all the locked columns and you want to start to lock again, doesnt work
Your looping code is wrong. I didn't bother to figure out what was wrong. Instead I made the code simpler:
//for(int i=0;i<(ncols-counter);i++){
while (mainColumnModel.getColumnCount() > 0)
{
mainColumnModel.removeColumn(mainColumnModel.getColumn(0));
}
//for(int i=0;i<counter;i++){
while (fixedColumnModel.getColumnCount() > 0)
{
fixedColumnModel.removeColumn(fixedColumnModel.getColumn(0));
}
Another problem is your fixed table doesn't have a header so you don't know what the columns are. This is fixed by using:
scrollPane.setRowHeaderView(fixedTable);
scrollPane.setCorner(JScrollPane.UPPER_LEFT_CORNER, fixedTable.getTableHeader());
Now that you have a header you need to add the MouseListener to the header, not the scrollpane:
//scrollPane.addMouseListener(new MouseAdapter() {
fixedTable.getTableHeader().addMouseListener(new MouseAdapter() {
Edit:
You have a similar problem to what I fixed above. That is don't keep using variable to track values when you can use the component itself.
if (ncols - counter>1){
You never reset the value of the counter so the if condition won't be true the second time.
As I did above just use the value from the column model:
//if (ncols - counter>1){
if (mainColumnModel.getColumnCount() > 1) {
This is just basic problem solving. Put a display statement in the block of code to see if it executes when you have problems.

Boolean expression should be evaluated at runtime in a timer, reacting to changes in the swing gui

Here is what I am trying to do: I have a swing gui with two JFrames. The first has a JCheckBox and the second displays some text. Also the second has a javax.swing.Timer that is waiting for the checkbox in the first frame to be clicked. Once it is clicked, some more text is to be displayed. It works if I have only one condition (click the checkbox) and the condition is directly in the if-statement, like this:
javax.swing.Timer timer = new javax.swing.Timer(100, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if ( otherGUI.jCheckBox.isSelected() ){
//add some text to second JFrame
timer.stop();
}
}
});
Now for the twist: This should happen not only once, but multiple times. There is an ArrayList of timers, each with its own text and condition, one starting after the other. My problem is: If I store the conditions as strings in an ArrayList, they seem to be evaluated once at the start of the programme, so the condition from above stays false, even when I click the checkbox. Here is my actual code:
SomeGUI gui = new SomeGUI();
ArrayList<javax.swing.Timer> timer = new ArrayList<javax.swing.Timer>();
ArrayList<String> text = new ArrayList<String>();
ArrayList<String> cond = new ArrayList<String>();
text.add("some text");
cond.add("gui.jCheckBox.isSelected()");
text.add("some more text");
cond.add(new Condition("true"));
//etc.
for ( int i = 0; i < text.size() - 1; i++ ){
int j = i;//not sure why this trick is necessary. i doesn't work later on
timer.add( new javax.swing.Timer(100, new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
boolean bool = false;
try{
bool = Boolean.parseBoolean( cond.get(j) );
}
catch(Exception ex){}
if ( bool ){
addText(p, text.get(j+1));
timer.get(j).stop();
timer.get(j+1).start();
}
}
}));
}
timer.get(0).start();
I already tried an ArrayList<Boolean> for the conditions to the same effect. The code above just represents my present state of trial and error.
I hope that I could make clear what I am trying to achieve. So how can I store boolean expressions in a list/array and have them evaluated in an if-statement again and again at runtime and not only once when the programme is started?
There is no simple "evaluation" of strings in Java. It is possible, but really not "java style". See Convert String to Code
One other option would be that your strings represent method names (which exist on a well known object); then you could use reflection to invoke that method based on the provided string. But also, pretty ugly.

Using items from multiple comboBoxes

I have got few comboBoxes like:
final JComboBox listCosN = new JComboBox();
listCosN.setBounds(289, 261, 36, 20);
painting.add(listaCosN);
listCosN.addItem(1);
listCosN.addItem(2);
listCosN.addItem(3);
listCosN.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
int choosedCosN = listCosN.getSelectedIndex();
}
});
And now i want to use those choosed items from all of the comboBoxes. Doing simply
(for ex.)
int x = choosedCosN; after "});"
doesn't solve anything, because "int x cannot be resolved to a variable". That means choosed items are like local variables? How can i solve this?

Get text from JRadioButton that isn't part of a ButtonGroup

My program is a GUI. I have this method where when a button is clicked. It populates the next screen with JRadioButtons dynamically.
private void setExamButtonActionPerformed(java.awt.event.ActionEvent evt)
{
if(evt.getActionCommand().equals("Set Exam"))
{
CardLayout cL = (CardLayout)cardPanels.getLayout();
cL.show(cardPanels, "setExamPanel");
}
try
{
//InputStream code
String theMessage = myObject.getMessage();
String delims = "(?=(0*([0-9]{1,2}|100)))";
String[] questions = theMessage.split(delims);
System.out.println(Arrays.toString(questions));
for (int j = 1; j < questions.length; j++)
{
settingQuestionBoxes = new JCheckBox(questions[j]);
settingQuestionTextField = new JTextField("");
jPanel1.add(settingQuestionBoxes);
jPanel1.add(settingQuestionTextField);
jPanel1.revalidate();
jPanel1.repaint();
}
//close streams and socket code
}
catch(Exception e)
{
System.out.println(e);
}
}
Then I have this other method from another screen where the data that is populated from the previous method goes to.
private void setExamQuestionButtonActionPerformed(java.awt.event.ActionEvent evt)
{
if(evt.getActionCommand().equals("Set Exam Question"))
{
ArrayList<JToggleButton> settingQuestionBoxes = new ArrayList<JToggleButton>();
for(JToggleButton questions: settingQuestionBoxes)
{
if(questions.isSelected())
{
System.out.println(questions.getActionCommand());
}
}
CardLayout cL = (CardLayout)cardPanels.getLayout();
cL.show(cardPanels, "instructorPanel");
}
}
So basically when i call this System.out.println(questions.getActionCommand()) I'm trying to see the text from the JRadiobutton that was clicked on.
Right now when I run the program and select a button. Nothing happens.
Put the buttons into a List<JToggleButton> such as an ArrayList<JToggleButton> and then iterate through the list when the information is needed.
for (JToggleButton btn : myButtonList) {
if (btn.isSelected() {
String actionCommand = btn.getActionCommand();
// use the actionCommand here
}
}
Note that JToggleButton is the parent class for JRadioButton and using it would allow you to add JRadioButtons, JCheckBoxes, and JToggleButtons to the list. Since your JRadioButton is not part of a ButtonGroup, perhaps you should be using a JCheckBox instead.
Edit
You now have posted this code, stating it doesn't work:
// Section (A)
ArrayList<JToggleButton> settingQuestionButton = new ArrayList<JToggleButton>();
// Section (B)
for(JToggleButton questions: settingQuestionButon)
{
if(questions.isSelected())
{
System.out.println(questions.getActionCommand());
}
}
Is this code, both (A) and (B), all together in your program? If so, it would make sense that it doesn't work. You should have (A) in a constructor or some set up method. You should follow (A) with code that creates your JRadioButtons or JCheckBoxes, that sets their actionCommand String, that places them in the GUI, and that adds them to the ArrayList.
The part (B) code, the enhanced for loop would need to be in code that is called in response to an event, perhaps in a JButton or radio button's ActionListener.
Please check out this information and fill us in on the details. Please consider creating and posting an sscce illustrating your problem for us.
Edit 2
Your code is confusing in that you appear to have two completely variables of different types with the exact same name, and you appear to be assuming that this will give the variable magical properties that will allow it to know what it's "twin" might be doing. Java doesn't work that way, and in fact variable names are not nearly all that important or smart to allow them any such functionality. Rather your code must be smart.
I'm assuming that more than one of your JCheckBoxes will be checked, and that you want to check which ones are checked at some point in your program. If so, then in your class you should have a List or ArrayList field, something like
private List<JToggleButton> questionsList = new ArrayList<JToggleButton>();
This way this field will available throughout the class.
Then where you create your JCheckBoxes, you add them to this list:
private void setExamButtonActionPerformed(java.awt.event.ActionEvent evt)
{
if(evt.getActionCommand().equals("Set Exam"))
{
CardLayout cL = (CardLayout)cardPanels.getLayout();
cL.show(cardPanels, "setExamPanel");
}
try
{
String theMessage = myObject.getMessage();
String delims = "(?=(0*([0-9]{1,2}|100)))";
String[] questions = theMessage.split(delims);
for (int j = 1; j < questions.length; j++)
{
settingQuestionBox = new JCheckBox(questions[j]); // *** renamed to make more sense
settingQuestionBox.setActionCommand(questions[j]); // **** add actionCommand String
questionsList.add(settingQuestionBox); // ****** add JCheckBox to List
settingQuestionTextField = new JTextField("");
jPanel1.add(settingQuestionBox);
jPanel1.add(settingQuestionTextField);
jPanel1.revalidate();
jPanel1.repaint();
}
//close streams and socket code
}
catch(Exception e)
{
// System.out.println(e);
e.printStackTrace(); // ***** more informative
}
}
Then elsewhere in your code
setExamQuestionButtonActionPerformed(java.awt.event.ActionEvent evt)
{
if(evt.getActionCommand().equals("Set Exam Question"))
{
// ArrayList<JToggleButton> settingQuestionBoxes = new ArrayList<JToggleButton>();
for(JToggleButton questions: questionsList)
{
if(questions.isSelected())
{
System.out.println(questions.getActionCommand());
}
}
CardLayout cL = (CardLayout)cardPanels.getLayout();
cL.show(cardPanels, "instructorPanel");
}
}
And of course you'll need to take care that the ActionListener is added to a button

Enclose try-catch in a public method and invoke when needed

How to enclose try-catch in a public method and invoke when needed.
I have try-catch in one screen code. I want to invoke it from another screen by calling a method to it that stands public(to the entire application).
Is it possible ? If so how.
Please guide.
Re edits:
As seen in the below code, second tab pane implementation has been shown,please ignore the syntactic differences one may find with native java(This has been implemented for Blackberry JDE). Implementation constructs remain the same hence please overlook the differences and suggest a logical solution to the problem being faced.
// setup the second tab
vfm = new VerticalFieldManager(
Field.USE_ALL_HEIGHT | Field.USE_ALL_WIDTH |
Manager.VERTICAL_SCROLL | Manager.NO_HORIZONTAL_SCROLL );
//Initialize grid for publishing results
grid.add(new LabelField("Name")
{
public void paint(Graphics graphics)
{
graphics.setColor(Color.CYAN);
super.paint(graphics);
}
});
grid.add(new LabelField("Total")
{
public void paint(Graphics graphics)
{
graphics.setColor(Color.CYAN);
super.paint(graphics);
}
});
grid.setColumnPadding(100);
grid.setRowPadding(20);
//TRY CATCH STARTS HERE
try
{
//Open or create the database
Database db = DatabaseFactory.openOrCreate("database1.db");
Statement statementG55 = db.createStatement("CREATE TABLE IF NOT EXISTS GTemp4(gname TEXT,gbal INTEGER)");
statementG55.prepare();
statementG55.execute();
statementG55.close();
Statement statementG56 = db.createStatement("SELECT gname,gbal FROM GTemp4 ORDER BY ROWID DESC");
statementG56.prepare();
statementG56.execute();
Cursor c = statementG56.getCursor();
//Get to the row of grid
for (int i =1; i < grid.getRowCount(); i++)
{
System.out.println("Inside for first loops");
//Get to the column of grid
for (int j = 0; j < grid.getColumnCount() ; j++)
{
System.out.println("Inside for second loops");
//Get to the row of temp4 table
while(c.next())
{
System.out.println("Inside while");
Row r;
r = c.getRow();
for (int k = 1; k >=0; k--)
{
System.out.println("Inside for loops");
if(k==0)
{
System.out.println("Retrieving Names");
grid.insert(new LabelField(r.getString(k))
{
public void paint(Graphics graphics)
{
graphics.setColor(Color.GOLD);
super.paint(graphics);
}
},i,j);
}
else
{
System.out.println("Retrieving other values");
String p = "" + r.getObject(k);
grid.insert(new LabelField(p)
{
public void paint(Graphics graphics)
{
graphics.setColor(Color.GOLD);
super.paint(graphics);
}
},i,j);
}
grid.setBackground(BackgroundFactory.createLinearGradientBackground(Color.MIDNIGHTBLUE,Color.STEELBLUE,Color.MIDNIGHTBLUE,Color.STEELBLUE));
}
System.out.println("Exiting while");
}
System.out.println("Exiting sec for");
break;
}
System.out.println("Exiting first for");
break;
}
statementG56.close();
db.close();
}
catch(Exception e)
{
System.out.println( e.getMessage() );
e.printStackTrace();
}
vfm.add(grid);
nullFld = new NullField( Field.FOCUSABLE );
hfm = new HorizontalFieldManager();
hfm.add( nullFld );
hfm.add( myLbl );
pane = new Pane( hfm, vfm );
model.addPane( pane );
A big thanks to everyone below who have made a suggestion.
Your question is still cryptic. I am assuming that you have some code which does some searching and then, it publishes the results to some JPanel called pane2. What you want is that once the Search button is pressed, you call the code.
You could have a method like so:
public void doSomeSearching(...) throws Exception //This will allow you to remove the try/catch block from within the method and be able to catch any exceptions in the layer above.
{
//Do the searching
//Update panel2
}
Then, what you need to do is to add an action listener to your button. This will allow the code to be executed once the button is clicked. (You can find more information on ActionListeners here).
JButton btnSearch = new JButton("Search");
button.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e)
{
try
{
doSomeSearching();
}
catch (Exception e)
{
//Do something when exception is raised.
}
}
});
This should allow you to trigger the search functionality when you click the button and handle any exceptions which should arise.
EDIT:
another try-catch included within pane2 to publish(It keeps running from the start,i.e,doesnt wait for action listener of search button to be executed)`
Having something loop indefinitely is something which should be ideally avoided since this consumes CPU cycles while basically doing nothing. This usually increases the resources your application consumes and might also cause your GUI to hang. If I where you I would have some method which updates panel2 which you than call once you have done your searching.
That being said, you could have some intermediary variable, such as a string which contains whatever you need to print and your search method continuously updates this intermediary variable.
This approach is not recommended.
Instead of placing a generic surrounding try-catch block around your method, you should consider adding the "throws" clause to the declaration of each method you want to handle this way, and then handle all those exception in a higher layer.

Categories