3 Replies - 2938 Views - Last Post: 21 June 2015 - 12:51 PM

#1 ChrisNt  Icon User is offline

  • cute bug

Reputation: 262
  • View blog
  • Posts: 894
  • Joined: 31-July 13

CHALLENGE: JTable search

Post icon  Posted 19 June 2015 - 12:31 PM

Time for a new JAVA CHALLENGE !!!

This challenge is not really difficult but its fun, you should try it ! .What you have to do here is to create a JTextfield which filters JTable's data.
You can see an example of how this program should respond based on user input.



Posted Image


HINT.


You can use the following classes but you could also make your own.
TableModel ( this class extends AbstractTableModel which will be used as the model for the JTable )
Gui ( this class should start your application )
DataList ( this class contains an ArrayList filled with data )


GUI
import javax.swing.JFrame;


public class Gui extends JFrame {
	
	
	public Gui() {
		setTitle("JTable search");
		setSize(500,500);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}
	
	public static void main(String args[]) {
		new Gui();
	}

}



DataList

import java.util.ArrayList;


public class DataList {
	
	private ArrayList<String> dataList;
	
	public DataList() {
		dataList = new ArrayList<String>();	
		fillArrayList();
	}
	
	private void fillArrayList() {
		dataList.add("Example1");
		dataList.add("Example11");
		dataList.add("Example111");
		dataList.add("Test0");
		dataList.add("Test00");
		dataList.add("Test");
		dataList.add("Testing");
	}

}





TableModel
import javax.swing.table.AbstractTableModel;


public class TableModel extends AbstractTableModel {

	@Override
	public int getRowCount() {		
		return 0;
	}

	@Override
	public int getColumnCount() {
		return 0;
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {	
		return null;
	}

}






HAVE FUN ! ! !

P.S Please place your code in spoiler tags

:bigsmile: :bigsmile: :bigsmile: :bigsmile:

This post has been edited by ChrisNt: 20 June 2015 - 12:54 AM


Is This A Good Question/Topic? 3
  • +

Replies To: CHALLENGE: JTable search

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,008
  • Joined: 11-December 07

Re: CHALLENGE: JTable search

Posted 21 June 2015 - 04:53 AM

Cool idea. How about this:

Spoiler

Was This Post Helpful? 3
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1567
  • View blog
  • Posts: 3,520
  • Joined: 05-April 11

Re: CHALLENGE: JTable search

Posted 21 June 2015 - 12:25 PM

Thanks for the challenge :^:

I've taken a MVC approach to the challenge. Definitely not the shortest solution, but that was never the intention with MVC :)

Tried to keep my controller slim.. Think I managed it OK for this challenge
cfoley reminded me of some JTable features I had totally forgotten. Could have made use of those features in my solution ^^
Java 8 is used, so make sure you have the JDK installed :)
Spoiler

Was This Post Helpful? 3
  • +
  • -

#4 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,008
  • Joined: 11-December 07

Re: CHALLENGE: JTable search

Posted 21 June 2015 - 12:51 PM

Nice. However, I would say that you only have to deal with the V of MVC. No need for the model to change. No need for the controller to care. Of course, this is mostly philosophical and you could rubbish my argument by requiring it to work on a massive dataset, for example.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1