JTable will not auto resize last column - java

I have a JTable that is placed inside a JPanel which is then placed in a JFrame that has another JPanel that has a ScrollPane. The general idea can be seen below if the explanation is confusing. I have set my JTable to auto resize the last column, but it never auto sizes. What is the problem?
JFrame -> JPanel -> JTable
-> JPanel -> Scroll Pane
My code:
this refers to my class which extends JFrame
this.setLayout(new BorderLayout());
JTextArea log = new JTextArea(20, 50);
log.setEditable(false);
JScrollPane logScrollPane = new JScrollPane(log);
String[] configRow = {"Config File", "Not Loaded"};
String[] logRow = {"Log File" , "Not Loaded"};
DefaultTableModel dtm = new DefaultTableModel();
dtm.addColumn("");
dtm.addColumn("");
dtm.addRow(configRow);
dtm.addRow(logRow);
JTable status = new JTable(dtm);
status.setTableHeader(null);
status.setShowGrid(false);
status.setEnabled(false);
status.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);
JPanel tablePanel = new JPanel();
tablePanel.setLayout(new BorderLayout());
tablePanel.add(status, BorderLayout.LINE_START);
JPanel logPanel = new JPanel();
logPanel.add(logScrollPane);
this.add(tablePanel, BorderLayout.NORTH);
this.add(logPanel, BorderLayout.SOUTH);

Your JTable must be placed inside a JScrollPane in order for the resizing to work properly. You are placing the JTable directly on a JPanel.
tablePanel.add(new JScrollPane(status), BorderLayout.LINE_START):

Related

How can I move a table downward in Java?

Is it possible to move a table downward in Java. I tried setBorder, setLocation, ... but it didn't work. What should I do?
JLabel label = new JLabel("Enter proper data: ");
label.setBounds(0, 0, 120, 50);
frame.add(label);
JButton btn = new JButton("Click");
btn.setBounds(100, 300, 80, 50);
frame.add(btn);
String data [][] = new String [6][4];
String column[]={"No.","Player 1","Player 2","Strategy"};
JTable table = new JTable(data, column);
JScrollPane sp=new JScrollPane(table);
frame.add(sp);
frame.setSize(300,400);
frame.setVisible(true);
Please look at this image:
Looks good for a BorderLayout for the outer panel (outlined in blue). The big red label goes in the PAGE_START and the (scroll pane of the) table goes in the CENTER.
Put the button in a panel with a FlowLayout (centered), the panel with the red outline, in the PAGE_END of the border layout. Done.
All extra height (if the user makes the GUI bigger) will be given to the table.
I changed the code based on #AndrewThompson suggestion and result was good.
JFrame frame = new JFrame("Play");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel outerPanel = new JPanel(new BorderLayout());
JPanel topPanel = new JPanel(new BorderLayout());
JLabel label = new JLabel("Enter proper data: ");
JButton btn = new JButton("Click");
String data [][] = new String [100][4];
String column[]={"No.","Player 1","Player 2","Strategy"};
JTable table = new JTable(data, column);
JScrollPane sp=new JScrollPane(table);
topPanel.add(label, BorderLayout.PAGE_START);
topPanel.add(sp, BorderLayout.CENTER);
topPanel.add(btn, BorderLayout.PAGE_END);
outerPanel.add(topPanel);
frame.add(outerPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

BorderLayout and JScrollPane problems

I have a JFrame with three areas:
A scrollpane with a list of objects
A panel with labels and textfields
A scrollpane with a panel potentially having multiple labels
When you click the item on the list, the textfields on the panel are filled and the labels on the second scroll are created. I have two problems with my code:
For some reason the scrollpane at the botom of the screen does not fill the whole borderlayout's south area, only half of it.
the scrollpane does not show anything when the item on the list is selected.
Here I tried to make an example:
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
JPanel geral = new JPanel();
JPanel lista = new JPanel();
JPanel dados = new JPanel();
JPanel paneHist = new JPanel();
JPanel historico = new JPanel();
GridLayout gridLay = new GridLayout(0, 2, 5, 10);
geral.setLayout(gridLay);
dados.setLayout(gridLay);
historico.setLayout(new BorderLayout());
lista.setLayout(new BorderLayout());
paneHist.setLayout(gridLay);
this.setLayout(new BorderLayout());
this.add(geral);
geral.add(lista, BorderLayout.WEST);
geral.add(dados, BorderLayout.EAST);
geral.add(historico, BorderLayout.SOUTH);
DefaultListModel listModel = new DefaultListModel();
listModel.addElement("just testing");
final JList list = new JList(listModel);
list.setLayoutOrientation(JList.VERTICAL);
list.setVisible(true);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JScrollPane scroll = new JScrollPane(list);
scroll.setPreferredSize(new Dimension(100, 500));
lista.add(scroll, BorderLayout.CENTER);
JTextField jtf = new JTextField();
dados.add(new JLabel("test:"));
dados.add(jtf);
list.addListSelectionListener(new ListSelectionListener() {
#Override
public void valueChanged(ListSelectionEvent lse) {
jtf.setText("clicked");
paneHist.add(new JLabel("texttexttext"));
paneHist.add(new JLabel("texttexttext"));
}}
);
JScrollPane scrollHist = new JScrollPane(paneHist);
scrollHist.setPreferredSize(new Dimension(500, 100));
historico.add(new JLabel("Historico:"), BorderLayout.NORTH);
historico.add(scrollHist, BorderLayout.EAST);
//list.setCellRenderer(new CellRenderer());
this.validate();
this.repaint();
}
Can't really tell what you are doing from the posted code.
Some general comments:
Don't use setPreferredSize(). Let each component determined its preferred size. In the case of a JList you can use the setVisibleRowCount(...) method so the JList can calculate a reasonable size.
In your ListSelectionListener, when you add/remove components from a visible GUI you need to revalidate() and repaint() the panel.

