JTable - 如何强制用户只选择一行

时间:2013-08-19 08:09:05

标签: java swing jtable

我必须实现一个JTable,其中必须选择一行(总是)。不允许空选择。我在初始化期间选择了第一行:

table.setRowSelectionInterval(0, 0);

另外,我正在使用

table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

但是用户仍然可以使用CLick + Ctrl取消选择一行。

最简单的方法是确保在表格中始终选择一行(exaclty)?

6 个答案:

答案 0 :(得分:20)

如果您创建了JTable实例,只需使用:

<EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/searchField" android:layout_row="0" android:layout_column="0"
        android:drawableLeft="@drawable/search"
        android:background="@drawable/rounded_edittext" android:minHeight="50dp"/>

答案 1 :(得分:15)

现在,您可以添加MouseListener s,SelectionListener s,KeyListener和密钥绑定来尝试解决此问题。或者,你可以解决问题的核心。

ListSelectionModel负责管理选择细节。

您可以为行选择提供自己的ListSelectionModel

public class ForcedListSelectionModel extends DefaultListSelectionModel {

    public ForcedListSelectionModel () {
        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    }

    @Override
    public void clearSelection() {
    }

    @Override
    public void removeSelectionInterval(int index0, int index1) {
    }

}

只需将它设置在你的桌子上......

table.setSelectionModel(new ForcedListSelectionModel());

答案 2 :(得分:4)

  

最简单的方法是确保一个(exaclty)行总是如此   选中表?

有三种(基本上)选择类型

JTable.setRowSelectionAllowed(boolean);
JTable.setColumnSelectionAllowed(boolean);
JTable.setCellSelectionAllowed(boolean);

修改

也适合我

int row = table.getSelectedRow();
    if ((row > -1)) {
        table.setRowSelectionInterval(row, row);
    }
}

答案 3 :(得分:1)

我会使用JTable#setRowSelectionAllowed,因为它可以确保选择一行。

答案 4 :(得分:0)

此代码将其更改回所需的索引,并为用户提供永不更新的外观。

Error building Player: CommandInvokationFailure: Failed to re-package resources. See the Console for details.
G:\Android\build-tools\24.0.0\aapt.exe package --auto-add-overlay -v -f -m -J gen -M AndroidManifest.xml -S "res" -I "G:/Android\platforms\android-24\android.jar" -F bin/resources.ap_ --extra-packages com.google.firebase:com.amazon.ags:com.google.android.gms:com.google.example.games.mainlibproj:com.google.android.gms.auth:com.google.android.gms.base:com.google.android.gms:com.google.android.gms.drive:com.google.android.gms.games:com.google.android.gms.nearby:com.google.android.gms.tasks:android.support.v4:com.unity3d.ads.android -S "C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\firebase-common-9.0.2\res" -S "C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\gamecircle_lib\res" -S "C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\google-play-services_lib\res" -S "C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\play-services-auth-base-9.0.2\res" -S "C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\play-services-base-9.0.2\res" -S "C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\play-services-basement-9.0.2\res" -S "C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\play-services-drive-9.0.2\res" -S "C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\play-services-games-9.0.2\res" -S "C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\play-services-nearby-9.0.2\res" -S "C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\play-services-tasks-9.0.2\res" -S "C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\support-v4-23.0.0\res" -S "C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res"

