I have a TableView with CONSTRAINED_RESIZE_POLICY column resize policy.
It works great when I resize the window manually, but when I maximize it or restore it from a maximized state, the columns do not adjust.
Is there a way to force a "refresh" on the TableView so columns resize in these cases?
UPDATE: Sample compilable code to reproduce the issue
public class TableViewResizeTest extends Application {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage primaryStage) {
primaryStage.setTitle("TableView resize demo");
ObservableList<Room> roomsList = FXCollections.observableArrayList();
final TableView rooms = new TableView();
TableColumn icons = new TableColumn();
TableColumn name = new TableColumn("Name");
TableColumn topic = new TableColumn("Topic");
TableColumn users = new TableColumn("Users");
rooms.getColumns().addAll(icons, name, topic, users);
name.setCellValueFactory( new PropertyValueFactory<Room,String>("name"));
topic.setCellValueFactory( new PropertyValueFactory<Room,String>("topic"));
users.setCellValueFactory( new PropertyValueFactory<Room,String>("users"));
icons.setCellValueFactory( new PropertyValueFactory<Room,String>("icons"));
name.setMinWidth(50);
name.setMaxWidth(450);
topic.setMinWidth(10);
users.setMinWidth(100);
users.setMaxWidth(150);
icons.setMaxWidth(35);
icons.setMinWidth(35);
// Room resize policy, this is almost perfect
rooms.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
for (int i = 1; i<50; i++) roomsList.add(new Room("Sample Room "+i));
rooms.setItems(roomsList);
BorderPane root = new BorderPane();
root.setCenter(rooms);
primaryStage.setScene(new Scene(root, 500, 460));
primaryStage.show();
}
public class Room {
public Room(String name) {
this.name = new SimpleStringProperty(name);
this.topic= new SimpleStringProperty("This is a sample description text");
this.icon= new SimpleStringProperty("");
nUsers = (int)(Math.random()*1000);
this.users= new SimpleStringProperty(nUsers.toString());
}
Integer nUsers;
private SimpleStringProperty name;
private SimpleStringProperty topic;
private SimpleStringProperty users;
private SimpleStringProperty icon;
public String getName() {
return name.get();
}
public void setName(String name) {
this.name.set(name);
}
public String getTopic() {
return topic.get();
}
public void setTopic(String topic) {
this.topic.set(topic);
}
public String getUsers() {
return nUsers.toString();
}
public void setUsers(String users) {
this.users.set(users);
}
public String getIcon() {
return icon.get();
}
public void setIcon(String icon) {
this.icon.set(icon);
}
}
}
According to the resize policy, the last column "Users" must be visible at all times, and if you resize the form manually it works fine. But if you try maximizing and restoring a couple of times, you will see that it comes off view, until you resize the window a bit manually.
I suppose you want to constraint the name, users and icon columns with max and min width boundaries, while the topic column takes the rest of free space. As a workaround I suggest to put the topic column to the end of columns, i.e. rooms.getColumns().addAll(icons, name, users, topic);.
Related
I got an asignment from school where i have to read in two names in one view, then we have to press a button that goes to another view and then the two names have to come in a label there. i have been trying to do this for some time but for some reason i can't figure it out.
Here is my code on the first view
public class Startview extends GridPane {
private TextField txtplayer1;
private TextField txtplayer2;
private Button play;
this.txtplayer1 = new TextField();
this.txtplayer2 = new TextField();
this.add(txtplayer1,2,1);
this.add(txtplayer2, 2, 2);
this.add(play, 2, 3);
public Button getplay() {
return play;
}
this is the code on my presenter where i write the actions
private void eventHandlers() {
view.getplay().setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
Gameview gameview = new Gameview();
Gamepresenter presenter3 = new Gamepresenter(model, gameview);
view.getScene().setRoot(gameview);
gameview.getScene().getWindow().sizeToScene();
gameview.getScene().getWindow().centerOnScreen();
}
});
and the gameview where the labels are on(which is also on a gridpane)
private Label name1;
private Label name2;
public Label getname1() {
return Name1;
}
public Label getName2() {
return Name2;
}
all help or tips are appreciated
Just like #James_D said, just use the functions in your model. In your startview use the setName from your player-class and in gameview use the getName from your player class.
may I ask how to pass the value to other Activity using the code below? This is because I want to pass each value of TextView in my ListView to another Activity. Thank you.
public class MySimpleArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final ArrayList<String> values;
public MySimpleArrayAdapter(Context context, ArrayList<String> values) {
super(context, R.layout.rowlayout, values);
this.context = context;
this.values = values;
}
/**
* Here we go and get our rowlayout.xml file and set the textview text.
* This happens for every row in your listview.
*/
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
TextView textView = (TextView) rowView.findViewById(R.id.criteriaName);
final TextView textView2 = (TextView)rowView.findViewById(R.id.txtView2);
SeekBar seekBar = (SeekBar)rowView.findViewById(R.id.sbBar);
// Setting the text to display
textView.setText(values.get(position));
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
textView2.setText(""+i);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
return rowView;
}
}
So, how am I going to pass the value in TextView2 to another Activity?
At first convert you Textview to a String then , do this
Intent i = new Intent(CurrentActivity.this,ClassOfNextActivity.class);
i.putExtra("My_String",convertedString);
startActivity(i);
On the next Activty you have to do this, to get your String from your first Activity.
Intent i = getIntent();
String newString = (String) i.getSerializableExtra(My_String):
I was doing some homework today and I've accomplished all of the goals of the assignment, which I'm sure will get me full points.
In an earlier class, however, we used the same Event Handler for more than one action (in this example, you either type a color in the text field, or click a button to change the background color of the box).
I can't figure out how I would do that in this case... do I have to choose a Type in the constructor? If the first parameter could be a button or a textfield then I think that would help.
I'm just trying to figure out how to apply DRY (Don't Repeat Yourself), where ever I can.
public class ColorChooserApplication extends Application
{
#Override
public void start(Stage stage)
{
// Create all UI components
VBox backgroundBox = new VBox(10);
backgroundBox.setPadding(new Insets(10));
HBox topBox = new HBox(10);
HBox bottomBox = new HBox(10);
TextField colorPrompt = new TextField();
colorPrompt.setOnAction(new ColorHandler(colorPrompt, backgroundBox));
Button redButton = new Button("Red");
redButton.setOnAction(new ButtonHandler(redButton, backgroundBox));
Button whiteButton = new Button("White");
whiteButton.setOnAction(new ButtonHandler(whiteButton, backgroundBox));
Button blueButton = new Button("Blue");
blueButton.setOnAction(new ButtonHandler(blueButton, backgroundBox));
// Assemble
topBox.getChildren().add(colorPrompt);
bottomBox.getChildren().addAll(redButton, whiteButton, blueButton);
backgroundBox.getChildren().addAll(topBox, bottomBox);
backgroundBox.setAlignment(Pos.CENTER);
topBox.setAlignment(Pos.CENTER);
bottomBox.setAlignment(Pos.CENTER);
// Set scene and show
stage.setScene(new Scene(backgroundBox));
stage.show();
}
class ColorHandler implements EventHandler<ActionEvent>
{
TextField colorTf;
VBox bgVbox;
public ColorHandler(TextField colorTf, VBox bgVbox)
{
this.colorTf = colorTf;
this.bgVbox = bgVbox;
}
#Override
public void handle(ActionEvent event)
{
String color = colorTf.getText();
bgVbox.setStyle("-fx-background-color:" + color);
}
}
class ButtonHandler implements EventHandler<ActionEvent>
{
Button colorButton;
VBox bgVbox;
public ButtonHandler(Button colorButton, VBox bgVbox)
{
this.colorButton = colorButton;
this.bgVbox = bgVbox;
}
#Override
public void handle(ActionEvent event)
{
String color = colorButton.getText();
bgVbox.setStyle("-fx-background-color:" + color);
}
}
public static void main(String[] args)
{
launch(args);
}
}
If you're using Java 8, you can do
class ColorHandler implements EventHandler<ActionEvent> {
Supplier<String> colorSupplier ;
VBox bgVbox ;
public ColorHandler(Supplier<String> colorSupplier, VBox bgVbox) {
this.colorSupplier = colorSupplier ;
this.bgVbox = bgVbox ;
}
#Override
public void handle(ActionEvent event) {
String color = colorSupplier.get();
bgVbox.setStyle("-fx-background-color: "+color);
}
}
and then
colorPrompt.setOnAction(new ColorHandler(colorPrompt::getText, backgroundBox));
redButton.setOnAction(new ColorHandler(redButton::getText, backgroundBox));
Note that all you need to provide for the first parameter is some function that returns the correct string for use in the css. So you can do things like
whiteButton.setOnAction(new ColorHandler(() -> "#ffffff", backgroundBox));
blueButton.setOnAction(new ColorHandler(() -> "cornflowerblue", backgroundBox));
etc.
I tried all to populate a TableView with data. The next code inserts a new row in table but the data not appear the table. I tried to find an explication for this without success.
Please help. I can't what is wrong.
In controller.java
#FXML private TableView<TempTableData> tempTable;
#FXML private TableColumn<TempTableData,String> columnTime;
#FXML private TableColumn<TempTableData,Float> columnTempOne;
#FXML private TableColumn<TempTableData,Float> columnTempTwo;
#FXML private TableColumn<TempTableData,Float> columnTempThree;
#FXML protected void initialize() {
columnTime = new TableColumn<TempTableData,String>();
columnTime.setCellValueFactory(
new PropertyValueFactory<TempTableData,String>("Time"));
columnTempOne = new TableColumn<TempTableData,Float>();
columnTempOne.setCellValueFactory(
new PropertyValueFactory<TempTableData,Float>("Temp 1"));
columnTempTwo = new TableColumn<TempTableData,Float>();
columnTempTwo.setCellValueFactory(
new PropertyValueFactory<TempTableData,Float>("Temp 2"));
columnTempThree = new TableColumn<TempTableData,Float>();
columnTempThree.setCellValueFactory(
new PropertyValueFactory<TempTableData,Float>("Temp 3"));
tempDataList = FXCollections.observableArrayList();
tempDataList.add(new TempTableData("0",3.0f, 4f, 5f));
tempTable.setItems(tempDataList);
}
TempTableData.java
public class TempTableData {
private final SimpleStringProperty time;
private final SimpleFloatProperty dataSensorOne;
private final SimpleFloatProperty dataSensorTwo;
private final SimpleFloatProperty dataSensorThree;
public TempTableData(String time, float dataSensorOne, float dataSensorTwo, float dataSensorThree){
this.time = new SimpleStringProperty(time);
this.dataSensorOne = new SimpleFloatProperty(dataSensorOne);
this.dataSensorTwo = new SimpleFloatProperty(dataSensorTwo);
this.dataSensorThree = new SimpleFloatProperty(dataSensorThree);
}
public String getTime() {
return time.get();
}
public void setTime(String time) {
this.time.set(time);
}
public float getDataSensorOne() {
return dataSensorOne.get();
}
public void setDataSensorOne(float dataSensorOne) {
this.dataSensorOne.set(dataSensorOne);
}
public float getDataSensorTwo() {
return dataSensorTwo.get();
}
public void setDataSensorTwo(float dataSensorTwo) {
this.dataSensorTwo.set(dataSensorTwo);
}
public float getDataSensorThree() {
return dataSensorThree.get();
}
public void setDataSensorThree(float dataSensorThree) {
this.dataSensorThree.set(dataSensorThree);
}
public String toString(){
String string = String.format("[time: %s | dataSensorOne: %f |dataSensorTwo: %f |dataSensorThree: %f ]",
time.get(), dataSensorOne.get(), dataSensorTwo.get(), dataSensorThree.get());
return string;
}
}
why you creating table columns again ? ,as they already created with #FXML annotation (injection!).
remove column instance creation lines from your code and everything will be fine
// remove these lines
columnTime = new TableColumn<TempTableData,String>();
columnTempTwo = new TableColumn<TempTableData,Float>();
columnTempThree = new TableColumn<TempTableData,Float>();
make sure your cell factory values are spelt exactly as the corresponding model class values.
e.g.
private final .... SimpleStringProperty time;
and
new ...... PropertyValueFactory("time"));
This is a wierd problem.
This will be a terrible question because I have little to no information.
About two days ago I had the ViewPagerAdapter working just fine. I could swipe and it would switch between views as defined by the adapter.
However, all of a sudden (not by itself, I'm sure I did something) the TitlePagerIndicator doesn't snap to the headings and doesn't display any content. By not snapping I mean that if I drag to the left, the title will sit at 3/4 of the screen instead of snapping to the side and displaying the next page (screenshot below).
I have debugged and instantiate item is called and a proper view is returned.
However, when I open the app I'm getting a lot of warnings like these:
VFY: unable to resolve virtual method 3015: Landroid/widget/LinearLayout;.getAlpha ()F
VFY: unable to resolve direct method 3011: Landroid/widget/LinearLayout;. (Landroid/content/Context;Landroid/util/AttributeSet;I)V
VFY: unable to resolve virtual method 2965: Landroid/widget/FrameLayout;.setAlpha (F)V
I'm assuming this is a problem with my imports, but everything compiles just fine, I have the ViewPagerIndicator as a library project, as well as Sherlock.
Here's my adapter code:
public class ViewPagerAdapter extends PagerAdapter implements TitleProvider {
private static String[] titles = new String[] {
"My Klinks",
"Received Klinks"
};
private final Context context;
public ViewPagerAdapter(Context context) {
this.context = context;
}
public String getTitle(int position) {
return titles[position];
}
#Override
public int getCount() {
return titles.length;
}
#Override
public Object instantiateItem(View pager, int position) {
TextView t = new TextView(context);
t.setText("WheeeE");
return t;
}
#Override
public void destroyItem(View pager, int position, Object view) {
((ViewPager) pager).removeView((TextView) view);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view.equals(object);
}
#Override
public void finishUpdate(View view) {
}
#Override
public void restoreState(Parcelable p, ClassLoader c) {
}
#Override
public Parcelable saveState() {
return null;
}
#Override
public void startUpdate(View view) {
}
}
And here is my activity code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.main );
// set up the slidey tabs
ViewPagerAdapter adapter = new ViewPagerAdapter( this );
ViewPager pager = (ViewPager)findViewById( R.id.viewpager );
TitlePageIndicator indicator = (TitlePageIndicator)findViewById( R.id.indicator );
pager.setAdapter( adapter );
indicator.setViewPager( pager );
// set up the action bar
final ActionBar ab = getSupportActionBar();
ab.setBackgroundDrawable(getResources().getDrawable(R.drawable.ad_action_bar_gradient_bak));
}
If someone else gets the same problem:
In instantiateView: don't forget to attach your new View to the ViewPager:
#Override
public Object instantiateItem(View pager, int position) {
TextView t = new TextView(context);
t.setText("WheeeE");
((ViewPager)pager).addView(t);
return t;
}
The current version of instantiateItem gets a ViewGroup instead of a View, the solution should be the same.
Well after a couple days of banging my head against a wall I've come to the conclusion that my ViewPagerAdapter was the problem.
I simply created a dynamic fragment and created a subclass of FragmentPagerAdapter instead and now it works just fine...