Jump to content


Jtable + Jscrollpane Trouble


  • Please log in to reply
11 replies to this topic

#1 Nicholas Adri Chandoke

Nicholas Adri Chandoke

    Advanced Member

  • Members
  • PipPipPip
  • 48 posts
  • Gender:Not Telling

Posted 02 August 2008 - 05:29 PM

Does anyone know how I can use the JScrollPane that I have to let me scroll horizontally in the contained JTable?
And, yes, it already scrolls vertically.
And, yes, it also is only with JTable(s).

Also, how do I get the JTable to delete data by pressing the "delete" key (KeyEvent.VK_DELETE)?

#2 Nicholas Adri Chandoke

Nicholas Adri Chandoke

    Advanced Member

  • Members
  • PipPipPip
  • 48 posts
  • Gender:Not Telling

Posted 03 August 2008 - 06:59 PM

Nevermind. I just had to change the JTable.AUTO_RESIZE_ALL_COLUMNS to JTable.AUTO_RESIZE_OFF (I was subclassing JTable).

However, the second question of deleting cells still holds...

#3 Kraicheck

Kraicheck

    Advanced Member

  • Members
  • PipPipPip
  • 879 posts
  • Gender:Male
  • Location:Belgium

Posted 04 August 2008 - 10:21 AM

Implement KeyListener, listen for KeyEvents and react on them.

#4 Nicholas Adri Chandoke

Nicholas Adri Chandoke

    Advanced Member

  • Members
  • PipPipPip
  • 48 posts
  • Gender:Not Telling

Posted 04 August 2008 - 02:14 PM

addKeyListener(keyOps);
...

private KeyListener keyOps = new KeyListener() {

		public void keyTyped(KeyEvent e) {
			//do nothing
		}

		public void keyPressed(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_CLEAR)
				setValueAt(null, getX(), getY());
		}

		public void keyReleased(KeyEvent e) {
			//do nothing
		}
		
	};

The only problem is that the JTable's behavior (when a key is pressed) is to either edit the cell or insert the specified character into a cell. I think that I need to change that behavior. Or maybe I didn't implement the KeyListener correctly?

#5 Kraicheck

Kraicheck

    Advanced Member

  • Members
  • PipPipPip
  • 879 posts
  • Gender:Male
  • Location:Belgium

Posted 05 August 2008 - 03:21 PM

Hmm, try to consume the event.
That would prevent the JTable from acting on it after your own eventhandling.

#6 Nicholas Adri Chandoke

Nicholas Adri Chandoke

    Advanced Member

  • Members
  • PipPipPip
  • 48 posts
  • Gender:Not Telling

Posted 05 August 2008 - 09:51 PM

Might I ask, but what the hell does it mean to "consume an event"?!
Do you mean to override it? if so, how do I override an event?
if not, what's it mean and by "acting on it" do you mean the pressing of the [delete] button?

Sorry if I sound freaked. Also, I did write the KeyListener correctly, right? (I don't do KeyListeners often)

#7 Captain Pierce

Captain Pierce

    Advanced Member

  • Moderator
  • PipPipPip
  • 877 posts
  • Gender:Male
  • Location:Georgia

Posted 05 August 2008 - 11:06 PM

Look at the API docs, should be pretty clear what Krai's talking about ;)

#8 Nicholas Adri Chandoke

Nicholas Adri Chandoke

    Advanced Member

  • Members
  • PipPipPip
  • 48 posts
  • Gender:Not Telling

Posted 09 August 2008 - 03:11 PM

Um. I looked in the Java API (most throughly in the javax.swing.table package) but I couldn't find any interface, abstract class, or class that could have me get the desired results. Also, I couldn't find anything containing the word "consume"... Am I looking in the wrong place?

I've never written a KeyListener before; am I making a mistake in my code 4 entries ago?

#9 Captain Pierce

Captain Pierce

    Advanced Member

  • Moderator
  • PipPipPip
  • 877 posts
  • Gender:Male
  • Location:Georgia

Posted 09 August 2008 - 04:46 PM

http://java.sun.com/javase/6/docs/api/java...t/KeyEvent.html

Look for the consume method in the inherited methods section.



#10 Kraicheck

Kraicheck

    Advanced Member

  • Members
  • PipPipPip
  • 879 posts
  • Gender:Male
  • Location:Belgium

Posted 10 August 2008 - 09:09 AM

Your keylistener looks correct.
Here's a direct link to the consume method: AWTEvent.consume()

#11 Nicholas Adri Chandoke

Nicholas Adri Chandoke

    Advanced Member

  • Members
  • PipPipPip
  • 48 posts
  • Gender:Not Telling

Posted 11 August 2008 - 04:58 PM

Thanks very much. I will try out this method when I get the time to.

#12 Nicholas Adri Chandoke

Nicholas Adri Chandoke

    Advanced Member

  • Members
  • PipPipPip
  • 48 posts
  • Gender:Not Telling

Posted 15 August 2008 - 03:57 AM

Alright! I just got to trying it out, and it works perfectly! thank you very much.