stderr[

]
stdout[
Configurations:
 (default)

Files:
  drawable\unityads_background_button_pause.xml
    Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\drawable\unityads_background_button_pause.xml
  drawable\unityads_icon_play.xml
    Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\drawable\unityads_icon_play.xml
  drawable\unityads_icon_speaker_base.xml
    Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\drawable\unityads_icon_speaker_base.xml
  drawable\unityads_icon_speaker_triangle.xml
    Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\drawable\unityads_icon_speaker_triangle.xml
  drawable\unityads_icon_speaker_waves.xml
    Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\drawable\unityads_icon_speaker_waves.xml
  layout\unityads_button_audio_toggle.xml
    Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\layout\unityads_button_audio_toggle.xml
  layout\unityads_button_pause.xml
    Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\layout\unityads_button_pause.xml
  layout\unityads_view_video_paused.xml
    Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\layout\unityads_view_video_paused.xml
  layout\unityads_view_video_play.xml
    Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\layout\unityads_view_video_play.xml
  layout\unityads_view_video_play2.xml
    Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\layout\unityads_view_video_play2.xml
  values\values.xml
    Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\values\values.xml
  AndroidManifest.xml
    Src: () AndroidManifest.xml

Resource Dirs:
  Type drawable
    drawable\unityads_background_button_pause.xml
      Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\drawable\unityads_background_button_pause.xml
    drawable\unityads_icon_play.xml
      Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\drawable\unityads_icon_play.xml
    drawable\unityads_icon_speaker_base.xml
      Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\drawable\unityads_icon_speaker_base.xml
    drawable\unityads_icon_speaker_triangle.xml
      Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\drawable\unityads_icon_speaker_triangle.xml
    drawable\unityads_icon_speaker_waves.xml
      Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\drawable\unityads_icon_speaker_waves.xml
  Type layout
    layout\unityads_button_audio_toggle.xml
      Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\layout\unityads_button_audio_toggle.xml
    layout\unityads_button_pause.xml
      Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\layout\unityads_button_pause.xml
    layout\unityads_view_video_paused.xml
      Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\layout\unityads_view_video_paused.xml
    layout\unityads_view_video_play.xml
      Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\layout\unityads_view_video_play.xml
    layout\unityads_view_video_play2.xml
      Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\layout\unityads_view_video_play2.xml
  Type values
    values\values.xml
      Src: () C:\Users\mreso\Documents\Falling Delivery\Temp\StagingArea\android-libraries\unityads\res\values\values.xml
Including resources from package: G:\Android\platforms\android-24\android.jar
applyFileOverlay for drawable
trying overlaySet Key=common_full_open_on_phone.png
trying overlaySet Key=common_google_signin_btn_icon_dark.xml
trying overlaySet Key=common_google_signin_btn_icon_dark_disabled.9.png
trying overlaySet Key=common_google_signin_btn_icon_dark_focused.9.png
trying overlaySet Key=common_google_signin_btn_icon_dark_normal.9.png
trying overlaySet Key=common_google_signin_btn_icon_dark_pressed.9.png
trying overlaySet Key=common_google_signin_btn_icon_light.xml
trying overlaySet Key=common_google_signin_btn_icon_light_disabled.9.png
trying overlaySet Key=common_google_signin_btn_icon_light_focused.9.png
trying overlaySet Key=common_google_signin_btn_icon_light_normal.9.png
trying overlaySet Key=common_google_signin_btn_icon_light_pressed.9.png
trying overlaySet Key=common_google_signin_btn_text_dark.xml
trying overlaySet Key=common_google_signin_btn_text_dark_disabled.9.png
trying overlaySet Key=common_google_signin_btn_text_dark_focused.9.png
trying overlaySet Key=common_google_signin_btn_text_dark_normal.9.png
trying overlaySet Key=common_google_signin_btn_text_dark_pressed.9.png
trying overlaySet Key=common_google_signin_btn_text_light.xml
trying overlaySet Key=common_google_signin_btn_text_light_disabled.9.png
trying overlaySet Key=common_google_signin_btn_text_light_focused.9.png
trying overlaySet Key=common_google_signin_btn_text_light_normal.9.png
trying overlaySet Key=common_google_signin_btn_text_light_pressed.9.png
trying overlaySet Key=common_ic_googleplayservices.png
trying overlaySet Key=common_plus_signin_btn_icon_dark.xml
trying overlaySet Key=common_plus_signin_btn_icon_dark_disabled.9.png
trying overlaySet Key=common_plus_signin_btn_icon_dark_focused.9.png
trying overlaySet Key=common_plus_signin_btn_icon_dark_normal.9.png
trying overlaySet Key=common_plus_signin_btn_icon_dark_pressed.9.png
trying overlaySet Key=common_plus_signin_btn_icon_light.xml
trying overlaySet Key=common_plus_signin_btn_icon_light_disabled.9.png
trying overlaySet Key=common_plus_signin_btn_icon_light_focused.9.png
trying overlaySet Key=common_plus_signin_btn_icon_light_normal.9.png
trying overlaySet Key=common_plus_signin_btn_icon_light_pressed.9.png
trying overlaySet Key=common_plus_signin_btn_text_dark.xml
trying overlaySet Key=common_plus_signin_btn_text_dark_disabled.9.png
trying overlaySet Key=common_plus_signin_btn_text_dark_focused.9.png
trying overlaySet Key=common_plus_signin_btn_text_dark_normal.9.png
trying overlaySet Key=common_plus_signin_btn_text_dark_pressed.9.png
trying overlaySet Key=common_plus_signin_btn_text_light.xml
trying overlaySet Key=common_plus_signin_btn_text_light_disabled.9.png
trying overlaySet Key=common_plus_signin_btn_text_light_focused.9.png
trying overlaySet Key=common_plus_signin_btn_text_light_normal.9.png
trying overlaySet Key=common_plus_signin_btn_text_light_pressed.9.png
trying overlaySet Key=cast_ic_notification_0.png
trying overlaySet Key=cast_ic_notification_1.png
trying overlaySet Key=cast_ic_notification_2.png
trying overlaySet Key=cast_ic_notification_connecting.xml
trying overlaySet Key=cast_ic_notification_on.png
trying overlaySet Key=common_full_open_on_phone.png
baseFile 0 has flavor mdpi-v4
baseFile 1 has flavor tvdpi-v4
baseFile 2 has flavor hdpi-v4
baseFile 3 has flavor xhdpi-v4
baseFile 4 has flavor xxhdpi-v4
overlayFile 0 has flavor mdpi-v4
overlayFile 1 has flavor tvdpi-v4
overlayFile 2 has flavor hdpi-v4
overlayFile 3 has flavor xhdpi-v4
overlayFile 4 has flavor xxhdpi-v4
found a match (0) for overlay file common_full_open_on_phone.png, for flavor mdpi-v4
found a match (1) for overlay file common_full_open_on_phone.png, for flavor tvdpi-v4
found a match (2) for overlay file common_full_open_on_phone.png, for flavor hdpi-v4
found a match (3) for overlay file common_full_open_on_phone.png, for flavor xhdpi-v4
found a match (4) for overlay file common_full_open_on_phone.png, for flavor xxhdpi-v4
trying overlaySet Key=common_google_signin_btn_icon_dark.xml
baseFile 0 has flavor 
overlayFile 0 has flavor 
found a match (0) for overlay file common_google_signin_btn_icon_dark.xml, for flavor 
trying overlaySet Key=common_google_signin_btn_icon_dark_disabled.9.png
baseFile 0 has flavor mdpi-v4
baseFile 1 has flavor tvdpi-v4
baseFile 2 has flavor hdpi-v4
baseFile 3 has flavor xhdpi-v4
baseFile 4 has flavor xxhdpi-v4
overlayFile 0 has flavor mdpi-v4
overlayFile 1 has flavor tvdpi-v4
overlayFile 2 has flavor hdpi-v4
overlayFile 3 has flavor xhdpi-v4
overlayFile 4 has flavor xxhdpi-v4
found a match (0) for overlay file common_google_signin_btn_icon_dark_disabled.9.png, for flavor mdpi-v4
found a match (1) for overlay file common_google_signin_btn_icon_dark_disabled.9.png, for flavor tvdpi-v4
found a match (2) for overlay file common_google_signin_btn_icon_dark_disabled.9.png, for flavor hdpi-v4
found a match (3) for overlay file common_google_signin_btn_icon_dark_disabled.9.png, for flavor xhdpi-v4
found a match (4) for overlay file common_google_signin_btn_icon_dark_disabled.9.png, for flavor xxhdpi-v4
trying overlaySet Key=common_google_signin_btn_icon_dark_focused.9.png
baseFile 0 has flavor mdpi-v4
baseFile 1 has flavor tvdpi-v4
baseFile 2 has flavor hdpi-v4
baseFile 3 has flavor xhdpi-v4
baseFile 4 has flavor xxhdpi-v4
overlayFile 0 has flavor mdpi-v4
overlayFile 1 has flavor tvdpi-v4
overlayFile 2 has flavor hdpi-v4
overlayFile 3 has flavor xhdpi-v4
overlayFile 4 has flavor xxhdpi-v4
found a match (0) for overlay file common_google_signin_btn_icon_dark_focused.9.png, for flavor mdpi-v4
found a match (1) for overlay file common_google_signin_btn_icon_dark_focused.9.png, for flavor tvdpi-v4
found a match (2) for overlay file common_google_signin_btn_icon_dark_focused.9.png, for flavor hdpi-v4
found a match (3) for overlay file common_google_signin_btn_icon_dark_focused.9.png, for flavor xhdpi-v4
found a match (4) for overlay file common_google_signin_btn_icon_dark_focused.9.png, for flavor xxhdpi-v4
trying overlaySet Key=common_google_signin_btn_icon_dark_normal.9.png
baseFile 0 has flavor mdpi-v4
baseFile 1 has flavor tvdpi-v4
baseFile 2 has flavor hdpi-v4
baseFile 3 has flavor xhdpi-v4
baseFile 4 has flavor xxhdpi-v4
overlayFile 0 has flavor mdpi-v4
overlayFile 1 has flavor tvdpi-v4
overlayFile 2 has flavor hdpi-v4
overlayFile 3 has flavor xhdpi-v4
overlayFile 4 has flavor xxhdpi-v4
found a match (0) for overlay file common_google_signin_btn_icon_dark_normal.9.png, for flavor mdpi-v4
found a match (1) for overlay file common_google_signin_btn_icon_dark_normal.9.png, for flavor tvdpi-v4
found a match (2) for overlay file common_google_signin_btn_icon_dark_normal.9.png, for flavor hdpi-v4
found a match (3) for overlay file common_google_signin_btn_icon_dark_normal.9.png, for flavor xhdpi-v4
found a match (4) for overlay file common_google_signin_btn_icon_dark_normal.9.png, for flavor xxhdpi-v4
trying overlaySet Key=common_google_signin_btn_icon_dark_pressed.9.png
baseFile 0 has flavor mdpi-v4
baseFile 1 has flavor tvdpi-v4
baseFile 2 has flavor hdpi-v4
baseFile 3 has flavor xhdpi-v4
baseFile 4 has flavor xxhdpi-v4
overlayFile 0 has flavor mdpi-v4
overlayFile 1 has flavor tvdpi-v4
overlayFile 2 has flavor hdpi-v4
overlayFile 3 has flavor xhdpi-v4
overlayFile 4 has flavor xxhdpi-v4
found a match (0) for overlay file common_google_signin_btn_icon_dark_pressed.9.png, for flavor mdpi-v4
found a match (1) for overlay file common_google_signin_btn_icon_dark_pressed.9.png, for flavor tvdpi-v4
found a match (2) for overlay file common_google_signin_btn_icon_dark_pressed.9.png, for flavor hdpi-v4
found a match (3) for overlay file common_google_signin_btn_icon_dark_pressed.9.png, for flavor xhdpi-v4
found a match (4) for overlay file common_google_signin_btn_icon_dark_pressed.9.png, for flavor xxhdpi-v4
trying overlaySet Key=common_google_signin_btn_icon_light.xml
baseFile 0 has flavor 
overlayFile 0 has flavor 
found a match (0) for overlay file common_google_signin_btn_icon_light.xml, for flavor 
trying overlaySet Key=common_google_signin_btn_icon_light_disabled.9.png
baseFile 0 has flavor mdpi-v4
baseFile 1 has flavor tvdpi-v4
baseFile 2 has flavor hdpi-v4
baseFile 3 has flavor xhdpi-v4
baseFile 4 has flavor xxhdpi-v4
overlayFile 0 has flavor mdpi-v4
overlayFile 1 has flavor tvdpi-v4
overlayFile 2 has flavor hdpi-v4
overlayFile 3 has flavor xhdpi-v4
overlayFile 4 has flavor xxhdpi-v4
found a match (0) for overlay file common_google_signin_btn_icon_light_disabled.9.png, for flavor mdpi-v4
found a match (1) for overlay file common_google_signin_btn_icon_light_disabled.9.png, for flavor tvdpi-v4
found a match (2) for overlay file common_google_signin_btn_icon_light_disabled.9.png, for flavor hdpi-v4
found a match (3) for overlay file common_google_signin_btn_icon_light_disabled.9.png, for flavor xhdpi-v4
found a match (4) for overlay file common_google_signin_btn_icon_light_disabled.9.png, for flavor xxhdpi-v4
trying overlaySet Key=common_google_signin_btn_icon_light_focused.9.png
baseFile 0 has flavor mdpi-v4
baseFile 1 has flavor tvdpi-v4
baseFile 2 has flavor hdpi-v4
baseFile 3 has flavor xhdpi-v4
baseFile 4 has flavor xxhdpi-v4
overlayFile 0 has flavor mdpi-v4
overlayFile 1 has flavor tvdpi-v4
overlayFile 2 has flavor hdpi-v4
overlayFile 3 has flavor xhdpi-v4
overlayFile 4 has flavor xxhdpi-v4
found a match (0) for overlay file common_google_signin_btn_icon_light_focused.9.png, for flavor mdpi-v4
found a match (1) for overlay file common_google_signin_btn_icon_light_focused.9.png, for flavor tvdpi-v4
found a match (2) for overlay file common_google_signin_btn_icon_light_focused.9.png, for flavor hdpi-v4
found a match (3) for overlay file common_google_signin_btn_icon_light_focused.9.png, for flavor xhdpi-v4
found a match (4) for overlay file common_google_signin_btn_icon_light_focused.9.png, for flavor xxhdpi-v4
trying overlaySet Key=common_google_signin_btn_icon_light_normal.9.png
baseFile 0 has flavor mdpi-v4
baseFile 1 has flavor tvdpi-v4
baseFile 2 has flavor hdpi-v4
baseFile 3 has flavor xhdpi-v4
baseFile 4 has flavor xxhdpi-v4
overlayFile 0 has flavor mdpi-v4
overlayFile 1 has flavor tvdpi<message truncated>

答案 5 :(得分:0)

首先做

table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
table.setRowSelectionInterval(0, 0);

然后设置ListSelectionModel