Adding buttons and scrollable panel to a tab - Java

I'm trying to build a Java GUI where among other things there will be several tabs.
In one of the tabs I want to have both a scrollable JTextArea, and also some buttons at the top/bottom that interacts with the JTextArea. I can't figure out how to get both into the same tab, I'm either getting the buttons and a non-scrollable jtextarea, or just the scrollable jtextarea. I also don't want to display the button in the other tabs. Here is my code:
private final JTextArea music = new JTextArea();
private final JTextArea button = new JTextArea();
private final JTextArea test = new JTextArea();
private final JTabbedPane tab = new JTabbedPane();
private JTable table;
music.append(newPlaylist.toString());
JFrame frame = new JFrame("GUI");
frame.setTitle("Music File Organiser");
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton button1 = new JButton("Hello");
JButton button2 = new JButton("Sort by Album Title");
JButton button3 = new JButton("Sort by Track Title");
button1.addActionListener(new ActionListener() {code};
button2.addActionListener(new ActionListener() {code};
button3.addActionListener(new ActionListener() {code};
panel.add(music);
panel.add(button1);
panel.add(button2);
panel.add(button3);
JScrollPane scroll = new JScrollPane(panel);
JScrollPane scroll2 = new JScrollPane(table);
tab.add("Music Files", scroll);
tab.add("Table", scroll2);
this.add(tab);
frame.setLocationRelativeTo(null);
this.setSize(1200, 1000);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
The tab in mind is the first one. How do I add buttons to "scroll"? The way I'm trying it here is to add the JTextArea "music" into panel, then adding the buttons to the same panel, then adding the panel to a JScrollPane, and then adding the JScrollPane to tab. Any help would really be appreciated.
Encapsulate your visual elements into child Panels.
JPanel buttonPanel = new JPanel(); //panel for buttons.
buttonPanel.add(button1);
buttonPanel.add(button2);
buttonPanel.add(button3);
JScrollPane scroll = new JScrollPane(music); //scrollable pane for JTextArea
panel.add(buttonPanel);
panel.add(scroll); //add sub-components to panel for tab
/*here you would add some layout code to fit the panel and scroll into the associated spaces */
tab.add("Music Files", panel); //add panel to tab
You can do something like this:
JPanel tab = new JPanel()
JPanel tabWithButtons = new JPanel()
JPanel tabWithScrollPanel = new JPanel()
tab.add(tabWithScrollPanel)
tab.add(tabWithButtons)
IMO this is better way, because I assume that buttons shouldn't be included in JScrollPane and they should be visible all the time
Of course, you have to add all elements to proper JPanels.
Also, you can try with width/height settings of panel which contains all your elements, maybe ScrollArea panel is to big?

Java swing Border and Boxlayout

i have a Jtable and now i want to have a JLabel and a JTextField and a JButton in one row
above this Jtable.
How do i do that?
until now i had this:
final JTableHeader header = table.getTableHeader();
setLayout(new BorderLayout());
add(header, BorderLayout.PAGE_START);
add(table, BorderLayout.CENTER);
now i added this above:
ImageIcon leftButtonIcon = createImageIcon("images/add.gif");
JButton addButton = new JButton(leftButtonIcon);
JLabel labelF = new JLabel("Filter:");
labelF.setAlignmentX(Component.RIGHT_ALIGNMENT);
JTextField eingabeF = new JTextField();
eingabeF.setSize(50, 10);
but how to do, that there ist this: "Filter: TEXTFIELD BUTTON"
and under this there is the Table ?
Add the labelF label, eingabeF text field and addButton button to an instance of JPanel with FLowLayout. And add it to the content pane(which has BorderLayout) with constraint BorderLayout.PAGE_START. JPanel has FlowLayout as it's default layout
Enclose your table to an instance of JScrollPane and Then add the scroll pane to the content pane with constraint BorderLayout.CENTER. The scroll pane automatically places the table header at the top of the viewport.
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);

JScrollPane for a panel containing a set of labels with BoxLayout

I'd like to use a JScrollPane for a panel which has an arbitrary list of labels in it using box layout. I'm trying to get it so that the scrollbar would appear if there were too many items (labels) to display.
I tried adding a JScrollPane to the panel and then add the labels but then I don't see any scroll bar.
Any ideas?
TIA
For this kind of thing, you'd normally use a JList or JTable (if you need custom rendering).
Make sure that you call validate() or revalidate() on the JScrollPane after adding an item, to force the preferred size of the panel to be recalculated.
Here's how I did it.
JPanel midPanel = new JPanel();
midPanel.setLayout(new BoxLayout(midPanel, BoxLayout.Y_AXIS));
midPanel.add(new JLabel("<html><u>Label</u>"));
Box box = Box.createVerticalBox();
for (Item item : data.getInventory()) {
inventory.add(box.add(new JLabel(item.getName())));
}
JScrollPane jscrlpBox = new JScrollPane(box);
midPanel.add(jscrlpBox);
add(midPanel, BorderLayout.CENTER);
From:
http://www.java2s.com/Code/Java/Swing-JFC/JScrollPanetoholdscrollablecomponent.htm
Did you remember to set the preferred size of the content panel?
final JFrame frame = new JFrame("Scroll Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
final Box textArea = Box.createVerticalBox();
final JScrollPane textAreaScroll = new JScrollPane(textArea);
textAreaScroll.setPreferredSize(new Dimension(80,150)); /* essential! */
JButton addButton = new JButton("ADD");
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
textArea.add(new JLabel("abc"));
textArea.revalidate();
}
});
frame.getContentPane().add(textAreaScroll, BorderLayout.SOUTH);
frame.getContentPane().add(Box.createRigidArea(new Dimension(10,10)), BorderLayout.CENTER);
frame.getContentPane().add(addButton, BorderLayout.NORTH);
frame.pack();
frame.setVisible(true);
In this example, the scroll bar works correctly, but if you remove the line marked as "essential", it will not work anymore.

Categories

Resources